C++(七)——多态
生活随笔
收集整理的這篇文章主要介紹了
C++(七)——多态
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
多態的基本語法
#include<iostream> using namespace std; //動態多態滿足條件 //1.有繼承關系 //2.子類要重寫父類的虛函數,注意重載是函數名相同但參數不同 //重寫 函數返回值類型 函數名 參數列表 完全相同//動態多態的使用 //父類的指針或引用執行子類對象 class Animal { public://虛函數virtual void speak() {cout << "動物在說話" << endl;} }; class Dog :public Animal{ public:void speak() {cout << "汪汪叫" << endl;} }; class Cat :public Animal { public:void speak() {cout << "喵喵叫" << endl;}}; //地址早綁定,在編譯階段確定函數地址 //如果想執行讓貓說話,則函數的地址需要在運行階段綁定,地址晚綁定 //父類引用指向子類的對象 void doSpeak(Animal &animal) {// Animal &animal = cat;animal.speak(); } void test01() {Cat cat;Dog dog;doSpeak(cat);doSpeak(dog); } void test02() {cout << "sizeof animal = " << sizeof(Animal) << endl; }int main() {//test01();test02();return 0; }案例1——計算器類
#include<iostream> #include<cstring> using namespace std; class Calculator { public:int getResult(string oper) {if (oper == "+")return m_num1 + m_num2;else if (oper == "-")return m_num1 - m_num2;else if (oper == "*")return m_num1 * m_num2;//如果擴展新功能,需要修改源碼//開發中提倡開閉原則//開閉原則:對擴展進行開發,對修改進行關閉}int m_num1;int m_num2; }; void test01() {Calculator c;c.m_num1 = 10;c.m_num2 = 10;cout << c.m_num1 << "+" << c.m_num2 << "=" << c.getResult("+") << endl;cout << c.m_num1 << "-" << c.m_num2 << "=" << c.getResult("-") << endl;cout << c.m_num1 << "*" << c.m_num2 << "=" << c.getResult("*") << endl; }//利用多態實現計算器 class AbstractCalculator { public:virtual int getResult() {return 0;}int m_num1;int m_num2; };class Add :public AbstractCalculator { public:int getResult() {return m_num1 + m_num2;}};class Sub :public AbstractCalculator { public:int getResult() {return m_num1 - m_num2;}};class Mul :public AbstractCalculator { public:int getResult() {return m_num1 * m_num2;}}; void test02() {//多態使用條件//父類指針或者引用指向子類對象AbstractCalculator* abc = new Add;abc->m_num1 = 100;abc->m_num2 = 100;cout << abc->m_num1 << " + " << abc->m_num2 << " = " << abc->getResult() << endl;delete abc;abc = new Sub;abc->m_num1 = 100;abc->m_num2 = 100;cout << abc->m_num1 << " - " << abc->m_num2 << " = " << abc->getResult() << endl;delete abc;abc = new Mul;abc->m_num1 = 100;abc->m_num2 = 100;cout << abc->m_num1 << " * " << abc->m_num2 << " = " << abc->getResult() << endl;delete abc;} int main() {//test01();test02();return 0; }純虛函數和抽象類
#include<iostream> using namespace std; class Base { public://純虛函數//只要有一個純虛函數,就是抽象類 無法實例化對象virtual void func() = 0;};class Son:public Base { public://抽象類的子類必須要重寫父類中的純虛函數,否則也屬于抽象類void func() {cout << "this is son" << endl;}};void test01() {Base* base = new Son;base ->func();delete base; }int main() {test01();return 0; }案例2——制作飲品
#include<iostream> using namespace std; //多態案例2 制作飲品 class AbstractDrinking { public://煮水virtual void Boil() = 0;//沖泡virtual void Brew() = 0;//導入杯中virtual void PourInCup() = 0;//加入輔料virtual void PutSomething() = 0;void makeDrink() {Boil();Brew();PourInCup();PutSomething();} };class Cofee :public AbstractDrinking { public:void Boil() {cout << "煮礦泉水" << endl;}void Brew() {cout << "沖咖啡" << endl;}void PourInCup() {cout << "倒入杯中" << endl;}void PutSomething() {cout << "加點糖" << endl;}};//制作茶葉class Tea :public AbstractDrinking { public:void Boil() {cout << "煮自來水" << endl;}void Brew() {cout << "放茶葉" << endl;}void PourInCup() {cout << "倒入杯中" << endl;}void PutSomething() {cout << "加檸檬" << endl;}};void doWork(AbstractDrinking &abs) {abs.makeDrink(); }void test01() {Cofee c;Tea t;doWork(c);doWork(t); }int main() {test01();return 0; }虛析構和純虛析構
#include<iostream> #include<cstring> using namespace std; class Animal { public:Animal() {cout << "animal構造函數調用" << endl;}//利用虛析構指針可以解決 父類釋放子類對象時不干凈的問題/*virtual ~Animal() {cout << "animal虛析構函數調用" << endl;}*///純虛析構 需要聲明也需要實現//有純虛析構函數 也屬于抽象類 無法實例化virtual ~Animal() = 0;virtual void speak() = 0;};Animal::~Animal() {cout << "animal純虛析構函數調用" << endl; }class Cat :public Animal { public:Cat(string name) {m_name = new string(name);cout << "cat構造函數調用" << endl;}void speak() {cout << *m_name << "喵喵叫" << endl;}~Cat() {if (m_name != NULL)delete m_name;m_name = NULL;cout << "cat析構函數調用" << endl;}string* m_name; };void test01() {Animal* a = new Cat("湯姆");a->speak();//父類的指針在析構時 不會調用子類中的析構函數,導致子類如果有堆區屬性,會內存泄露。delete a;}int main() {test01();return 0; }案例3——電腦組裝
#include<iostream> using namespace std; class Cpu { public:virtual void calculator() = 0; };class VideoCard { public:virtual void display() = 0; };class Memory { public:virtual void storage() = 0; }; class Intel :public Cpu, public VideoCard, public Memory{ public:void calculator() {cout << "intel cpu正在運行" << endl;}void display() {cout << "intel 顯卡正在運行" << endl;}void storage() {cout << "intel 內存正在運行" << endl;} };class Lenovo :public Cpu, public VideoCard, public Memory { public:void calculator() {cout << "lenovo cpu正在運行" << endl;}void display() {cout << "lenovo 顯卡正在運行" << endl;}void storage() {cout << "lenovo 內存正在運行" << endl;} };//電腦類 class Computer { public:Computer(Cpu *c, VideoCard *v, Memory *m) {m_c = c;m_v = v;m_m = m;}void doWork() {m_c->calculator();m_v->display();m_m->storage();}~Computer() {if (m_c != NULL) {delete m_c;m_c = NULL;}//0x000002dde96fc100 {m_c=0x0000000000000000 <NULL> m_v=0x000002dde96fbb68 {...} m_m=0x000002dde96fc3b0 {...} }if (m_m != NULL) {delete m_m;m_m = NULL;}if (m_v != NULL) {delete m_v;m_v = NULL;}cout << "析構函數已執行" << endl;} private:Cpu* m_c;VideoCard* m_v;Memory* m_m;};void test01() {Cpu* c = new Intel;VideoCard* v = new Intel;Memory* m = new Intel;Computer* computer = new Computer(c, v, m);computer -> doWork();delete computer; }int main() {test01();return 0; }總結
以上是生活随笔為你收集整理的C++(七)——多态的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++(六)——继承
- 下一篇: C++(八)——文件操作