#include <iostream>
using namespace std;
/*有时,基类存在重载的构造函数,而搞造函数对类的数据成员进行了初始化,
* 这种情况下,在继承基类时,需要对基类的数据成员进行初始化
*
*/
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:
Man() : Person(tr
第二十九节 C++ 继承之向基类传递参数
最新推荐文章于 2023-04-06 19:40:02 发布
这篇博客探讨了C++中继承的概念,并重点讲解了如何在子类构造时向基类传递参数。通过实例展示了Person类的构造、作为男性实例的标识以及析构过程。
摘要由CSDN通过智能技术生成