多重继承例子

#include<iostream>
using namespace std;
typedef int NET2K1;
enum COLOR{red,green,blue,yellow,white,black,brown};
class net2k
{
public:
net2k(COLOR color,NET2K1 height);
virtual ~net2k(){cout<<"第一个类的析构函数!/n";}
virtual void windows()const {cout<<"第一个类的第一个虚函数!";}
virtual NET2K1 getheight() const{return itsheight;}
virtual COLOR getcolor() const {return itscolor;}
private:
NET2K1 itsheight;
COLOR itscolor;
};  //第一个类

net2k::net2k(COLOR color,NET2K1 height):
itscolor(color),itsheight(height) // 初始化变量
{
 cout<<"第一个类初始化变量/n";
}

class bird  //第二个类的声明开始
{
public:
bird(COLOR,bool);
virtual ~bird() {cout<<"第二个类的析构函数/n";}
virtual void chirp()const {cout<<"第二个类虚函数";}
virtual void fly()const
{
cout<<"第二个类的第二个有virtual的构造函数-虚函数!";
}
virtual COLOR getcolor()const {return itscolor;}
virtual bool getmigration()const {return itsmigration;}
private:
COLOR itscolor;
bool itsmigration;
};

bird::bird(COLOR color,bool migrates):
itscolor(color),itsmigration(migrates)
{
cout<<"第二个类初始化变量/n";
}

class pegasus:public net2k,public bird //从多个基类中派生
{
public:
void chirp()const{windows();} //覆盖第二个基类的虚构造函数
pegasus(COLOR,NET2K1,bool,long);
~pegasus(){cout<<"派生类的析构函数/n";}
virtual long getnumberbelievers() const
{
return itsnumberbelievers;
}
private:
long itsnumberbelievers;
};

pegasus::pegasus(
COLOR acolor,
NET2K1 height,
bool migrates,
long numbelieve):
net2k(acolor,height),
bird(acolor,migrates),
itsnumberbelievers(numbelieve)
{
cout<<"派生类的打印输出.../n";
}

int main()
{
pegasus *ppeg=new pegasus(red,5,true,10); //false
ppeg->fly();
puts("");
ppeg->windows();
cout<<"/nppeg->getheight()的输出:"<<ppeg->getheight();
cout<<"NET2K1 tall and";
//判断部分
if(ppeg->getmigration())
cout<<"/n真D^_^.";
else
cout<<"/n假D!.";
cout<<"/nppeg->getnumberbelievers()的输出:"<<ppeg->getnumberbelievers();
delete ppeg;
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值