#include <iostream>
using namespace std;
/*假设基类中函数被重载,当派生类覆盖基类的成员函数时,会隐藏到基类的其他的重载函数
* 注:应该尽量避免隐藏基类的成员函数
*/
class Person {
protected: //只能在派生类内部访问protected成员,外部不可访问
bool man;
public: // public权限,可被派生类访问
void CheckPerson();
void CheckPerson(int age);
Person(bool initValue) { //构造函数的重载,带初始值
man = initValue;
cout << "Person constructor" << endl;
}
~Person() {
cout << "~Person deconstructor" << endl;
}
};
void Person::CheckPerson() {
if (man)
cout << "Person is man" << endl;
else
cout << "Person is woman" << endl;
}
void Person::CheckPerson(int age) {
if (man)
cout << "man age is " << age <<endl;
else
cout << "woman age is " << age << endl;
}
class Man: public Person { //public继承,可访问基类public和protected权限的成员
public:
void CheckPerson(); //覆
第三十二节 C++ 继承之基类的成员函数在派生类中被隐藏
最新推荐文章于 2023-06-10 11:02:52 发布