C++类的继承关系举例分析

发布时间:2021-11-24 11:33:27 作者:iii
来源:亿速云 阅读:202

这篇文章主要讲解了“C++类的继承关系举例分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++类的继承关系举例分析”吧!

一、类的组合关系:

1、整体与部分的关系

组合关系,从字面上来理解的话,就是谁也离不开谁,必须相互组合在一起才行,例如我们生活当中的电脑组成:

C++类的继承关系举例分析  

代码示例:

#include <iostream>
#include <string>

using namespace std;

class Memory
{
public:
    Memory()
    {
        cout << "Memory()" << endl;
    }
    ~Memory()
    {
        cout << "~Memory()" << endl;
    }
};

class Disk
{
public:
    Disk()
    {
        cout << "Disk()" << endl;
    }
    ~Disk()
    {
        cout << "~Disk()" << endl;
    }   
};

class CPU
{
public:
    CPU()
    {
        cout << "CPU()" << endl;
    }
    ~CPU()
    {
        cout << "~CPU()" << endl;
    }    
};

class MainBoard
{
public:
    MainBoard()
    {
        cout << "MainBoard()" << endl;
    }
    ~MainBoard()
    {
        cout << "~MainBoard()" << endl;
    }    
};

class Computer // 这个地方不仅描述了事物,也还描述了关系,里面只要有一个类不存在,电脑这个类也就不复存在了。
{
    Memory mMem;
    Disk mDisk;
    CPU mCPU;
    MainBoard mMainBoard;
public:
    Computer()// 这里会先调用类成员的构造函数,之后调用电脑类的构造函数,这说明了组合关系;电脑类对象的创建,要依赖上述四个类的对象的创建。
    {
        cout << "Computer()" << endl;
    }
    void power()
    {
        cout << "power()" << endl;
    }
    void reset()
    {
        cout << "reset()" << endl;
    }
    ~Computer()// 电脑类析构的时候,也会将对应的类成员析构,这说明同生死;说明了电脑类对象的存在完全依赖于类成员对象的存在。
     {
    {
        cout << "~Computer()" << endl;
    }
};

int main()
{   
    Computer c;
    
    return 0;
}

 

运行结果:

root@txp-virtual-machine:/home/txp# ./a.out
Memory()
Disk()
CPU()
MainBoard()
Computer()
~Computer()
~MainBoard()
~CPU()
~Disk()
~Memory()

 

2、组合关系的特点:

二、类的继承关系:

说到这个继承,你可以把它类比成生活当中的父亲和儿子,儿子继承的父亲的长相特征。那么在我们面向对象中继的承又是指什么呢?

1、面向对象中的继承是指类之间的父子关系

2、继承代码描述形式:

class Parent
{
    int mv;
  public:
    void method()
    {
    
    }
};
class Child : public Parent//描述继承关系
{

};
 

代码示例:

#include <iostream>
#include <string>

using namespace std;

class Parent
{
    int mv;
public:
    Parent()
    {
        cout << "Parent()" << endl;
        mv = 100;
    }
    void method()
    {
        cout << "mv = " << mv << endl;
    }
};

class Child : public Parent
{
public:
    void hello()
    {
        cout << "I'm Child calss!" << endl;
    }
};

int main()
{   
    Child c;
    
    c.hello();
    c.method();
    
    return 0;
}

 

运行结果:

root@txp-virtual-machine:/home/txp# ./a.out
Parent()
I'm Child class!
mv= 10

 

3、继承规则:

说明:继承是c++中代码复用的重要手段。通过继承,可以获得父类的所有功能,并且可以在子类中重写已有的功能,或者添加新功能。

代码示例:

#include <iostream>
#include <string>

using namespace std;

class Memory
{
public:
    Memory()
    {
        cout << "Memory()" << endl;
    }
    ~Memory()
    {
        cout << "~Memory()" << endl;
    }
};

class Disk
{
public:
    Disk()
    {
        cout << "Disk()" << endl;
    }
    ~Disk()
    {
        cout << "~Disk()" << endl;
    }   
};

class CPU
{
public:
    CPU()
    {
        cout << "CPU()" << endl;
    }
    ~CPU()
    {
        cout << "~CPU()" << endl;
    }    
};

class MainBoard
{
public:
    MainBoard()
    {
        cout << "MainBoard()" << endl;
    }
    ~MainBoard()
    {
        cout << "~MainBoard()" << endl;
    }    
};

class Computer
{
    Memory mMem;
    Disk mDisk;
    CPU mCPU;
    MainBoard mMainBoard;
public:
    Computer()
    {
        cout << "Computer()" << endl;
    }
    void power()
    {
        cout << "power()" << endl;
    }
    void reset()
    {
        cout << "reset()" << endl;
    }
    ~Computer()
    {
        cout << "~Computer()" << endl;
    }
};

class HPBook : public Computer
{
    string mOS;
public:
    HPBook()
    {
        mOS = "Windows 8";
    }
    void install(string os)
    {
        mOS = os;
    }
    void OS()
    {
        cout << mOS << endl;
    }
};

class MacBook : public Computer
{
public:
    void OS()
    {
        cout << "Mac OS" << endl;
    }
};

int main()
{   
    HPBook hp;
    
    hp.power();
    hp.install("Ubuntu 16.04 LTS");
    hp.OS();
    
    cout << endl;
    
    MacBook mac;
    
    mac.OS();
    
    return 0;
}

 

运行结果:

root@txp-virtual-machine:/home/txp# ./a.out
Memory()
Disk()
CPU()
MainBoard()
Computer()
power()
Ubuntu 16.04 LTS

Memory()
Disk()
CPU()
MainBoard()
Computer()
Mac OS
~Computer()
~MainBoard()
~CPU()
~Disk()
~Memory()
~Computer()
~MainBoard()
~CPU()
~Disk()
~Memory()

4、小结:

感谢各位的阅读,以上就是“C++类的继承关系举例分析”的内容了,经过本文的学习后,相信大家对C++类的继承关系举例分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. Python的内置方法和类的继承举例
  2. C++多重继承关系举例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:docker中info命令请求流程分析

下一篇:C++11的lambda表达式怎么使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》