[Cpp]/*
Project 1: Based on the given base class Animal and main () functions.
1. Design related classes based on the given main () function and running result prompts. Observe the running results and 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. All of these members can provide the above program for transformation from the base class, this data member is used by the derived classes as the Animal data member of the abstract class.
The base class Animal and main () functions are given below:
*/
/*
* Copyright (c) 2013, computer College, Yantai University
* All rights reserved.
* File name: test. cpp
* Author: Qiu xuewei
* Completion date: January 1, May 31, 2013
* Version: v1.0
* Input Description: None
* Problem description:
* Program output:
* Problem analysis:
* Algorithm Design: omitted
*/
# Include <iostream>
Using namespace std;
Class Animal
{
Public:
Virtual void cry ()
{
Cout <"What kind of animals do I know? "<Endl;
}
};
Class Mouse: public Animal
{
Public:
Mouse (string n, char s );
Void cry ();
Private:
String name;
Char sex;
};
Mouse: Mouse (string n, char s)
{
Name = n;
Sex = s;
}
Void Mouse: cry ()
{
If (sex = 'M ')
Cout <"my name is" <name <". I am a male mouse and my cry is" squeaking "<endl;
If (sex = 'F ')
Cout <"my name is" <name <", I am a female mouse, and my cry is" squeaking "" <endl;
}
Class Cat: public Animal
{
Public:
Cat (string n, char s );
Void cry ();
Private:
String name;
Char sex;
};
Cat: Cat (string n, char s): name (n), sex (s ){}
Void Cat: cry ()
{
If (sex = 'M ')
Cout <"my name is" <name <", I am a public cat, and my cry is" Meow "" <endl;
If (sex = 'F ')
Cout <"my name is" <name <", I am a female cat, and my cry is" "<endl;
}
Class Dog: public Animal
{
Public:
Dog (string n );
Void cry ();
Private:
String name;
};
Dog: Dog (string n): name (n ){}
Void Dog: cry ()
{
Cout <"I am a dog, and my cry is" Wang "" <endl;
}
Class Giraffe: public Animal
{
Public:
Giraffe (string n, char s );
Void cry ();
Private:
String name;
Char sex;
};
Giraffe: Giraffe (string n, char s): name (n), sex (s ){}
Void Giraffe: cry ()
{
Cout <"my name is" <name <". I am a giraffe. My neck is too long and I cannot make a 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", 'F ');
P = & c1;
P-> cry ();
Dog d1 ("Droopy ");
P = & d1;
P-> cry ();
Giraffe g1 ("Gill", 'M ');
P = & g1;
P-> cry ();
Return 0;
}
/*
Project 1: Based on the given base class Animal and main () functions.
1. Design related classes based on the given main () function and running result prompts. Observe the running results and 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. All of these members can provide the above program for transformation from the base class, this data member is used by the derived classes as the Animal data member of the abstract class.
The base class Animal and main () functions are given below:
*/
/*
* Copyright (c) 2013, computer College, Yantai University
* All rights reserved.
* File name: test. cpp
* Author: Qiu xuewei
* Completion date: January 1, May 31, 2013
* Version: v1.0
* Input Description: None
* Problem description:
* Program output:
* Problem analysis:
* Algorithm Design: omitted
*/
# Include <iostream>
Using namespace std;
Class Animal
{
Public:
Virtual void cry ()
{
Cout <"What kind of animals do I know? "<Endl;
}
};
Class Mouse: public Animal
{
Public:
Mouse (string n, char s );
Void cry ();
Private:
String name;
Char sex;
};
Mouse: Mouse (string n, char s)
{
Name = n;
Sex = s;
}
Void Mouse: cry ()
{
If (sex = 'M ')
Cout <"my name is" <name <". I am a male mouse and my cry is" squeaking "<endl;
If (sex = 'F ')
Cout <"my name is" <name <", I am a female mouse, and my cry is" squeaking "" <endl;
}
Class Cat: public Animal
{
Public:
Cat (string n, char s );
Void cry ();
Private:
String name;
Char sex;
};
Cat: Cat (string n, char s): name (n), sex (s ){}
Void Cat: cry ()
{
If (sex = 'M ')
Cout <"my name is" <name <", I am a public cat, and my cry is" Meow "" <endl;
If (sex = 'F ')
Cout <"my name is" <name <", I am a female cat, and my cry is" "<endl;
}
Class Dog: public Animal
{
Public:
Dog (string n );
Void cry ();
Private:
String name;
};
Dog: Dog (string n): name (n ){}
Void Dog: cry ()
{
Cout <"I am a dog, and my cry is" Wang "" <endl;
}
Class Giraffe: public Animal
{
Public:
Giraffe (string n, char s );
Void cry ();
Private:
String name;
Char sex;
};
Giraffe: Giraffe (string n, char s): name (n), sex (s ){}
Void Giraffe: cry ()
{
Cout <"my name is" <name <". I am a giraffe. My neck is too long and I cannot make a 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", 'F ');
P = & c1;
P-> cry ();
Dog d1 ("Droopy ");
P = & d1;
P-> cry ();
Giraffe g1 ("Gill", 'M ');
P = & g1;
P-> cry ();
Return 0;
}