Write in C++ the following 3 classes. For each of them add a constructor and a d
ID: 3885188 • Letter: W
Question
Write in C++ the following 3 classes.
For each of them add a constructor and a destructor.
Constructor and destructor print in the screen a message with the name of the class and the name of the method.
For instance, “I am the constructor of the class Deer”. Create a class Animal. Create a class Vegetarian. Create a class Deer. Make the class Deer to inherits from Animal and from Vegetarian. Create a main() 3 function. Inside the main() function create a Deer object. Run your program. If everything is correct, the program should show 6 messages – the call to the constructors and destructors for Deer, Animal, and Vegetarian.
Explanation / Answer
#include<iostream>
using namespace std;
class Animal {
public:
Animal(){
cout << "I am the constructor of the class Animal ";
}
~Animal(){
cout << "I am the destructor of the class Animal ";
}
};
class Vegetarian : public Animal
{
public:
Vegetarian(){
cout << "I am the constructor of the class Vegetarian ";
}
~Vegetarian(){
cout << "I am the destructor of the class Vegetarian ";
}
};
class Deer : public Vegetarian
{
public:
Deer(){
cout << "I am the constructor of the class Deer ";
}
~Deer(){
cout << "I am the destructor of the class Deer ";
}
};
int main(){
Deer d;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.