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, ...
{
    ...
}
相关推荐
Starry_hello world1 小时前
C++ 快速幂算法
c++·算法·有问必答
2301_807611494 小时前
77. 组合
c++·算法·leetcode·深度优先·回溯
微网兔子5 小时前
伺服器用什么语言开发呢?做什么用什么?
服务器·c++·后端·游戏
YuforiaCode5 小时前
第十三届蓝桥杯 2022 C/C++组 修剪灌木
c语言·c++·蓝桥杯
YOULANSHENGMENG5 小时前
linux 下python 调用c++的动态库的方法
c++·python
CodeWithMe6 小时前
【C++】STL之deque
开发语言·c++
炯哈哈6 小时前
【上位机——MFC】运行时类信息机制
开发语言·c++·mfc·上位机
rigidwill6667 小时前
LeetCode hot 100—最长有效括号
数据结构·c++·算法·leetcode·职场和发展
阳光_你好8 小时前
C++/Qt中QActionGroup类用法
c++·qt
菜鸟射手8 小时前
QT creater和vs2017文件路径问题
linux·c++·windows·qt