Tenth weekly project an animal called it.

Source: Internet
Author: User

"Item 1-Animals call it"

The following is the given base class animal declaration and main () function.

Class Animal{public:  virtual void Cry ()    {      cout<< "I don't know what kind of animal, how do I learn to bark?" "<<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;}
The running result of the program will be:

1, based on the given main () function and hints of running results, design the relevant classes, observe the running results, extract the data members needed in each class, and match the required member functions.
2, obviously, animal design for the abstract class more appropriate, animal do not need to be able to instantiate, is specifically used for the base class. Transform the program so that Animal is designed as an abstract class, at which point P = new Animal () in the main () function; An error occurs and the row is deleted.
3, each animal derived class has a" first name "data member, which is set to a member of the base class animal better. Transform the above program to use the "first name" member as an abstract class animal data member by each derived class.

#include "iostream" #include <string>using namespace Std;class animal{public:virtual void Cry () {COUT&L t;< "Don't know what kind of animal, let me learn to call?"    "<<endl;    }};class mouse:public animal{private:string name; Char sex;public:mouse (String nam, char s): Name (NAM), sex (s) {} virtual void Cry () {cout<< "My name" &LT;&L t;name<< ", is a" << ((sex== ' m ')? " Male ":" female ") <<" mouse, My Cry is: Squeak squeak! "    "<<endl;        }};class cat:public animal{private:string Name;public:cat (string nam): Name (NAM) {} virtual void Cry () { cout<< "My name is <<name<<", is a cat, my Cry is: Meow meow meow!    "<<endl;        }};class dog:public animal{private:string Name;public:dog (string nam): Name (NAM) {} virtual void Cry () { cout<< "My name is <<name<<", is a dog, my voice is: Wang Bark!    "<<endl;    }};class giraffe:public animal{private:string name; Char Sex;public:giraffe (string Nam,char s): Name (NAM), sex (s) {} virtual void Cry () {cout<<" My name is "<<name<<", is "<< ((sex== ' m ')?" Male ":" female ") <<" giraffe, my neck is too long to make a sound! "    "<<endl;    }};int Main () {Animal *p;    p = new Animal (); P->cry ();    Output: I do not know what kind of animal, let me learn how to call?    Mouse M1 ("Jerry", ' m ');    p=&m1; P->cry ();    Output: My name is Jerry, a male mouse, my voice is: Squeak Squeak!    Mouse m2 ("Jemmy", ' f ');    p=&m2; P->cry ();    Output: My name is Jemmy, is a female mouse, my cry is: Squeak Squeak!    Cat C1 ("Tom");    p=&c1; P->cry ();    Output: My name is Tom, is a cat, my Cry is: Meow meow meow!    Dog D1 ("Droopy");    p=&d1; P->cry ();    Output: My name is Droopy, is a dog, my voice is: Wang Bark!    Giraffe G1 ("Gill", ' m ');    p=&g1; P->cry ();    Output: My name is Gill, is a male giraffe, the neck is too long, can not make a sound! return 0;}
Operation Result:




Tenth weekly project an animal called it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.