`
emowuyi
  • 浏览: 1480362 次
文章分类
社区版块
存档分类
最新评论

c++ 在子类中向父类的构造函数传递参数的小例子.包括类中常量的初始化

 
阅读更多

基类:

#include <iostream.h>

class Animal()
{

public:

Animal(int height,int weight){

cout<<"Animal"<<endl;

}
void eat()
{
cout<<"animal eat"<<endl;
}
};

子类:

class fish:public Animal()

{

public:

fish():Animal(400,300),a(20){//在子类fish中向父类的构造函数传递参数.a(20)是初始化fish类中的常量a

cout<<"fish"<<endl;

}

private:

const int a;

};

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics