/** 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: