#include <iostream>
using namespace std;
/*有些基类的方法在派生类被覆盖,但有时我们需要调用基类的方法。
* 这里提供了两种方式去调用基类被覆盖的方法(均使用作用域解析运算符::)。
* 1: 直接在派生类中调用被覆盖的基类方法,如 Person::CheckPerson() ;
* 2: 使用派生类对象调用被覆盖的基类方法,如 Tom.Person::CheckPerson();
*/
class Person {
protected: //只能在派生类内部访问protected成员,外部不可访问
bool man;
public: // public权限,可被派生类访问
void CheckPerson();
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;
}
class Man: public Person { //public继承,可访问基类public和protected权限的成员
public:
void CheckPer