C++(四十九)之类的继承方式

上一篇我们讲了类的继承,本文我们来讲解一下类的继承方式。

类的继承:

class B;

class A: 继承方式 B ...

 

继承的名词:

父类(基类)    子类(派生类)  名词是对应的。

 

要注意这里的继承方式有三种:

public   protected   private

不同的继承方式,对父类的访问权限不同:

C++(四十九)之类的继承方式

C++(四十九)之类的继承方式

大家直接看举例的图,文字描述后会变得更加不清楚。(框图能看懂就看,看不懂就看举例的图)

 

下面我们举例说明:

/****************************************************
 * brief  : 类的继承方式 
 * author : shao 
 * date   :    2020-04-01
 * note   : 类的继承方式有public/protected/private 
 *
 ****************************************************/
#include <iostream>

using namespace std;

/**
 * public 继承 
 */
class Base1{
public:
    int a;
    Base1()
    {
        this->a = 1;
        this->b = 2;
        this->c = 3; 
    }
    
protected:
    int b;
private:
    int c;
}; 

/**
 * public方式继承:
 * 父类的属性被继承过来的时候属性不变。 
 */
class Son1 : public Base1{
public:
    void showExtendsInfo(void)
    {
        cout << "this->a = " << this->a << endl; 
        cout << "this->b = " << this->b << endl; 
        //cout << "this->c = " << this->c << endl;   //c是父类的private属性,继承过来也看不到 
    }
}; 

void test01()
{
    Son1 s1;
    s1.a = 4;
    //s1.b = 5; //b是protected属性,只能类内部看到,类外看不到 
    //s1.c = 6; //c是private属性,只能类内部看到,类外看不到 
    s1.showExtendsInfo(); 
}

//=========================华丽的分割线============================ 
/**
 * protected 继承 
 */
class Base2{
public:
    int a;
    Base2()
    {
        this->a = 1;  
        this->b = 2;  
        this->c = 3; 
    }
    
protected:
    int b;
private:
    int c;
}; 

/**
 * public方式继承:
 * 父类的属性被继承过来的时候属性不变。 
 */
class Son2 : protected Base2{
public:
    void showExtendsInfo(void)
    {
        cout << "this->a = " << this->a << endl;  //a是protected继承过来的,所以到子类的属性变成protected  
        cout << "this->b = " << this->b << endl;  //b原本就是protected, 继承后属性不变 
        //cout << "this->c = " << this->c << endl;   //c是父类的private属性,继承过来也看不到 
    }
}; 

void test02()
{
    Son2 s2;  
    //s2.a = 4; //a继承过来后变成protected属性,类外部也不能方位 
    //s1.b = 5; //b是protected属性,只能类内部看到,类外看不到 
    //s1.c = 6; //c是private属性,只能类内部看到,类外看不到 
    s2.showExtendsInfo(); 
}

//=========================华丽的分割线============================ 
/**
 * private 继承 
 */
class Base3{
public:
    int a;
    Base3()
    {
        this->a = 1;  
        this->b = 2;  
        this->c = 3; 
    }
    
protected:
    int b;
private:
    int c;
}; 

/**
 * public方式继承:
 * 父类的属性被继承过来的时候属性不变。 
 */
class Son3 : private Base3{
public:
    void showExtendsInfo(void)
    {
        cout << "this->a = " << this->a << endl;  //a是private继承过来的,所以到子类的属性变成private  
        cout << "this->b = " << this->b << endl;  //b原是protected, 继承后变成private 
        //cout << "this->c = " << this->c << endl;   //c是父类的private属性,继承过来也看不到 
    }
}; 

void test03()
{
    Son3 s3;  
    //s2.a = 4; //a继承过来后变成private属性,类外部也不能方位 
    //s1.b = 5; //b是继承后也变成private属性,只能类内部看到,类外看不到 
    //s1.c = 6; //c是private属性,只能类内部看到,类外看不到 
    s3.showExtendsInfo(); 
}

int main(void)
{
    test01();
    cout << "=========================华丽的分割线============================ \n" << endl;
    test02();
    cout << "=========================华丽的分割线============================ \n" << endl;
    test03();

    return 0;
}
 

结果如下:

C++(四十九)之类的继承方式

由结果可以看到,继承后的父类权限和框图中的例子相同。