C++类的三种继承方式:公有、保护、私有及相互关系

一、公有继承 (public)

        形式:     class A {};

                        class A : public B {};

#include <iostream>  
using namespace std;  
  
// 基类  
class Base {  
public:  
    // 基类的公有成员  
    void show() {  
        cout << "Base class show function." << endl;  
    }  
  
protected:  
    // 基类的保护成员  
    int protectedVar = 10;  
  
private:  
    // 基类的私有成员,不能被派生类直接访问  
    int privateVar = 20;  
};  
  
// 派生类  
class Derived : public Base {  
public:  
    // 派生类的新方法  
    void display() {  
        // 访问基类的公有成员  
        show();  
  
        // 访问基类的保护成员  
        cout << "Protected variable in Base: " << protectedVar << endl;  
  
        // 尝试访问基类的私有成员会导致编译错误  
        // cout << "Private variable in Base: " << privateVar << endl; // 错误  
    }  
  
    // 重写基类的show方法  
    void show() override {  
        cout << "Derived class show function overriding base class show." << endl;  
    }  
};  
  
int main() {  
    Derived d;  
    d.display(); // 调用Derived类的display方法  
    d.show();    // 调用Derived类重写的show方法  
  
    return 0;  
}

在类的公有继承中,派生类通过公有继承的方式继承基类的数据成员及成员函数,属于基类的数据成员,在派生类可以被使用的有基类的公有的数据成员及成员函数,以及保护的数据成员及成员函数,当然我们可以通过重写的方式去实现对基类中show成员函数的访问

重写(override):子类对父类中方法进行按照自身需要进行对方法体进行重写,重写方法与父类方法在访问权限、返回值、方法名以及参数不变。

 二、保护继承(protected)

        形式:     class A {};

                        class A : protected B {};

#include <iostream>  
using namespace std;  
  
// 基类  
class Base {  
protected:  
    int x; // 在基类中为protected  
public:  
    Base(int val) : x(val) {}  
    void display() {  
        cout << "Base::x = " << x << endl;  
    }  
};  
  
// 派生类,使用保护继承  
class Derived : protected Base {  
public:  
    Derived(int val) : Base(val) {}  
    void showX() {  
        // 可以在派生类中访问基类的protected成员  
        cout << "Derived shows Base::x = " << x << endl;  
        // 也可以调用基类的公有成员函数  
        display();  
    }  
};  
  
// 另一个类,尝试访问Derived中的x(注意:这里不能直接访问)  
class AnotherClass {  
public:  
    void showDerivedX(Derived& d) {  
        // 不能直接访问Derived中继承自Base的protected成员x  
        // 下面的代码会导致编译错误  
        // cout << "AnotherClass sees d.x = " << d.x << endl;  
          
        // 但可以调用Derived的公有成员函数,如果该函数间接访问了x  
        d.showX();  
    }  
};  
  
int main() {  
    Derived d(10);  
    d.showX(); // 正确:Derived类的成员函数可以访问x  
  
    AnotherClass ac;  
    ac.showDerivedX(d); // 正确:通过Derived的公有成员函数访问x  
  
    // 下面的代码会导致编译错误,因为x在Derived中是protected的  
    // cout << "Direct access from main: d.x = " << d.x << endl;  
  
    return 0;  
}

派生类可以通过保护继承的方式,继承和调用 基类的公有成员及公有的成员函数,保护的成员函数

 三、私有继承(private)

        形式:     class A {};

                        class A : private B {};

#include <iostream>  
using namespace std;  
  
// 基类  
class Base {  
public:  
    int x; // 在基类中为public  
    void display() {  
        cout << "Base::x = " << x << endl;  
    }  
};  
  
// 派生类,使用私有继承  
class Derived : private Base {  
public:  
    Derived(int val) : Base(val) {} // 构造函数初始化列表调用基类的构造函数  
  
    // 派生类中的一个成员函数,用于显示x的值  
    // 注意:这里x在Derived中是private的,但可以在Derived的成员函数中被访问  
    void showX() {  
        cout << "Derived shows Base::x = " << x << endl; // 正确:在Derived内部可以访问  
        display(); // 正确:也可以调用基类的成员函数  
    }  
  
    // 尝试提供对x的外部访问,但这会导致编译错误  
    // int getX() { return x; } // 错误:x在Derived中是private的  
};  
  
int main() {  
    Derived d(10);  
    d.showX(); // 正确:通过Derived的公有成员函数访问x  
  
    // 下面的代码会导致编译错误,因为x在Derived中是private的  
    // cout << "Direct access from main: d.x = " << d.x << endl; // 错误  
  
    // 即使display是Base的公有成员函数,但在Derived中是作为private继承的一部分,  
    // 因此也不能直接从Derived的实例外部调用  
    // d.display(); // 错误:display在Derived中是private的  
  
    return 0;  
}

派生类通过私有继承的方式继承基类的成员函数及数据成员,在派生类中数据成员和成员函数均为私有(private),但我们可以通过派生类的公有成员函数访问基类的数据成员和成员函数。

数据成员abcd
基类A公有公有
派生类B(protected)保护保护公有公有
B的派生类C(private)不可访问不可访问不可访问不可访问

数据成员abcd
基类A公有公有
派生类B(public)公有公有公有私有
B的派生类C(protected)保护保护保护不可访问

 

数据成员abcd
基类A私有保护公有
派生类B(protected)不可访问保护保护公有
B的派生类C(private)不可访问不可访问不可访问不可访问

以上就是关于c++类的三种继承方式的讲解。 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值