C++實(shí)現(xiàn)動(dòng)態(tài)綁定代碼分享
C++實(shí)現(xiàn)動(dòng)態(tài)綁定代碼分享
#include <iostream>
#include<string>
using namespace std;
class BookItem
{
private:
string bookName;
size_t cnt;
public:
BookItem(const string&s,size_t c,double p):
bookName(s),cnt(c),price(p)
{}
~BookItem(){}
protected:
double price;
public:
double bookPrice()
{
return this->price;
}
string getBookName()
{
return this->bookName;
}
size_t getBookCount()
{
return this->cnt;
}
virtual double money()
{
return cnt*price;
}
virtual void costMoney()
{
cout<<money()<<endl;
}
};
class BookBatchItem:public BookItem
{
private:
string bookName;
size_t cnt;
public:
BookBatchItem(const string&s,size_t c,double p,double discountRate):
BookItem(s,c,p),cnt(c),discount(discountRate)
{}
~BookBatchItem(){}
private:
double discount;
public:
double money()
{
if(cnt>=10)
return cnt*price*(1.0-discount);
else
return cnt*price;
}
void costMoney()
{
cout<<money()<<endl;
// cout<<cnt<<endl;
// cout<<price<<endl;
// cout<<discount<<endl;
// cout<<"..."<<endl;
}
};
int main()
{
BookItem b1("Uncle Tom's house",11,12.5);
b1.costMoney();
BookBatchItem b2("Gone with wind",11,12.5,0.12);
b2.costMoney();
BookItem* pb=&b1;
pb->costMoney();
pb=&b2;
pb->costMoney();
return 0;
}
只有采用“指針->函數(shù)()”或“引用.函數(shù)()”的方式調(diào)用C++類中的虛函數(shù)才會(huì)執(zhí)行動(dòng)態(tài)綁定,非虛函數(shù)并不具備動(dòng)態(tài)綁定的特征,不管采用任何方式調(diào)用都不行。
下面代碼中,一個(gè)java或者C#的程序員容易犯的一個(gè)錯(cuò)誤。
class Base
{
public:
Base() { p = new char ; }
~Base() { delete p; }
private:
char * p ;
};
class Derived:public Base
{
public:
Derived() { d = new char[10] ; }
~Derived() { delete[] d; }
private:
char * d ;
};
int main()
{
Base *pA = new Derived();
delete pA ;
Derived *pA = new Derived();
delete pA ;
}
代碼中:
執(zhí)行delete pA時(shí),直接執(zhí)行~Base析構(gòu)函數(shù),不會(huì)執(zhí)行~Derived析構(gòu)函數(shù)的,原因在于析構(gòu)函數(shù)不是虛函數(shù)。
執(zhí)行delete pB時(shí),先執(zhí)行~Derived()然后再執(zhí)行~Base()。
相比之下,java和C#中,所有的函數(shù)調(diào)用都是動(dòng)態(tài)綁定的。
關(guān)于C++的成員函數(shù)調(diào)用與綁定方式,可以通過(guò)下面的代碼測(cè)試:
class Base
{
public:
virtual void Func() { cout<<"Base"<<endl; }
};
class Derived:public Base
{
public:
virtual void Func() { cout<<"Derived"<<endl; }
};
int main()
{
Derived obj;
Base * p1 = &obj;
Base & p2 = obj;
Base obj2 ;
obj.Func() ; //靜態(tài)綁定,Derived的func
p1->Func(); //動(dòng)態(tài)綁定,Derived的func
(*p1).Func(); //動(dòng)態(tài)綁定,Derived的func
p2.Func(); //動(dòng)態(tài)綁定,Derived的func
obj2.Func(); //靜態(tài)綁定,Base的func
return 0 ;
}
可以看出“對(duì)象名.函數(shù)()”屬于靜態(tài)綁定,當(dāng)然,使用指針轉(zhuǎn)換為對(duì)象的方式應(yīng)該屬于指針調(diào)用那一類了,至于“類名::函數(shù)()”毫無(wú)疑問(wèn)屬于靜態(tài)綁定。
- C++ 多態(tài)性虛函數(shù)和動(dòng)態(tài)綁定學(xué)習(xí)筆記
- 關(guān)于C++虛函數(shù)與靜態(tài)、動(dòng)態(tài)綁定的問(wèn)題
- 深入理解C++的動(dòng)態(tài)綁定與靜態(tài)綁定的應(yīng)用詳解
- 詳解C++動(dòng)態(tài)內(nèi)存管理
- C++內(nèi)存管理面經(jīng)
- 詳解C++中動(dòng)態(tài)內(nèi)存管理和泛型編程
- C++圖文并茂分析講解內(nèi)存管理
- 一文詳解C++中動(dòng)態(tài)內(nèi)存管理
- C語(yǔ)言與C++中內(nèi)存管理詳解
- 一起來(lái)學(xué)習(xí)C++的動(dòng)態(tài)內(nèi)存管理
- C++中動(dòng)態(tài)綁定和內(nèi)存管理的實(shí)現(xiàn)
相關(guān)文章
基于C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了基于C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
C++中Overload,Override,Hide之間的區(qū)別
重載overload,這個(gè)概念是大家熟知的。在同一可訪問(wèn)區(qū)內(nèi)被聲名的幾個(gè)具有不同參數(shù)列的(參數(shù)的類型、個(gè)數(shù)、順序不同)同名函數(shù),程序會(huì)根據(jù)不同的參數(shù)列來(lái)確定具體調(diào)用哪個(gè)函數(shù),這種機(jī)制就是重載2013-09-09
C語(yǔ)言中形參和實(shí)參詳解及實(shí)例代碼
這篇文章主要介紹了C語(yǔ)言中形參和實(shí)參詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
Visual Studio2019調(diào)試DLL的實(shí)現(xiàn)
本文主要介紹了Visual Studio2019調(diào)試DLL的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
C++實(shí)現(xiàn)簡(jiǎn)單FTP客戶端軟件開發(fā)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單FTP客戶端軟件開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

