Inherited member variable of the same name in the processing method
1, when the sub-class member variables with the same name as the parent class member variables
2, subclass still inherits from the parent class members with the same name
3, in a subclass be resolved by the scope of the same name :: operator to distinguish members ( using the same name of the base class member in a derived class, the class name explicitly qualifier )
4, members of the different positions of the same name stored in the memory
Summary: The member variables and member functions of the same name are distinguished by the scope resolution operator
#include<iostream>
using namespace std;
class A
{
public:
int a;
int b;
public:
void get()
{
cout<<"A的b: "<<b<<endl;
}
void print()
{
cout<<"父类的AAAA"<<endl;
}
protected:
private:
};
class B:public A
{
public:
int c;
int b;
public:
void get_child()
{
cout<<"B的b: "<<b<<endl;
}
void print()
{
cout<<"子类的BBBB"<<endl;
}
protected:
private:
};
//同名成员变量
int main01()
{
B b1;
b1.b = 1;//这样写默认修改的是子类的B
b1.get_child();
b1.A::b = 100;//想要修改父类的b要这样才对
b1.get();
b1.B::b = 300;//
b1.get_child();
system("pause");
return 0;
}
//同名成员函数
int main()
{
B b1;
b1.print();//默认的调用子类的b
b1.A::print();
b1.B::print();
system("pause");
return 0;
}