Problem Description:
The following is the given base class animal declaration and main () function.
<strong><span style= "Font-family:kaiti_gb2312;font-size:18px;color: #ff6666;" >class Animal
{public
:
virtual void Cry ()
{
cout<< "I do not know what kind of animal, let me learn how to call." "<<endl;
}
;
int main () {
Animal *p;
p = new Animal ();
P->cry ();
Mouse M1 ("Jerry", ' m ');
p=&m1;
P->cry ();
Mouse m2 ("Jemmy", ' f ');
p=&m2;
P->cry ();
Cat C1 ("Tom");
p=&c1;
P->cry ();
Dog D1 ("droopy");
p=&d1;
P->cry ();
Giraffe G1 ("Gill", ' m ');
p=&g1;
P->cry ();
return 0;
} </span></strong>
The running result of the program will be:
1, according to the main () function and the hints of running results, design the relevant classes, pay attention to observe the results of the operation, extract the data members needed in each class, and match the required member functions.
Code:
#include <iostream> #include <cstring> using namespace std; Class animal{public:virtual void Cry () {cout<< "I do not know what kind of animal, let me learn how to call."
"<<endl;
}
};
Class Mouse:public animal{private:string name;
char sex; Public:mouse (String S,char x): Name (s), sex (x) {} void Cry () {cout<< "My name" <<name<< "I am a" << ;(sex== ' m '?
Male ":" Mother ") <<" mouse, My Cry is: Squeak Squeak!\n ";
}
}; Class Cat:public animal{private:string name; Public:cat (string s): Name (s) {} void Cry () {cout<< "I'm Calling
"<<name<<", I am a cat, my Cry is: Meow meow meow!\n ";
}
}; Class Dog:public animal{private:string name; Public:dog (string s): Name (s) {} void Cry () {cout<< "I'm Calling
"<<name<<", I am a dog, my cry is: Wang Bark!\n ";
}
};
Class Giraffe:public animal{private:string name;
char sex; Public:giraffe (String S,char x): Name (s), sex (x) {} void Cry () {cout<< "My name" <<name<< "I am a" <& lt; (sex== ' m '? ") Male ":" Mother ") <<" giraffe, my neck too long, hairDo not make a sound to!\n ";
}
};
int main () {Animal *p;
p = new Animal ();
P->cry ();
Mouse M1 ("Jerry", ' m ');
p=&m1;
P->cry ();
Mouse m2 ("Jemmy", ' f ');
p=&m2;
P->cry ();
Cat C1 ("Tom");
p=&c1;
P->cry ();
Dog D1 ("Droopy");
p=&d1;
P->cry ();
Giraffe G1 ("Gill", ' m ');
p=&g1;
P->cry ();
return 0;
}
Operation Result: