学习 c++ 笔记 类的继承——继承中的同名成员变量

xiaoxiao2021-02-28  38

#include "iostream" using namespace std; /* 对于同名成员变量,在使用的时候默认是子类的,若想要使用父类的,则需要声明的时候添加域名 */

class A { public:  int a;  int b; public:  void get()  {   cout << "b " << b<<endl;  }  void print()  {   cout << "AAAAAA" << endl;  } protected: private: }; class B :public A { public:  int b;  int c; public:  void get_c()  {   cout << "c_b " << b << endl;  }  void print()  {   cout << "BBBBBB" << endl;  } protected: private: }; void main() {  B b1;  b1.print(); //默认情况是修改子类的  b1.A::print(); //修改父类的  b1.B::print(); //修改子类的  system("pause");  return; } void main71() {  B b1;  b1.b = 2; //修改子类的b  b1.get_c();    b1.A::b = 20; //修改父类的b  b1.B::b = 30;//修改子类的b system("pause"); return ; }
转载请注明原文地址: https://www.6miu.com/read-2626818.html

最新回复(0)