多继承与虚函数
来源:互联网 发布:线切割编程软件教程 编辑:程序博客网 时间:2024/06/02 07:28
#include <iostream>using namespace std;enum COLOR { Red, Green, Blue, White};class Animal{public:Animal(int);virtual ~Animal() { cout << "Animal 析构函数被调用...\n"; }virtual int GetAge() { return itsAge;}virtual void SetAge(int age) { itsAge = age; }private:int itsAge;};Animal::Animal(int age) : itsAge(age){cout << "Animal的构造函数被调用...\n";}class Horse : virtual public Animal // 这个是虚继承,{public:Horse(COLOR color, int height, int age);virtual ~Horse(){ cout << "Horse的析构函数被调用...\n"; }virtual void Whinny() const { cout << "Whinny!.."; }virtual int GetHeight() const { return itsHeight;}virtual COLOR GetColor() const { return itsColor; }private:int itsHeight;COLOR itsColor;};Horse::Horse(COLOR color,int height, int age) : itsColor(color), itsHeight(height), Animal(age){cout << "Horse构造函数被调用...\n";}class Bird : virtual public Animal // 这个是虚继承,{public:Bird(COLOR color,bool migrates, int age);virtual ~Bird() { cout << "Bird 的析构函数被调用...\n"; }// 鸟叫virtual void Chirp() const { cout << "Chirp...\n"; }virtual void Fly() const { cout << "I can Fly!"; }virtual bool GetMigration() const { return itsMigration;}virtual COLOR GetColor() const { return itsColor;}private:bool itsMigration; // itsMigration 是代表的迁移,COLOR itsColor;};Bird::Bird(COLOR color, bool migrates, int age) : itsColor(color), itsMigration(migrates), Animal(age) // 构造函数, {cout << "Bird的构造函数被调用...\n";}// 多继承,就是Pegasus继承Horse而Horse又继承Animal,Pegasus继承Bird而又继承Animal, 用虚继承解决这种二义性的问题,class Pegasus : public Horse,public Bird // 多继承,会显示二义性问题,从Horse里边继承的和从Bird里边继承的一样就是二义性问题,{public:void Chirp() const { Whinny(); }Pegasus(COLOR,int,bool,long,int);~Pegasus() { cout << "Pegasus的析构函数被调用了...\n"; }virtual long GetNumberBelievers() const{return itsNumberBelievers;}private:long itsNumberBelievers;};Pegasus::Pegasus(COLOR aColor,int height,bool migrates,long numberBelieve, int age): Horse(aColor,height,age),Bird(aColor,migrates,age),itsNumberBelievers(numberBelieve),Animal(age) // 这个是重点,{cout << "Pegasus构造函数被调用...\n";}int main(){Pegasus *pPeg = new Pegasus(Red,5,true,80,1);pPeg->Chirp();cout << endl;pPeg->Fly();cout << endl;pPeg->Whinny();cout << endl;cout << "有" << pPeg->GetNumberBelievers() << "人相信有飞马," << endl;// pPeg->GetColor(); // 在这里就会出现问题,不知道调用的是那一个的颜色,是Horse或Bird的,COLOR color = pPeg->Horse::GetColor();cout << "飞马的颜色:" << color << endl; // 输出的是0,代表的是red 因为COLOR是一个枚举,pPeg->Bird::GetColor();pPeg->GetAge();delete pPeg;return 0;}
0 0
- 虚函数与多继承
- 多继承与虚函数
- 多继承与虚函数
- 继承与虚函数
- 继承与虚函数
- 继承与虚函数
- 虚函数与继承
- 虚函数与继承
- 理解继承、虚函数与多态
- 多继承与纯虚函数接口
- 虚继承与虚函数继承
- 虚函数继承与虚继承
- 虚函数继承与虚继承
- 虚继承与虚函数继承
- 虚继承与虚函数继承
- 虚继承与虚函数继承
- 虚继承与虚函数
- 虚继承与虚函数
- 图片左右轮播
- C118 smsweb 程序成果展示,配合一点硬件改造,可全软件控制重启、刷机_20160329_七侠镇莫尛貝
- 提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果
- socket 与TCP/UDP编程
- 暑期实习第一步
- 多继承与虚函数
- 关于 CAS 不能登录的问题
- 安装VirtualBox后,vboxadd服务启动失败
- LeetCode 223 Rectangle Area
- 剑指offer-面试题16:反转链表
- Centos7 如何安装中文输入法
- python pip安装模块失败
- Lua中的loadfile、dofile、require详解
- “不能安装这个“安装 OS X EI Capitan”应用程序副本不能验证” 的解决办法