多態
1 基本概念
多態是C++面向對象三大特性之一
多態分爲兩類
- 靜態多態: 函數重載 和 運算符重載屬於靜態多態,複用函數名
- 動態多態: 派生類和虛函數實現運行時多態
靜態多態和動態多態區別:
- 靜態多態的函數地址早綁定 - 編譯階段確定函數地址
- 動態多態的函數地址晚綁定 - 運行階段確定函數地址
下面通過案例進行講解多態
#include <iostream>
using namespace std;
class animal
{
public:
//Speak函數就是虛函數
//函數前面加上virtual關鍵字,變成虛函數,那麼編譯器在編譯的時候就不能確定函數調用了
virtual void speak()
{
cout << "動物在說話" << endl;
}
};
class cat : public animal
{
public:
void speak()
{
cout << "cat說話" << endl;
}
};
class dog : public animal
{
public:
void speak()
{
cout << "dog說話" << endl;
}
};
//我們希望傳入什麼對象,那麼就調用什麼對象的函數
//如果函數地址在編譯階段就能確定,那麼靜態聯編
//如果函數地址在運行階段才能確定,就是動態聯編
void dospeak(animal & Animal)
{
Animal.speak();
}
//多態滿足條件:
//1、有繼承關係
//2、子類重寫父類中的虛函數
//多態使用:
//父類指針或引用指向子類對象
int main()
{
cat ca;
ca.speak();
dog dog1;
dog1.speak();
system("pause");
return 0;
}
運行結果:
總結
多態滿足條件
- 有繼承關係
- 子類重寫父類中的虛函數
多態使用條件 - 父類指針或引用指向子類對象
重寫:函數返回值類型 函數名 參數列表 完全一致稱爲重寫
2 計算器舉例
案例描述:
分別利用普通寫法和多態技術,設計實現兩個操作數進行運算的計算器類
多態的優點:
代碼組織結構清晰
可讀性強
利於前期和後期的擴展以及維護
普通寫法:
#include <iostream>
using namespace std;
class calculator
{
public:
int result(string oper)
{
if (oper == "+")
{
return number1 + number2;
}
else if (oper == "-")
{
return number1 - number2;
}
else if (oper == "*")
{
return number1 * number2;
}
else
{
return 0;
}
}
int number1;
int number2;
};
void testcalculator()
{
calculator cal;
cal.number1 = 100;
cal.number2 = 200;
cout << cal.number1 << "+" << cal.number2 << "=" << cal.result("+") << endl;
cout << cal.number1 << "-" << cal.number2 << "=" << cal.result("-") << endl;
cout << cal.number1 << "*" << cal.number2 << "=" << cal.result("*") << endl;
}
int main()
{
testcalculator();
system("pause");
return 0;
}
多態寫法:
#include <iostream>
using namespace std;
class calculator
{
public:
virtual int result()
{
return 0;
}
int num1;
int num2;
};
class plus1 : public calculator
{
public:
int result()
{
return num1 + num2;
}
};
class sub : public calculator
{
public:
int result()
{
return num1 - num2;
}
};
class cal : public calculator
{
public:
int result()
{
return num1 * num2;
}
};
void testdt()
{
plus1 *plu = new plus1;
plu->num1 = 10;
plu->num2 = 20;
cout << plu->num1 << "+" << plu->num2 << "=" << plu->result() << endl;
delete plu;
sub *su = new sub;
su->num1 = 30;
su->num2 = 40;
cout << su->num1 << "-" << su->num2 << "=" << su->result() << endl;
delete su;
cal *ca = new cal;
ca->num1 = 50;
ca->num2 = 50;
cout << ca->num1 << "*" << ca->num2 << "=" << ca->result() << endl;
delete ca;
}
int main()
{
testdt();
system("pause");
return 0;
}
總結
C++開發提倡利用多態設計程序架構,因爲多態優點很多
開發程序原則:開閉
開放擴展,閉合修改
3 純虛函數和抽象類
在多態中,通常父類中虛函數的實現是毫無意義的,主要都是調用子類重寫的內容
因此可以將虛函數改爲純虛函數
純虛函數語法:virtual 返回值類型 函數名 (參數列表)= 0 ;
當類中有了純虛函數,這個類也稱爲抽象類
抽象類特點:
- 無法實例化對象
- 子類必須重寫抽象類中的純虛函數,否則也屬於抽象類
#include <iostream>
using namespace std;
class base
{
public:
virtual void func() = 0;
};
class son : public base
{
public:
virtual void func()
{
cout << "子函數func調用" << endl;
}
};
void test06091()
{
son * s = NULL;
//s = new base; base爲抽象類,無法實例化對象
s = new son;
s->func();
delete s;
}
int main()
{
test06091();
system("pause");
return 0;
}
運行結果
4 多態製作飲品
案例描述:
製作飲品的大致流程爲:煮水 - 沖泡 - 倒入杯中 - 加入輔料
利用多態技術實現本案例,提供抽象製作飲品基類,提供子類製作咖啡和茶葉
#include <iostream>
using namespace std;
class makedrink
{
public:
virtual void boil() = 0;
virtual void brew() = 0;
virtual void incup() = 0;
virtual void putsomething() = 0;
void dodringk()
{
boil();
brew();
incup();
putsomething();
}
};
class coffee : public makedrink
{
public:
virtual void boil()
{
cout << "燒農夫山泉" << endl;
}
virtual void brew()
{
cout << "沖泡咖啡" << endl;
}
virtual void incup()
{
cout << "倒入紙杯" << endl;
}
virtual void putsomething()
{
cout << "加糖!" << endl;
}
};
class tea : public makedrink
{
public:
virtual void boil()
{
cout << "燒礦泉水" << endl;
}
virtual void brew()
{
cout << "沖泡茶葉" << endl;
}
virtual void incup()
{
cout << "倒入玻璃杯" << endl;
}
virtual void putsomething()
{
cout << "加枸杞!" << endl;
}
};
//方式一
//void test06092()
//{
// coffee * cof = NULL;
// cof = new coffee;
// cof->boil();
// cof->brew();
// cof->incup();
// cof->putsomething();
// delete cof;
//
// tea * te = new tea;
// te->boil();
// te->brew();
// te->incup();
// te->putsomething();
// delete te;
//}
void dowork(makedrink * drink)
{
drink->dodringk();
delete drink;
}
void test06093()
{
dowork(new coffee);
cout << "---------------" << endl;
dowork(new tea);
}
int main()
{
//test06092();
test06093();
system("pause");
return 0;
}
運行結果
5 虛析構和純虛析構
多態使用時,如果子類中有屬性開闢到堆區,那麼父類指針在釋放時無法調用到子類的析構代碼
解決方式:將父類中的析構函數改爲虛析構或者純虛析構
虛析構和純虛析構共性:
- 可以解決父類指針釋放子類對象
- 都需要有具體的函數實現
虛析構和純虛析構區別:
- 如果是純虛析構,該類屬於抽象類,無法實例化對象
虛析構語法:
virtual ~類名(){}
純虛析構語法:
virtual ~類名() = 0;
類名::~類名(){}
#include <iostream>
#include <string>
using namespace std;
class animal
{
public :
animal()
{
cout << "animal構造函數調用" << endl;
}
virtual void speak() = 0;
virtual ~animal() = 0;
//virtual ~animal()
//{
// cout << "animal虛析構函數調用" << endl;
//}
};
animal::~animal()
{
cout << "animal純析構函數" << endl;
}
//和包含普通純虛函數的類一樣,包含了純虛析構函數的類也是一個抽象類。不能夠被實例化。
class cat : public animal
{
public:
cat(string name)
{
cout << "cat構造函數調用" << endl;
cat_name = new string(name);
}
virtual void speak()
{
cout << *cat_name << "小貓在說話" << endl;
}
~cat()
{
cout << "cat析構函數" << endl;
if (this->cat_name != NULL)
{
delete cat_name;
cat_name = NULL;
}
}
string *cat_name;
};
void test06094()
{
animal * ani = new cat("tom");
ani->speak();
delete ani;
}
int main()
{
test06094();
system("pause");
return 0;
}
運行結果
6 電腦組裝
案例描述:
電腦主要組成部件爲 CPU(用於計算),顯卡(用於顯示),內存條(用於存儲)
將每個零件封裝出抽象基類,並且提供不同的廠商生產不同的零件,例如Intel廠商和Lenovo廠商創建電腦類提供讓電腦工作的函數,並且調用每個零件工作的接口
測試時組裝三臺不同的電腦進行工作
#include <iostream>
using namespace std;
class cpubase
{
public:
virtual void address() = 0;
};
class nvidiabase
{
public:
virtual void address() = 0;
};
class memorybase
{
public:
virtual void address() = 0;
};
class computerbase
{
public :
computerbase(cpubase * cpubas, nvidiabase * nvidiabas, memorybase * memorybas)
{
m_cpu = cpubas;
m_nvidia = nvidiabas;
m_memory = memorybas;
}
void work()
{
m_cpu->address();
m_nvidia->address();
m_memory->address();
}
~computerbase()
{
if (m_cpu != NULL)
{
delete m_cpu;
m_cpu = NULL;
}
if (m_nvidia != NULL)
{
delete m_nvidia;
m_nvidia = NULL;
}
if (m_memory != NULL)
{
delete m_memory;
m_memory = NULL;
}
}
cpubase * m_cpu;
nvidiabase * m_nvidia;
memorybase * m_memory;
};
class intelcpu : public cpubase
{
public:
virtual void address()
{
cout << "CPU: intelcpu" << endl;
}
};
class chinacpu : public cpubase
{
public:
virtual void address()
{
cout << "CPU: chinacpu" << endl;
}
};
class amdshow : public nvidiabase
{
public:
virtual void address()
{
cout << "Display:AMDshow" << endl;
}
};
class nvidiashow : public nvidiabase
{
public:
virtual void address()
{
cout << "Display:NVIDIAshow" << endl;
}
};
class caihong7 : public memorybase
{
public:
virtual void address()
{
cout << "Memory:caihong7" << endl;
}
};
class shumaxibu : public memorybase
{
public:
virtual void address()
{
cout << "Memory:shumaxibu" << endl;
}
};
void test060969()
{
//第一臺電腦零件
cpubase * cpuba = new intelcpu;
nvidiabase * nvidiaba = new amdshow;
memorybase * memoryba = new caihong7;
//第一臺電腦
cout << "computer1 start:" << endl;
computerbase * computer1 = new computerbase(cpuba, nvidiaba, memoryba);
computer1->work();
delete computer1;
//第二胎
cout << "computer2 start:" << endl;
computerbase * computer2 = new computerbase(new chinacpu, new nvidiashow, new shumaxibu);
computer2->work();
delete computer2;
//第二胎
cout << "computer3 start:" << endl;
computerbase * computer3 = new computerbase(new intelcpu, new nvidiashow, new caihong7);
computer3->work();
delete computer3;
//第二胎
cout << "computer4 start:" << endl;
computerbase * computer4 = new computerbase(new intelcpu, new amdshow, new caihong7);
computer4->work();
delete computer4;
}
int main()
{
test060969();
system("pause");
return 0;
}