C++笔记 第四十三课 继承的概念和意义---狄泰学院

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42187898/article/details/84328709

如果在阅读过程中发现有错误,望评论指正,希望大家一起学习,一起进步。
学习C++编译环境:Linux

第四十三课 继承的概念和意义

1.思考

类之间是够存在直接的关联关系?
2.生活中的例子
组合关系:整体与部分的关系
在这里插入图片描述

43-1 组合关系的描述

强大之处在于:描述生活中的对象—面向对象

#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()

3.类之间的组合关系

组合关系的特点
其他类的对象作为当前类的成员使用
当前类的对象与成员对象的生命期相同
成员对象在用法上与普通对象完全一致

4.生活中的例子

继承关系:父子关系
在这里插入图片描述

5.惊艳的继承

面向对象中的继承之类之间的父子关系
子类拥有父类的所有属性和行为
子类就是一种特殊的父类
子类对象可以当做父类对象使用
子类中可以添加父类没有的方法和属性
C++中通过下面的方式描述继承关系
在这里插入图片描述

43-2 继承初体验

#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 class!" << endl;
    }
};
int main()
{   
    Child c;
    c.hello();
    c.method();
    
    return 0;
}
运行结果
Parent()
I'm Child class!
mv = 100

重要原则:
子类就是一个特殊的父类
子类对象可以直接初始化父类对象
子类对象可以直接赋值给父类对象

6.继承的意义

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

43-3 继承的强化练习

#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()

小结
继承是面向对象中类之间的一种关系
子类拥有父类的所有属性和行为
子类对象可以当做父类对象使用
子类中可以添加父类没有的方法和属性
继承是面向对象中代码复用的重要手段

猜你喜欢

转载自blog.csdn.net/weixin_42187898/article/details/84328709