1、類的組合關係:ios
一、總體與部分的關係c++
組合關係,從字面上來理解的話,就是誰也離不開誰,必須相互組合在一塊兒才行,例如咱們生活當中的電腦組成:web

代碼示例:微信
#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;
}
運行結果:編輯器
root@txp-virtual-machine:/home/txp# ./a.out
Memory()
Disk()
CPU()
MainBoard()
Computer()
~Computer()
~MainBoard()
~CPU()
~Disk()
~Memory()
二、組合關係的特色:函數
將其它類的對象做爲當前類的成員使用flex
當前類的對象與成員對象的生命週期相同ui
成員對象在用法上與普通對象徹底一致url
2、類的繼承關係:spa
說到這個繼承,你能夠把它類比成生活當中的父親和兒子,兒子繼承的父親的長相特徵。那麼在咱們面向對象中繼的承又是指什麼呢?
一、面向對象中的繼承是指類之間的父子關係
子類擁有父類的全部屬性和行爲
子類就是一種特殊的父類
子類對象能夠看成父類對象使用
子類中能夠添加父類中沒有的方法和屬性
二、繼承代碼描述形式:
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 calss!" << endl;
}
};
int main()
{
Child c;
c.hello();
c.method();
return 0;
}
運行結果:
root@txp-virtual-machine:/home/txp# ./a.out
Parent()
I'm Child class!
mv= 10
三、繼承規則:
子類就是一個特殊的父類
子類對象能夠直接初始化父類對象
子類對象能夠直接賦值給父類對象
說明:繼承是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;
}
運行結果:
root@txp-virtual-machine:/home/txp# ./a.out
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()
四、小結:
繼承是面向對象中類之間的一種關係
子類擁有父類的全部屬性和行爲
子類對象能夠看成父類對象使用
子類中能夠添加父類沒有的方法和屬性
繼承是面向對象中代碼複用的重要手段(換句話說,就是不要去寫重複的代碼,提升工做效率)
3、總結:
好了,今天的分享就到這裏,若是文章中有錯誤或者不理解的地方,能夠交流互動,一塊兒進步。我是txp,下期見!
本文分享自微信公衆號 - TXP嵌入式(txp1121518wo-)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。