提问者:小点点

在C++中,选择从哪个类中获取成员


请考虑以下代码:

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++中做到这一点?


共1个答案

匿名用户

#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;
}

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++|中|选择|类|中|获取|成员)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?