C++ 继承

继承

继承基本语法

提要点:class 子类 : 继承方式 父类

cpp 复制代码
//继承语法: class 自己定义的类(子类):继承方式 父类
#include<iostream>
using namespace std;
class Person
{
public:
    void body()
    {
        cout<<"Hello pretty girl"<<"\n";
    }
    void age1()
    {
        cout<<"19"<<"\n";
    }
};
class Alaso_shuang : public Person
{
public:
    void voice()
    {
        cout<<"interesting to listen"<<"\n";
    }
};
void test()
{
    Alaso_shuang Alaso;
    Alaso.age1();
    Alaso.body();
    Alaso.voice();
}
int main()
{
    test();
    return 0;
}

继承方式

提要点:

1.继承方式分为:公共继承(public)保护继承(protected)私有继承(private)

2.当子类以共有继承 继承父类时,父类所有的对象(除了private)都是可以以共有继承形式访问

2.当子类以 保护继承 继承父类时,子类所有继承对象均以保护继承的形式出现,并且内外不能访问

3.当子类以 私有继承 继承父类时,子类所有继承对象均以私有继承的形式出现

cpp 复制代码
#include<iostream>
using namespace std;
class F
{
public:
    int a;
protected:
    int b;
private:
    int c;
};
class F1
{
public:
    int a;
protected:
    int b;
private:
    int c;
};
class F2
{
public:
    int a;
protected:
    int b;
private:
    int c;
};
class son : public F
{
public:
    void f()
    {
        a = 10;//父类中公共权限成员拿到手了
    }
protected:
    void f1()
    {
        b = 20;
    }
// private:
//     void f3()
//     {
//         c = 1;//报错啦
//     }
};

class son1 : protected F1
{
protected:
    void f1()
    {
        a = 10;
        b = 40;
        //c = 90;//继续报错,说明父类隐私不允许访问
    }
};
class son2 : private F2
{
private:
    void f5()
    {
        a = 100;
        b = 300;
    }
};
void test01()
{
    son1 s;
}
int main()
{
    test01();
    return 0;
}

继承中对象模型

提要点:父类中所有对象均会被子类继承

cpp 复制代码
#include<iostream>
using namespace std;
class f
{
public:
    int a;
protected:
    int b;
private:
    int c;
}; 
class son1:public f
{
    int a;
};
void solve()
{
    son1 s;
    cout<<"sizeof's value : "<< sizeof(s);
}
int main()
{
    solve();
    //答案是16,说明父类所有的成员都继承在了子类身上
    return 0;
}

继承同名成员处理方式

提要点:

1.访问子类同名成员(直接访问即可)

2.访问父类同名成员(需要加作用域)

cpp 复制代码
#include<iostream>
using namespace std;
class F
{
public:
    F()
    {
        a = 10000;
    }
public:
    int a;
};
class son1 : public F
{

public:
    son1()
    {
        a = 2000000;
    }
    
public:
    int a;
};
void solve()
{
    son1 s;
    cout<<s.a<<"\n";
    F f;
    cout<<f.F::a<<"\n";//父类添加作用域
}
int main()
{
    solve();
    return 0;
}

多继承

提要点:语法:子类:public 父类1,public 父类2

(不建议使用该格式,会引来麻烦)

cpp 复制代码
#include<iostream>
using namespace std;
class F1
{
public:
    F1()
    {
        m_A = 100000;
    }
public:
    int m_A;
};

class F2
{
public:
    F2()
    {
        m_B = 200000;
    }
    
public:
    int m_B;
};
class children : public F1,public F2//继承两个父类
{
public:
    int c_M;
    int d_M;
};
void test()
{
    children C;
    cout<<sizeof(C)<<"\n";
    cout<<C.m_A<<"\n";
    cout<<C.m_B<<"\n";
    //若是父类成员名称相同,就按照继承同名成员方式处理,直接加作用域即可
}
int main()
{
    test();
    return 0;
}

菱形继承

提要点:

1.菱形继承会导致所指对象的属性不明确,还会浪费空间

2.解决菱形继承的办法:虚继承 ,在父类前加一个 virtual 即可

一般我们不会写菱形继承的形式,在此也不做代码解释了哈哈哈哈哈

相关推荐
小白学大数据8 分钟前
Python 项目实战:用 Flask 构建生产级 MySQL 增删改查 REST API
开发语言·人工智能·python·mysql·flask
Nil20821 分钟前
leetcode 20有效的括号
开发语言
jaysee-sjc31 分钟前
【苍穹外卖】Day01:从零认识企业级项目开发
java·开发语言·数据库·mysql·spring·intellij-idea·mybatis
码匠许师傅32 分钟前
【C++三方组件】RE2:工业级正则的安全与性能
c++
ThornArmor35 分钟前
《向内深潜,向外飞掠》
c语言·开发语言·c++·vim·visual studio
传奇开心果编程36 分钟前
【仓颉语言基础语法学与练】第1课:从零开始
开发语言·学习·华为
j7~39 分钟前
【C++微服务项目开发脚手架】(环境篇)虚拟机 + Docker + MySQL/Redis/RabbitMQ/ES/etcd/FastDFS 全套配齐
linux·c++·ubuntu·docker·vmware·项目开发·微服务脚手架
君顾140 分钟前
本地AI错题纠正小程序:端侧推理与错题闭环实战
java·开发语言·错题
qq_1998868742 分钟前
第5板块·第4节:性能优化中的高级技巧
c++·人工智能·gpu算力
吠品43 分钟前
Python 写入 Excel 的两种主流方案实际用法总结
c语言·开发语言·算法