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; }
輸出結(jié)果如下:
組合關系的特點
- 將其它類的對象作為當前類的成員使用
- 當前類的對象與成員對象的生命期相同
- 成員對象在用法上與普通對象完全一致
繼承關系∶父子關系
二、驚艷的繼承
面向?qū)ο笾械睦^承指類之間的父子關系
- 子類擁有父類的所有屬性和行為
- 子類就是一種特殊的父類
- 子類對象可以當作父類對象使用
- 子類中可以添加父類沒有的方法和屬性
C++ 中通過下面的方式描述繼承關系
下面就來進行繼承的初體驗:
#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; }
輸出結(jié)果如下:
重要規(guī)則:
- 子類就是一個特殊的父類
- 子類對象可以直接初始化父類對象
- 子類對象可以直接賦值給父類對象
三、繼承的意義
繼承是 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; }
輸出結(jié)果如下:
四、小結(jié)
- 繼承是面向?qū)ο笾蓄愔g的一種關系
- 子類擁有父類的所有屬性和行為
- 子類對象可以當作父類對象使用
- 子類中可以添加父類沒有的方法和屬性
- 繼承是面向?qū)ο笾写a復用的重要手段
到此這篇關于C++圖文并茂講解繼承的文章就介紹到這了,更多相關C++繼承內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C語言利用結(jié)構體數(shù)組實現(xiàn)學生成績管理系統(tǒng)
這篇文章主要為大家詳細介紹了C語言利用結(jié)構體數(shù)組實現(xiàn)學生成績管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01C/C++實現(xiàn)對STORM運行信息查看及控制的方法
這篇文章主要介紹了C/C++實現(xiàn)對STORM運行信息查看及控制的方法,需要的朋友可以參考下2014-07-07