WEEK 1 Project 1-animal class

Source: Internet
Author: User
/** Copyright and version declaration section of the Program * copyright (c) 2013, student * All rightsreserved from computer College of Yantai University. * file name: object. CPP * Author: Yang Chen * Completion Date: July 15, May 31, 2013 * version: V1.0 * input Description: none * Problem description: * program output: */# include <iostream> # include <string> using namespace STD; class animal {public: Virtual void cry () {cout <"which animal does not know, how can I learn it? "<Endl ;}}; class mouse: Public animal {public: mouse (string Nam, char S): Name (NAM), sex (s) {} // The constructor of the derived class should write virtual void cry () {cout <"my name is" <name <", is a "<(sex = 'M ')? "Male": "female") <"Mouse, my cry is: cheep! "<Endl ;}protected: Char sex; string name ;}; class Cat: Public animal {public: CAT (string Nam): Name (NAM) {} virtual void cry () {cout <"my name is" <name <". It is a cat. My cry is:! "<Endl ;}protected: string name ;}; class Dog: Public animal {public: Dog (string Nam): Name (NAM) {} virtual void cry () {cout <"my name is" <name <". It is a dog and my cry is: Wang! "<Endl;} protected: string name;}; Class Giraffe: Public animal {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 sound! "<Endl ;}protected: string name; char sex ;}; 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 ;}

Output result:

Related Article

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.