C++初学者指南 第十篇(7)
来源:互联网 发布:卡地亚蓝气球档次 知乎 编辑:程序博客网 时间:2024/06/05 04:15
必备技能10.6:继承多个基类
在C++中,一个派生类是可以继承两个或者多个基类的。如下的程序中,D就继承了B1和B2两个类。
//演示继承多个基类#include <iostream>using namespace std; class B1{protected: int x;public: void showx() { cout << x << "\n"; }}; class B2{protected: int y;public: void showy() { cout << y << "\n"; }}; //类D继承了多个基类class D : public B1, public B2{public: //D是可以访问x和y的。因为它们在基类中是保护成员。 void set(int i, int j) { x = i; y = j; }}; int main(){ D ob; ob.set(10,20); //调用D提供的函数 ob.showx(); //显示从B1继承来的成员值 ob.showy();//显示从B1继承来的成员值 return 0;}
正如在这里例子中看到的那样,我们使用逗号来把继承的多个基类分割。另外,还要确保每个基类前面都有访问限定符。
- C++初学者指南 第十篇(7)
- C++初学者指南 第十一篇(7)
- C++初学者指南 第十篇(1)
- C++初学者指南 第十篇(2)
- C++初学者指南 第十篇(3)
- C++初学者指南 第十篇(4)
- C++初学者指南 第十篇(5)
- C++初学者指南 第十篇(6)
- C++初学者指南 第十篇(8)
- C++初学者指南 第十篇(9)
- C++初学者指南 第十篇(10)
- C++初学者指南 第十篇(11)
- C++初学者指南 第十篇(12)
- C+++初学者指南+第六篇(7)
- C+++初学者指南+第六篇(5)
- C+++初学者指南+第六篇(6)
- C++:初学者的指南
- Objective-C 初学者指南
- Wix打包系列(一)如何使用wix制作安装程序
- glViewPort() glOrtho()用法 (学习)
- 增加编程经验的3种途径
- Java网络编程:在服务端接收和发送数据
- GetResponse()获取错误时处理方式 500错误
- C++初学者指南 第十篇(7)
- GetSysColor()函数
- if(Conditional statement)
- Chapter19 线程和同步
- tsclient 全屏切换 -- Ctrl-Alt-Enter
- 网络开店赚钱
- svn trunk branches tags 的用法
- 大话设计模式学习--简单工厂模式
- C++初学者指南 第十篇(8)