C++之继承

继承是面向对象三大特性之一

有一些类与类之间存在特殊关系,例如下图中:

我们发现,定义这些类的时候,下级别的成员有上一级的共性,还有自己的特性。

这个时候我们就可以考虑用继承的方式减少重复的代码。

一,继承的基本语法

cpp 复制代码
class 子类 : 继承方式 父类
{
    ...
}

继承的好处

1.可以减少重复的代码。

2.在父类中体现共性,在子类中体现特性。

普通实现

cpp 复制代码
#include <iostream>

using namespace std;

class Java
{
public:
    void header()
    {
        cout << "首页,公开课,登录,注册...(公共头部)" << endl;
    }
    void footer()
    {
        cout << "帮助中心,交流合作,站内地图...(公共底部)" << '\n' << endl;
    }
    void left()
    {
        cout << "Java, Python, C++, ...(公共分类列表)" << endl;
    }
    void content()
    {
        cout << '\n' << "Java学习资料" << '\n' << endl;
    }
};

class CPP
{
public:
    void header()
    {
        cout << "首页,公开课,登录,注册...(公共头部)" << endl;
    }
    void footer()
    {
        cout << "帮助中心,交流合作,站内地图...(公共底部)" << '\n' << endl;
    }
    void left()
    {
        cout << "Java, Python, C++, ...(公共分类列表)" << endl;
    }
    void content()
    {
        cout << '\n' << "C++学习资料" << '\n' << endl;
    }
};

class Python
{
public:
    void header()
    {
        cout << "首页,公开课,登录,注册...(公共头部)" << endl;
    }
    void footer()
    {
        cout << "帮助中心,交流合作,站内地图...(公共底部)" << '\n' << endl;
    }
    void left()
    {
        cout << "Java, Python, C++, ...(公共分类列表)" << endl;
    }
    void content()
    {
        cout << '\n' << "Python学习资料" << '\n' << endl;
    }
};

void test01()
{
    Java ja;
    ja.header();
    ja.left();
    ja.content();
    ja.footer();
}

void test02()
{
    Python py;
    py.header();
    py.left();
    py.content();
    py.footer();
}

void test03()
{
    CPP cpp;
    cpp.header();
    cpp.left();
    cpp.content();
    cpp.footer();
}

int main()
{
    test01();
    test02();
    test03();
    return 0;
}

Result:

继承实现

cpp 复制代码
#include <iostream>

using namespace std;

class BasePage
{
public:
    void header()
    {
        cout << "首页,公开课,登录,注册...(公共头部)" << endl;
    }
    void footer()
    {
        cout << "帮助中心,交流合作,站内地图...(公共底部)" << '\n' << endl;
    }
    void left()
    {
        cout << "Java, Python, C++, ...(公共分类列表)" << endl;
    }
};

class Java : public BasePage
{
public:
    void content()
    {
        cout << '\n' << "Java学习资料" << '\n' << endl;
    }
};

class CPP : public BasePage
{
public:
    void content()
    {
        cout << '\n' << "C++学习资料" << '\n' << endl;
    }
};

class Python : public BasePage
{
public:
    void content()
    {
        cout << '\n' << "Python学习资料" << '\n' << endl;
    }
};

void test01()
{
    Java ja;
    ja.header();
    ja.left();
    ja.content();
    ja.footer();
}

void test02()
{
    Python py;
    py.header();
    py.left();
    py.content();
    py.footer();
}

void test03()
{
    CPP cpp;
    cpp.header();
    cpp.left();
    cpp.content();
    cpp.footer();
}

int main()
{
    test01();
    test02();
    test03();
    return 0;
}

Result:

二,继承方式

  • 公共继承
  • 保护继承
  • 私有继承

其都不能访问父类中 private 的信息。

三,继承同名成员处理方式

  • 子类对象可以直接访问到子类中同名成员
  • 子类对象加作用域可以访问到父类同名成员
  • 当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类中同名函数

四,多继承语法

C++允许一个类继承多个类

语法如下:

cpp 复制代码
class 子类 : 继承方式 父类1, 继承方式 父类2, ...
{
    ...
}
相关推荐
REDcker1 天前
C++变量存储与ELF段布局详解 从const全局到rodata与nm_readelf验证实践
java·c++·面试
王老师青少年编程1 天前
csp信奥赛C++高频考点专项训练之字符串 --【字符串排序】:合并序列
c++·字符串·csp·高频考点·信奥赛·字符串排序·合并序列
handler011 天前
UDP协议与网络通信知识点
c语言·网络·c++·笔记·网络协议·udp
神仙别闹1 天前
基于QT(C++)实现学生成绩管理系统
数据库·c++·qt
君义_noip1 天前
CSP-S 2025 入门级 第一轮(初赛) 完善程序(1)
c++·算法·信息学奥赛·初赛·csp 第一轮
蜡笔小马1 天前
07.C++设计模式-组合模式
c++·设计模式·组合模式
liulilittle1 天前
TCP UCP v1.0:BBR 的非破坏性约束层
网络·c++·网络协议·tcp/ip·算法·c·通信
每天回答3个问题1 天前
leetcodeHot100 | 104.二叉树的最大深度
c++·面试·
坚果派·白晓明1 天前
【鸿蒙PC三方库移植适配框架解读系列】第五篇:完整流程图与角色职责
c语言·c++·华为·harmonyos·鸿蒙
xiao_li_ya1 天前
C++学习日记1(`*`的理解、const关键词)
开发语言·c++