17.C++继承

本文详细探讨了C++中的继承方式,包括public、protected和private继承,以及它们如何影响子类对父类成员的访问权限。通过实例代码展示了不同继承方式下,子类对象如何访问和使用父类的public、protected和private属性。强调了继承在对象和类角度上的访问规则,并提供了代码示例进行解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
using namespace std;
class father
{
private:
    int a;
protected:
    int b;
public:
    int c;
};

class son:public father
{
private:
    int a;
};

int main()
{
    cout<<sizeof(son)<<endl;
    return 0;
}
[root@Orz 10:36 ~]# run
16

结论:继承父类全部特性

公有继承:

子类

继承方式

父类属性整合进子类

public

protected

private

public继承

public

protected

禁止直接访问

protected继承

protected

protected

禁止直接访问

private继承

private

private

禁止直接访问

从对象角度:

一个对象只能访问一个类的共有属性,类的保护属性和私有属性是只有通过共有属性和友元函数间接访问

从类角度讲:

父类的保护属性,在子类中可以直接访问

父类的隐有属性,子类无法直接访问,这是一类区别另一个类的核心属性

1.公开继承public

#include<iostream>
using namespace std;
class Father
{
private:
    int a;
protected:
    int b;
public:
    int c;
    Father(int a = 1, int b = 2, int c = 3) :a(a), b(b), c(c) {}
    void print()
    {
        cout << "Father" << a << endl;
        cout << "Father" << b << endl;
        cout << "Father" << c << endl;
    }
};

class Son :public Father//公开继承
{
private:
    int d;
public:
    Son(int a = 1, int b = 2, int c = 3, int d = 4) :Father(a, b, c), d(d) {}
    void show()
    {
        //cout <<"Son from Father" << a << endl;//父类private无法直接访问
        cout << "Son from Father" << b << endl;//父类protected整合进子类protected
        cout << "Son from Father" << c << endl;//父类public整合进子类public
        cout << "Son" << d << endl;
    }
};

int main()
{
    Son son;
    son.c = 3;//继承Father public对外公开
    son.print();///继承Father protect对外公开
    son.show();
    return 0;
}

2.保护继承protected

#include<iostream>
using namespace std;
class Father
{
private:
    int a;
protected:
    int b;
public:
    int c;
    Father(int a = 1, int b = 2, int c = 3) :a(a), b(b), c(c) {}
    void print()
    {
        cout << "Father" << a << endl;
        cout << "Father" << b << endl;
        cout << "Father" << c << endl;
    }
};

class Son :protected Father//保护继承
{
private:
    int d;
public:
    Son(int a = 1, int b = 2, int c = 3, int d = 4) :Father(a, b, c), d(d) {}
    void show()
    {
        //cout <<"Son from Father" << a << endl;//父类private无法直接访问
        cout << "Son from Father" << b << endl;//父类protected仍然为子类protected
        cout << "Son from Father" << c << endl;//父类public沦为子类protected
        cout << "Son" << d << endl;
    }
};

int main()
{
    Son son;
    //son.c = 3;//父类public沦为子类protected,外界无法访问
    //son.print();//父类public沦为子类protected,外界无法访问
    son.show();
    return 0;
}

3.私有继承private

#include<iostream>
using namespace std;
class Father
{
private:
    int a;
protected:
    int b;
public:
    int c;
    Father(int a = 1, int b = 2, int c = 3) :a(a), b(b), c(c) {}
    void print()
    {
        cout << "Father" << a << endl;
        cout << "Father" << b << endl;
        cout << "Father" << c << endl;
    }
};

class Son :private Father//私有继承
{
private:
    int d;
public:
    Son(int a = 1, int b = 2, int c = 3, int d = 4) :Father(a, b, c), d(d) {}
    void show()
    {
        //cout <<"Son from Father" << a << endl;//父类private无访问
        cout << "Son from Father" << b << endl;//父类protected沦为子类private
        cout << "Son from Father" << c << endl;//父类public沦为子类private
        cout << "Son" << d << endl;
    }
};

int main()
{
    Son son;
    //son.c = 3;//父类public沦为子类protected,外界无法访问
    //son.print();//父类public沦为子类protected,外界无法访问
    son.show();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值