#include <iostream>
using namespace std;
// 定义基类1
class Base1 {
public:
void showBase1() {
cout << "This is Base1" << endl;
}
};
// 定义基类2
class Base2 {
public:
void showBase2() {
cout << "This is Base2" << endl;
}
};
// 定义多重继承的派生类
class Derived : public Base1, public Base2 {
public:
void showDerived() {
cout << "This is Derived from Base1 and Base2" << endl;
}
};
int main() {
Derived d;
d.showBase1(); // 调用Base1的成员函数
d.showBase2(); // 调用Base2的成员函数
d.showDerived(); // 调用派生类自己的成员函数
return 0;
}
Base1
和 Base2
。通过多重继承,Derived
类可以访问两个基类的成员函数。Derived
类的对象 d
,并通过该对象调用了来自 Base1
、Base2
以及 Derived
自己的成员函数。这个例子展示了 C++ 中如何使用多重继承来让一个类继承多个基类的功能。
下一篇:c++怎么运行代码
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站