#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;
}
};
// 派生类,多重继承自Base1和Base2
class Derived : public Base1, public Base2 {
public:
void showDerived() {
cout << "This is Derived class" << endl;
}
};
int main() {
Derived d;
d.showBase1(); // 调用Base1的方法
d.showBase2(); // 调用Base2的方法
d.showDerived(); // 调用Derived的方法
return 0;
}
showBase1 和 showBase2。main 函数中,创建了一个 Derived 类的对象 d,并通过该对象调用了来自不同基类的方法。这种结构展示了 C++ 中如何使用多重继承来组合多个基类的功能。
上一篇:c++ make_shared
下一篇:c++sort函数头文件
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站