最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C++实现的多重继承功能简单示例
时间:2022-06-25 07:43:27 编辑:袖梨 来源:一聚教程网
本文实例讲述了C++实现的多重继承功能。分享给大家供大家参考,具体如下:
多重继承
1. 多重继承即一个类继承了多个基类的属性。
2. 多重继承下派生类的构造函数必须同时负责所有基类构造函数的调用,
3. 派生类构造函数的参数个数,必须满足多个基类初始化的需要。
4. 在多重继承下,当建立派生类对象时,系统首先调用各个基类的构造函数,调用顺序与定义派生类时指定的基类顺序一致。
多重继承范例:
#include/* run this program using the console pauser or add your own getch, system("pause") or input loop */ class A { private: float fa; public: A(float a = 0) { fa = a; } float getdata() { return fa; } }; class B { private: float fb; public: B(float b = 0) { fb = b; } float getdata() { return fb; } }; class C:public A,public B { public: C(int a=0,int b=0):A(a),B(b) { } int sum() { return A::getdata()+B::getdata(); } }; int main(int argc, char** argv) { class C c1(12,23); class A *a1; a1 = &c1;//指向派生类的基类指针 //std::cout << c1.getdata() << std::endl; 此时有二义性的问题 std::cout << a1->getdata() << std::endl; std::cout << c1.B::getdata() << std::endl; std::cout << c1.A::getdata() << std::endl; return 0; }
输出:
12
23
12
相关文章
- 荧光深渊好玩吗 荧光深渊手游深度体验与玩法解析 03-30
- 彩虹之光开荒玩法全解析 彩虹之光新手开荒攻略与资源规划指南 03-30
- 前碧蓝档案制作人主导全新手游计划Project K5预计9月1日公开 03-30
- 性感死神妹力四射 推理所剩无几但很涩的雾雨迷宫 03-30
- 绝区零月城柳怎么玩 电系异常核心极性紊乱爆发天花板 03-30
- 如何在QQ邮箱撤回邮件 03-30