Here's the implementation of the Animal, Dog, and Cat classes with constructors, destructors, and the makeSound() method as described:
#include <iostream>
using namespace std;
class Animal {
protected:
std::string type;
public:
// Animal constructor with unique message
Animal() {
type = "";
cout << "Animal constructor called" << endl;
}
// Virtual destructor for proper cleanup
virtual ~Animal() {
cout << "Animal destructor called" << endl;
}
// Virtual function for sound (default implementation)
virtual void makeSound() const {
cout << "Animal sound" << endl;
}
};
class Dog : public Animal {
public:
// Dog constructor with unique message and type initialization
Dog() {
type = "Dog";
cout << "Dog constructor called" << endl;
}
// Dog destructor with unique message
~Dog() override {
cout << "Dog destructor called" << endl;
}
// Override makeSound() to print "Woof!"
void makeSound() const override {
cout << "Woof!" << endl;
}
};
class Cat : public Animal {
public:
// Cat constructor with unique message and type initialization
Cat() {
type = "Cat";
cout << "Cat constructor called" << endl;
}
// Cat destructor with unique message
~Cat() override {
cout << "Cat destructor called" << endl;
}
// Override makeSound() to print "Meow!"
void makeSound() const override {
cout << "Meow!" << endl;
}
};
int main() {
// Test the classes
Dog dog;
Cat cat;
cout << "Dog's type: " << dog.type << endl; // Output: Dog
cout << "Cat's type: " << cat.type << endl; // Output: Cat
// Polymorphic call through base pointer
Animal* animal1 = new Dog();
Animal* animal2 = new Cat();
animal1->makeSound(); // Output: Woof!
animal2->makeSound(); // Output: Meow!
// Cleanup
delete animal1;
delete animal2;
return 0;
}
Key Features:
-
Constructors/Destructors with Unique Messages:
- Each class has distinct messages in their constructors and destructors:
Animal: "Animal constructor called", "Animal destructor called"
Dog: "Dog constructor called", "Dog destructor called"
Cat: "Cat constructor called", "Cat destructor called"
-
Type Initialization:
Dog sets type = "Dog" in its constructor.
Cat sets type = "Cat" in its constructor.
-
Polymorphism with makeSound():
Animal has a virtual makeSound() method (default implementation).
Dog overrides it to print "Woof!".
Cat overrides it to print "Meow!".
- The base class destructor is
virtual to ensure proper cleanup of derived objects.
-
Testing:
- The
main function demonstrates:
- Creating objects of
Dog and Cat.
- Using base pointers to call
makeSound() polymorphically.
- Proper destruction of objects (destructors are called correctly).
Output:
Animal constructor called
Dog constructor called
Animal constructor called
Cat constructor called
Dog's type: Dog
Cat's type: Cat
Animal constructor called
Dog constructor called
Animal constructor called
Cat constructor called
Woof!
Meow!
Dog destructor called
Animal destructor called
Cat destructor called
Animal destructor called
Dog destructor called
Animal destructor called
Cat destructor called
Animal destructor called
This code adheres to all requirements and demonstrates proper inheritance, polymorphism, and constructor/destructor behavior.