[关闭]
@zimenglan 2014-11-25T13:08:16.000000Z 字数 755 阅读 4827

C++ 不能使用继承成员问题

C++


谁可以帮忙解答下,为什么这样做不行(代码里面注有error的方法)?

说是不能使用继承成员

如果我想实现这样的效果, 该怎么做?

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <map>
  6. using namespace std;
  7. class A {
  8. public:
  9. A() {}
  10. ~A(){}
  11. virtual void printInfo();
  12. void printInfo1();
  13. protected:
  14. virtual void printInfo2();
  15. void printInfo3();
  16. int a;
  17. };
  18. class B : public A {
  19. public:
  20. B() {
  21. A::a = 5;
  22. a = 10;
  23. b = 8;
  24. }
  25. void print_a() {
  26. std::cout << "parentClass: " << A::a << std::endl;
  27. std::cout << "subClass: " << a << std::endl;
  28. }
  29. ~B(){}
  30. protected:
  31. int b;
  32. int a;
  33. };
  34. // error
  35. void B::printInfo() {
  36. std::cout << "info from B - 0" << std::endl;
  37. }
  38. // error
  39. void B::printInfo1() {
  40. std::cout << "info from B - 1" << std::endl;
  41. }
  42. // error
  43. void B::printInfo2() {
  44. std::cout << "info from B - 2" << std::endl;
  45. }
  46. // error
  47. void B::printInfo3() {
  48. std::cout << "info from B - 3" << std::endl;
  49. }
  50. int main() {
  51. B b;
  52. b.print_a();
  53. return 0;
  54. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注