1.当子类成员变量与父类成员变量同名时
2.子类依然从父类继承同名成员
3.在子类中通过作用域分辨符::进行同名成员区分(在派生类中使用基类的同名成员,显示地使用类名限定符)
4.同名成员存储在内存中的不同位置
注意:基类成员的作用域延伸到所有派生类
派生类的重名成员屏蔽基类的同名成员
案例:
// 继承中同名变量和成员函数.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class A{
public:
int a;
int b;
void get(){
cout << "b: " << b << endl;
}
void print(){
cout << "我是A" << endl;
}
private:
};
class B :public A{
public:
int b;
int c;
void getChild(){
cout << "b: " << b << endl;
}
void print(){
cout << "我是B" << endl;
}
private:
};
int _tmain(int argc, _TCHAR* argv[])
{
B b1;
b1.b = 1;//默认给子类b属性赋值
b1.get();//随机值
b1.getChild();//b: 1
b1.A::b = 2;//通过域作用符来修改父类的属性
b1.get();//b: 2
b1.getChild();//b: 1
b1.print();//我是B
b1.A::print();//我是A
return 0;
}