c++虚基类
2016-06-29 20:15
405 查看
#include<iostream> #include<stdlib.h> using namespace std; class human { public: int func(){ return 1; } }; class father :virtual public human//有函数func() func1() { public: int func1(){ return 2; } }; class mother :virtual public human//有函数func() func2() { public: int func2(){ return 3; } }; class son :public father, public mother//有函数func() func() func1() func2() func3() { public: int func3(){ return 4; } }; int main() { son a; cout << a.father::func() << endl; cout << a.mother::func() << endl; cout << a.human::func() << endl; cout << a.func() << endl;//在没有说明为虚基类的时候编译错误,因为不知道调用哪一个func()函数 cout << a.func1() << endl; cout << a.func2() << endl; cout << a.func3() << endl; father b; cout << b.func() << endl; cout << b.func1() << endl; son*p = new son; cout << p->func() << endl;//在没有说明为虚基类的时候编译错误 cout << p->func1() << endl; cout << p->func2() << endl; system("pause"); return 0; }
相关文章推荐
- c++多重继承
- c++多态性
- 树莓派GPIO控制--C语言篇
- ubuntu系统vim基本配置
- C++错误累积
- #define a int[10] typedef int a[10]
- 大家好,这是我第一篇在CSDN上的博文,本篇主要讨论小型内存池的设计
- C++ 11 创建和使用 shared_ptr
- C++经典书籍推荐
- 《深入探索C++对象模型》第三章奇怪语句解释
- C++ 服务器端学习之心跳控制
- C++编程笔记:dll的生成与使用
- 解决 C/C++ 程序执行一闪而过的方法
- C++如何输出特定格式浮点数
- C++ explicit
- const在函数前与函数后的区别
- 矩阵取数游戏noip2006(c++ BigInteger的第一次运用)---重点!!
- C++中的异常处理(三)
- C++中的异常处理(二)
- C++中的异常处理(一)