类的继承格式
派生可以通过构造函数给基类的私有成员赋值
#include "iostream"
using namespace std;
class person
{
public:
person(int a,int b):a(a),b(b)
{
cout << "person-build" << endl;
}
protected:
int a;
int b;
};
class newperson:public::person
{
public:
newperson(int a,int b):person(a,b)
{
cout << "newperson-build" << endl;
}
void showperson()
{
cout << this->a << endl;
cout << this->b << endl;
}
void setpreson(int a,int b)
{
this->a=a;
this->b=b;
}
};
int main()
{
newperson rlxy(10,20);
rlxy.showperson();
rlxy.setpreson(50,60);
rlxy.showperson();
}
类只能继承基类的公有,保护成员,不能继承私有
派生类的继承方式只会在多级继承的时候产生影响
后面的博客会提到