43-继承的概念和意义

注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。

测试环境:Ubuntu 10.10

GCC版本:9.2.0

 

一、思考

        类之间是否存在直接的关联关系

 

二、生活中的例子

1)组合关系:整体与部分的关系

UML图:菱形实心为组合关系 ,箭头方向指向4个成员对象。

实例分析
组合关系的描述
43-1.cpp
#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;
}

操作:

1) g++ 43-1.cpp -o 43-1.out编译正确,打印结果:

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

分析:

        最后调用Computer中构造函数。这些类组合关系。调用顺序与声明顺序有关。

 

三、类之间的组合关系

1)组合关系的特点

    -    将其它类的对象作为当前类的成员使用

    -    当前类的对象与成员对象的生命期相同

    -    成员对象在用法上与普通对象完全一致

 

四、生活中的例子

1)继承关系:父子关系

注意:单向关系。 

UML图:空心三角箭头指向基类。

 

五、惊艳的继承

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

        -    子类拥有父类的所有属性和行为

        -    子类就是一种特殊的父类

        -    子类对象可以当作父类对象使用(因为子类包含父类功能)

        -    子类中可以添加父类没有的方法和属性(扩展了父类)

 

2)C++中通过下面的方式描述继承关系

class Parent
{
    int mv;
public:
    void method() {};
};

class Child : public Parent  //描述继承关系   :表示继承
{

};

 

编程实验
继承初体验
43-2.cpp
#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();    //I'm Child class!
    c.method();    //mv = 100

    return 0;
}

操作:

1) g++ 43-2.cpp -o 43-2.out编译正确,打印结果:

Parent()            //子类创建时调用了父类构造函数(继承了父类)
I'm Child class!    //调用c.hello()打印
mv = 100            //调用c.method()打印

分析:

       定义Child c时,会调用继承自父类的构造函数,打印了Parent(),然后构造子类Child函数的构造函数(编译器提供),创建了对象c。

 

2) 修改代码,增加Parent p1 = c;:

int main()
{   
    Child c;
    Parent p1 = c;//子类对象c赋值给父类对象p1,调用了编译器提供的拷贝构造函数。
    
    c.hello();     //I'm Child class!
    c.method();    //mv = 100

    return 0;
}

编译程序,打印结果:

Parent()
I'm Child class!
mv = 100    

分析:

        子类对象c调用了拷贝构造函数,不会打印Parent()。

 

3) 修改代码:

int main()
{   
    Child c;
    Parent p1 = c; //子类对象c赋值给父类对象p1,调用了编译器提供的拷贝构造函数。
    Parent p2;     //调用父类parent无参构造函数
    
    c.hello();     //I'm Child class!
    c.method();    //mv = 100

    p2 = c;
    
    return 0;
}

运行结果:

Parent()
Parent()        //p2实例化时:调用父类parent无参构造函数
I'm Child class!
mv = 100    

 

3)重要规则

        -    子类就是一个特殊的父类

        -    子类对象可以直接初始化父类对象(反之不能)

        -    子类对象可以直接赋值给父类对象(反之不能)

 

4)继承的意义

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

编程实验
继承的强化练习
43-3.cpp
#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();  //子类调用power()
    hp.install("Ubuntu 16.04 LTS");
    hp.OS();
    
    cout << endl;
    
    MacBook mac;
    
    mac.OS();
    
    return 0;
}

编译正常,打印结果:

//HPBook类实例化过程
Memory()      //先父母的成员变量
Disk()
CPU()
MainBoard()
Computer()    //父母的自身
power()
Ubuntu 16.04 LTS
//MacBook类实例化过程
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、付费专栏及课程。

余额充值