请考虑以下代码:
class A {
public:
virtual void f() {
std::cout << "A-F" << std::endl;
}
virtual void g() {
std::cout << "A-G" << std::endl;
}
};
class B: public A {
public:
void f() {
std::cout << "B-F" << std::endl;
}
};
class C: public A {
public:
void g() {
std::cout << "C-G" << std::endl;
}
};
现在我要定义一个类C,它的成员f是类B,成员g来自类a,类似于这样:
class D: public B, public C {
public:
\\ Inheritate f from B
\\ Inheritate g from C
};
我如何在C++中做到这一点?
#include <iostream>
using namespace std;
class A {
public:
virtual void f() {
std::cout << "A-F" << std::endl;
}
void g() {
std::cout << "A-G" << std::endl;
}
};
class B: public A {
public:
void f() {
std::cout << "B-F" << std::endl;
}
};
class C: public B{
};
int main()
{
C c;
c.f();
c.g();
return 0;
}