一种错误的观念:

子类继承父类,只把父类的公有成员继承下来,私有的不会继承。

事实上无论是如何继承,都会把父类的所有成员继承下来。

 1 #include<iostream>
 2 using namespace std;
 3 
 4 class Base {
 5 private:
 6     int x;
 7 };
 8 
 9 class D :private Base{
10 public:
11     int y;
12 };
13 
14 int main()
15 {
16     cout << sizeof(Base) << endl;
17     cout << sizeof(D) << endl;
18     return 0;
19 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-03
  • 2021-08-16
  • 2021-10-17
  • 2021-08-02
  • 2022-12-23
  • 2021-11-06
猜你喜欢
  • 2021-09-05
  • 2021-07-24
  • 2020-05-21
  • 2021-08-15
  • 2021-07-27
  • 2021-07-15
  • 2022-12-23
相关资源
相似解决方案