是的,C++支持多继承
#include <iostream>
class Base1 {
public:
void methodBase1() {
std::cout << "Method from Base1" << std::endl;
}
};
class Base2 {
public:
void methodBase2() {
std::cout << "Method from Base2" << std::endl;
}
};
class Derived : public Base1, public Base2 {
public:
void methodDerived() {
std::cout << "Method from Derived" << std::endl;
}
};
int main() {
Derived d;
d.methodBase1(); // Calls methodBase1 from Base1
d.methodBase2(); // Calls methodBase2 from Base2
d.methodDerived(); // Calls methodDerived from Derived
return 0;
}
在这个例子中,Derived
类继承了Base1
和Base2
类。因此,Derived
类的对象可以访问这两个基类的公共方法。