[Solved]Modify Code Mammalscpp Include Mice Labradors Spaniels Types Dogs Print Methods Objects Ca Q37023225
Modify the code mammals.cpp to include:
- mice
- labradors and spaniels (types of dogs)
- print methods for all objects (which calls all the printmethods up the inheritance tree)
- one piece of data for each object specific to that object (andthis should be printed in the print function)
- Add a static variable “totalNumber” to mammal class to keeptrack of number of mammals (all mammals), and include in printstatement
- Add copy constructor, assignment operator, and destructor tomammal class
Code “mammals.cpp” is provided below:
/* C++ code to illustrate inheritance and polymorphism. Make sure you understand why the output is what it is. QUESION: What happens when you comment out the speak method in the cat class?*/ #include <iostream>using namespace std;//————————————————————————–class Mammal { protected: double weight; public: Mammal(){ weight=0.0; } virtual void speak() { // void speak() { cout << ” Sound = I don’t know what a mammal sounds like!” << endl; } virtual void speak( double volume ) { cout << ” Sound? ” << ” volume= ” << volume << endl; } virtual void printWeight() {cout << “I dont know what mammal weighs!”<<endl;} virtual void setWeight(double w){ weight = w; }};//—————————-class Dog: public Mammal { private: double speed; public: Dog(){ weight=2.0;} void speak() { //cout << weight <<endl; cout << ” Sound = woof! (dog)” << endl; } void printWeight(){ cout << “Dog weighs: ” << weight << endl; }};//—————————-class Cat: public Mammal { private: double age; public: virtual void speak() { cout << ” Sound = meow! (cat)” << endl; } void speak( double volume ) { cout << ” Sound = meow! (cat)” << ” volume= ” << volume << endl; } void printWeight(){ cout << “Cat weighs: ” << weight << endl; }};//—————————-class Lion: public Cat { private: bool zoo; public: /* void speak() { cout << ” Sound = grrr! (lion)” << endl; } */};//—————————————————————-int main() { int answer; cout << ” This is a code to illustrate inheritance and polmorphism”; cout << endl; cout << ” It creates an array of four mammals: dog, mammal, lion, and cat”; cout << endl; Mammal * group[5]; group[0] = new Dog; group[1] = new Mammal; group[2] = new Lion; group[3] = new Cat; cout << ” Do you want the 5th member to be a Dog (1) or a Cat (2)?”<<endl; cin >> answer; if ( answer == 1) group[4] = new Dog; else if ( answer == 2) group[4] = new Cat; else { cout << “error. enter 1 or 2.” << endl; return 1; } cout << “————————————” << endl; cout << ” have all objects call speak() :” << endl; for ( int i=0 ; i<5 ; i++ ) { group[i]->speak(); } cout << “————————————” << endl; cout << ” have a couple objects call setWeight :” << endl; group[0]->setWeight(5.0); group[2]->setWeight(50.0); cout << “————————————” << endl; cout << ” have all objects call printWeight() :” << endl; for ( int i=0 ; i<5 ; i++ ) { group[i]->printWeight(); } cout << “————————————” << endl; cout << ” have object [3] call speak(5.6) :” << endl; group[3]->speak( 5.6 ) ; return 0;}
Expert Answer
Answer to Modify the code mammals.cpp to include: mice labradors and spaniels (types of dogs) print methods for all objects (whic… . . .
OR

