天天看點

C++深度解析 繼承的概念和意義 --- 組合,繼承(42)類之間的關系:組合、繼承。組合關系:整體與部分的關系組合關系的特點繼承關系:父子關系重要規則:繼承的意義:小結

C++深度解析 繼承的概念和意義(42)

類之間的關系:組合、繼承。

組合關系:整體與部分的關系

C++深度解析 繼承的概念和意義 --- 組合,繼承(42)類之間的關系:組合、繼承。組合關系:整體與部分的關系組合關系的特點繼承關系:父子關系重要規則:繼承的意義:小結

示例程式:(組合關系的描述)

#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;
}
           

結果如下:

C++深度解析 繼承的概念和意義 --- 組合,繼承(42)類之間的關系:組合、繼承。組合關系:整體與部分的關系組合關系的特點繼承關系:父子關系重要規則:繼承的意義:小結

組合關系的特點

将其他類的對象作為目前類的成員使用

目前類的對象與成員對象的生命期相同

成員對象在用法上與普通對象完全一緻

繼承關系:父子關系

面向對象中的繼承 指 類之間的父子關系

子類擁有父類的所有屬性和行為

子類就是一種特殊的父類

子類對象可以當作父類對象使用

子類中可以添加父類沒有的方法和屬性

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 class!" << endl;
    }
};

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

結果如下:

C++深度解析 繼承的概念和意義 --- 組合,繼承(42)類之間的關系:組合、繼承。組合關系:整體與部分的關系組合關系的特點繼承關系:父子關系重要規則:繼承的意義:小結

分析:由于Child類繼承Parent類,是以在建立Child類對象時,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 class!" << endl;
    }
};

int main()
{
    Child c;
    Parent p1 = c;
    Parent p2;
    
    c.hello();
    c.method();
    
    p2 = c;
    
    return 0;
}
           

繼承的意義:

繼承是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;
}
           

結果如下:

C++深度解析 繼承的概念和意義 --- 組合,繼承(42)類之間的關系:組合、繼承。組合關系:整體與部分的關系組合關系的特點繼承關系:父子關系重要規則:繼承的意義:小結

小結

繼承是面向對象中類之間的一種關系

子類擁有父類的所有屬性和行為

子類對象可以當作父類對象使用

子類中可以添加父類沒有的方法和屬性

繼承是面向對象中代碼複用的重要手段

繼續閱讀