You are on page 1of 3

1: #include <iostream>

2: #include <string>
3: // Base class
4: class Animal {
5: protected:
6: std::string name;
7: int age;
8: std::string species;
9:
10: public:
11: Animal(const std::string& name, int age, const std::string& species)
12: : name(name), age(age), species(species) {}
13:
14: virtual void makeSound() const {
15: std::cout << "The animal makes a sound.\n";
16: }
17:
18: void displayInfo() const {
19: std::cout << "Name: " << name << std::endl;
20: std::cout << "Age: " << age << std::endl;
21: std::cout << "Species: " << species << std::endl;
22: }
23: };
24:
25: // Derived classes
26: class Mammal : public Animal {
27: protected:
28: std::string furColor;
29:
30: public:
31: Mammal(const std::string& name, int age, const std::string& species, const std::string& furColor)
32: : Animal(name, age, species), furColor(furColor) {}
33:
34: void makeSound() const override {
35: std::cout << "The mammal makes a sound.\n";
36: }
37:
38: void displayInfo() const {
39: Animal::displayInfo();
40: std::cout << "Fur Color: " << furColor << std::endl;
41: }
42: };
43:
44: class Bird : public Animal {
45: protected:
46: double wingspan;
47:
48: public:
49: Bird(const std::string& name, int age, const std::string& species, double wingspan)
50: : Animal(name, age, species), wingspan(wingspan) {}
51:
52: void makeSound() const override {
53: std::cout << "The bird makes a sound.\n";
54: }
55:
56: void displayInfo() const {
57: Animal::displayInfo();
58: std::cout << "Wingspan: " << wingspan << std::endl;
59: }
60: };
61:
62: class Reptile : public Animal {
63: protected:
64: bool isVenomous;
65:
66: public:
67: Reptile(const std::string& name, int age, const std::string& species, bool isVenomous)
68: : Animal(name, age, species), isVenomous(isVenomous) {}
69:
70: void makeSound() const override {
71: std::cout << "The reptile makes a sound.\n";
72: }
73:
74: void displayInfo() const {
75: Animal::displayInfo();
76: std::cout << "Venomous: " << (isVenomous ? "Yes" : "No") << std::endl;
77: }
78: };
79:
80: // Additional animal classes
81: class Lion : public Mammal {
82: public:
83: Lion(const std::string& name, int age, const std::string& furColor)
84: : Mammal(name, age, "Lion", furColor) {}
85:
86: void makeSound() const override {
87: std::cout << "The lion roars.\n";
88: }
89: };
90:
91: class Penguin : public Bird {
92: public:
93: Penguin(const std::string& name, int age, double wingspan)
94: : Bird(name, age, "Penguin", wingspan) {}
95:
96: void makeSound() const override {
97: std::cout << "The penguin honks.\n";
98: }
99: };
100:
101: class Snake : public Reptile {
102: public:
103: Snake(const std::string& name, int age, bool isVenomous)
104: : Reptile(name, age, "Snake", isVenomous) {}
105:
106: void makeSound() const override {
107: std::cout << "The snake hisses.\n";
108: }
109: };
110:
111: int main() {
112: std::string name;
113: int age;
114: std::string species;
115: std::string furColor;
116: double wingspan;
117: bool isVenomous;
118:
119: std::cout << "Enter the details of a Lion:" << std::endl;
120: std::cout << "Name: ";
121: std::cin >> name;
122: std::cout << "Age: ";
123: std::cin >> age;
124: std::cout << "Fur Color: ";
125: std::cin >> furColor;
126: Lion lion(name, age, furColor);
127:
128: std::cout << "\nEnter the details of a Penguin:" << std::endl;
129: std::cout << "Name: ";
130: std::cin >> name;
131: std::cout << "Age: ";
132: std::cin >> age;
133: std::cout << "Wingspan: ";
134: std::cin >> wingspan;
135: Penguin penguin(name, age, wingspan);
136:
137: std::cout << "\nEnter the details of a Snake:" << std::endl;
138: std::cout << "Name: ";
139: std::cin >> name;
140: std::cout << "Age: ";
141: std::cin >> age;
142: std::cout << "Is Venomous (1 for Yes, 0 for No): ";
143: std::cin >> isVenomous;
144: Snake snake(name, age, isVenomous);
145:
146: std::cout << "\n--- Displaying Information ---" << std::endl;
147: lion.displayInfo();
148: std::cout << std::endl;
149: penguin.displayInfo();
150: std::cout << std::endl;
151: snake.displayInfo();
152: std::cout << std::endl;
153:
154: std::cout << "\n--- Making Sounds ---" << std::endl;
155: lion.makeSound();
156: penguin.makeSound();
157: snake.makeSound();
158:
159: return 0;
160: }

You might also like