第四十三课:继承的概念和意义----狄泰软件学院

文章来源于狄泰软件学院唐老师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;
}
Memory()
Disk()
CPU()
MainBoard()
Computer()
~Computer()
~MainBoard()
~CPU()
~Disk()
~Memory()

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述实例分析:继承初体验

#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;
}
Parent()
I'm Child calss!
mv = 100

重要规则:

  1. 子类就是一个特殊的父类
  2. 子类对象可以直接初始化父类对象
  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;
}
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()

总结:

  1. 继承是面向对象中类之间的一种关系
  2. 子类拥有父类的所有属性和行为
  3. 子类对象可以当作父类对象使用
  4. 子类中可以添加父类没有的方法和属性
  5. 继承是面向对象中代码复用的重要手段
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值