13th weeks-polymorphism-Project 1-animals, 13th weeks 1-
/** Copyright (c) 2014, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Liu Chang * Completion Date: July 15, May 30, 2015 * version number: v1.0 ** Problem description: 1. According to the prompt of the given main () function and running result, design related classes, observe the running results, extract the data members required for each class, and match the required member functions. 2. Obviously, the Animal design is more suitable for abstract classes. Animal does not need to be instantiated and is used for base classes. Transform the program to design Animal as an abstract class. In this case, p = new Animal (); in the main () function, an error occurs and this row is deleted. 3. Each derived class of Animal has a "name" data member, which is better for setting members of the base class Animal. Transform the above program and use the "name" member as the Animal data member of the abstract class to be used by the derived classes .. * Input Description:; * program output: outputs the sounds of different animals:
(1) The Code is as follows:
# Include <iostream> using namespace std; class Animal {public: virtual void cry () {cout <"what kind of Animal do I learn? "<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 is" <name <", is a "<(sex = 'M ')? "Male": "female") <"Mouse, my cry is: cheep! "<Endl ;}}; class Cat: public Animal {private: string name; public: Cat (string nam): name (nam) {} virtual void cry () {cout <"my name is" <name <". It's a cat. My cry is: Meow! "<Endl ;}}; class Dog: public Animal {private: string name; public: Dog (string nam): name (nam) {} virtual void cry () {cout <"my name is" <name <". It is a dog and my cry is: Wang! "<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": "Mother") <"giraffe, my neck is too long to sound! "<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 ;}
Running result:
(2) The Code is as follows:
# Include <iostream >#include <string> using namespace std; class Animal {public: virtual void cry () = 0 ;}; 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 is" <name <", is a" <(sex = 'M ')? "Male": "female") <"Mouse, my cry is: cheep! "<Endl ;}}; class Cat: public Animal {private: string name; public: Cat (string nam): name (nam) {} virtual void cry () {cout <"my name is" <name <". It's a cat. My cry is: Meow! "<Endl ;}}; class Dog: public Animal {private: string name; public: Dog (string nam): name (nam) {} virtual void cry () {cout <"my name is" <name <". It is a dog and my cry is: Wang! "<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": "Mother") <"giraffe, my neck is too long to sound! "<Endl ;}}; int main () {Animal * p; 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 ;}
Running result:
(3) The Code is as follows:
# Include <iostream> # include <cstring> using namespace std; class Animal {protected: string name; public: Animal (string nam): name (nam) {} virtual void cry () = 0 ;}; class Mouse: public Animal {private: char sex; public: Mouse (string nam, char s): Animal (nam ), sex (s) {} virtual void cry () {cout <"my name is" <name <", is a "<(sex = 'M ')? "Male": "female") <"Mouse, my cry is: cheep! "<Endl ;}}; class Cat: public Animal {public: Cat (string nam): Animal (nam) {} virtual void cry () {cout <"my name is" <name <". It's a cat. My cry is: Meow! "<Endl ;}}; class Dog: public Animal {public: Dog (string nam): Animal (nam) {} virtual void cry () {cout <"my name is" <name <". It is a dog and my cry is: Wang! "<Endl ;}}; class Giraffe: public Animal {private: char sex; public: Giraffe (string nam, char s): Animal (nam), sex (s) {} virtual void cry () {cout <"my name is" <name <", is" <(sex = 'M ')? "Male": "Mother") <"giraffe, my neck is too long to sound! "<Endl ;}}; int main () {Animal * p; 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 ;}
Running result:
Learning Experience:
This problem was encountered on OJ. At that time, I could not do it, but I don't know how to write it. I went to do it after reading the virtual function two days ago, and I decided to do it again.