13th Week Item 1-animals so called (1)

Source: Internet
Author: User

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" <&lt ;(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:


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.