一个简单的C++程序
2018-06-04
这样?
#include <iostream>
#include <string>
using namespace std;
class Chicken{
public:
string name;
float weight;
public:
Chicken(string n, float w){
name = n;
weight = w;
}
void showName(){
cout<<name<<": "<<weight<<"kg"<<endl;
}
virtual void simulateCluck(){
cout<<name<<": "<<"鸡叫了"<<endl;
}
void Show_Sing(){
showName();
simulateCluck();
cout<<endl;
}
};
class Chick:public Chicken{
public:
Chick(string n, float w):Chicken(n,w){
}
void simulateCluck(){
cout<<name<<": "<<"小鸡,叽叽叽"<<endl;
}
};
class Hen:public Chicken{
public:
Hen(string n, float w):Chicken(n,w){}
void simulateCluck(){
cout<<name<<": "<<"母鸡,咯咯哒"<<endl;
}
};
class Cock:public Chicken{
public:
Cock(string n, float w):Chicken(n,w){}
void simulateCluck(){
cout<<name<<": "<<"公鸡,咕咕咕"<<endl;
}
};
int main()
{
Chicken *c1 = new Chicken("c1",1);
Chicken *c2 = new Chick("c2",0.2);
Chicken *c3 = new Hen("c3",1.5);
Chicken *c4 = new Cock("c4",2.5);
c1->Show_Sing();
c2->Show_Sing();
c3->Show_Sing();
c4->Show_Sing();
delete c1;
c1=NULL;
delete c2;
c2=NULL;
delete c3;
c4=NULL;
delete c4;
c4=NULL;
return 0;
}