C++基础 -16- 类的继承

发布时间 2024-01-05 09:06:04作者: 日落悬崖

 ———————类的继承———————

 

? 派生可以通过构造函数给基类的私有成员赋值

 

?类的继承格式(图片+代码段)

 

#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();


}

?类只能继承基类的公有,保护成员,不能继承私有

 

?派生类的继承方式只会在多级继承的时候产生影响

?后面的博客会提到

———————End———————