您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: