#include<iostream.h>
class Animal{
public:
Animal(int height,int weight){
//cout<<"animal construct"<<endl;
}
~Animal(){
//cout<<"animal disconstruct"<<endl;
}
void eat(){
cout<<"animal eat"<<endl;
}
void sleep(){
cout<<"animal sleep"<<endl;
}
void breathe(){
cout<<"animal breath"<<endl;
}
};
class Fish: public Animal{
public:
Fish():Animal(400,300),a(1){ //子类中向父类带参数的构造函数传递参数的一种方式
// cout<<"fish construct"<<endl;
}
~Fish(){
// cout<<"fish disconstruct"<<endl;
}
void breathe()
{
//Animal::breathe(); // : 冒号是作用域标识符,主要用于表示是属于哪一个类的.
cout<<"fish bubble"<<endl;
}
private:
const int a; //常量是必须要进行初始化的
};
void fn(Animal *pAn){
pAn->breathe();
}
void main(){
Fish fs;
//fs.breathe();
Animal *pAn;
pAn = &fs;
fn(pAn);
}
程序运行结果如下: