在過去的學習中,我們始終接觸的單個類的繼續,但是在現實生活中,一些新事物往往會擁有兩個或者兩個以上事物的屬性,爲了解決這個問題,C++引入了多重繼續的概念,C++答應爲一個派生類指定多個基類,這樣的繼續結構被稱做多重繼續。 舉個例子,交通工具類可以派生出汽車和船連個子類,但擁有汽車和船共同特性水陸兩用汽車就必須繼續來自汽車類與船類的共同屬性。
由此我們不難想出如下的圖例與代碼:
當一個派生類要使用多重繼續的時候,必須在派生類名和冒號之後列出所有基類的類名,並用逗好分隔。//程序作者:管甯
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle(int weight = 0)
{
Vehicle::weight = weight;
}
void SetWeight(int weight)
{
cout<<"重新設置重量"<<endl;
Vehicle::weight = weight;
}
virtual void ShowMe() = 0;
PRotected:
int weight;
};
class Car:public Vehicle//汽車
{
public:
Car(int weight=0,int aird=0):Vehicle(weight)
{
Car::aird = aird;
}
void ShowMe()
{
cout<<"我是汽車!"<<endl;
}
protected:
int aird;
};
class Boat:public Vehicle//船
{
public:
Boat(int weight=0,float tonnage=0):Vehicle(weight)
{
Boat::tonnage = tonnage;
}
void ShowMe()
{
cout<<"我是船!"<<endl;
}
protected:
float tonnage;
};
class AmphibianCar:public Car,public Boat//水陸兩用汽車,多重繼續的體現
{
public:
AmphibianCar(int weight,int aird,float tonnage)
:Vehicle(weight),Car(weight,aird),Boat(weight,tonnage)
//多重繼續要注重調用基類構造函數
{
}
void ShowMe()
{
cout<<"我是水陸兩用汽車!"<<endl;
}
};
int main()
{
AmphibianCar a(4,200,1.35f);//錯誤
a.SetWeight(3);//錯誤
system("pause");
}上面的代碼從表面看,看不出有明顯的語發錯誤,但是它是不能夠通過編譯的。這有是爲什麽呢?
這是由于多重繼續帶來的繼續的模糊性帶來的問題。
更多內容請看C/C++技術專題專題,或
先看如下的圖示:
在圖中深紅色標記出來的地方正是主要問題所在,水陸兩用汽車類繼續了來自Car類與Boat類的屬性與方法,Car類與Boat類同爲AmphibianCar類的基類,在內存分配上AmphibianCar獲得了來自兩個類的SetWeight()成員函數,當我們調用a.SetWeight(3)的時候計算機不知道如何選擇分別屬于兩個基類的被重複擁有了的類成員函數SetWeight()。 由于這種模糊問題的存在同樣也導致了AmphibianCar a(4,200,1.35f);執行失敗,系統會産生Vehicle」不是基或成員的錯誤。以上面的代碼爲例,我們要想讓AmphibianCar類既獲得一個Vehicle的拷貝,而且又同時共享用Car類與Boat類的數據成員與成員函數就必須通過C++所提供的虛擬繼續技術來實現。我們在Car類和Boat類繼續Vehicle類出,在前面加上virtual要害字就可以實現虛擬繼續,使用虛擬繼續後,當系統碰到多重繼續的時候就會自動先加入一個Vehicle的拷貝,當再次請求一個Vehicle的拷貝的時候就會被忽略,保證繼續類成員函數的唯一性。
修改後的代碼如下,注重觀察變化://程序作者:管甯
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle(int weight = 0)
{
Vehicle::weight = weight;
cout<<"載入Vehicle類構造函數"<<endl;
}
void SetWeight(int weight)
{
cout<<"重新設置重量"<<endl;
Vehicle::weight = weight;
}
virtual void ShowMe() = 0;
protected:
int weight;
};
class Car:virtual public Vehicle//汽車,這裏是虛擬繼續
{
public:
Car(int weight=0,int aird=0):Vehicle(weight)
{
Car::aird = aird;
cout<<"載入Car類構造函數"<<endl;
}
void ShowMe()
{
cout<<"我是汽車!"<<endl;
}
protected:
int aird;
};
class Boat:virtual public Vehicle//船,這裏是虛擬繼續
{
public:
Boat(int weight=0,float tonnage=0):Vehicle(weight)
{
Boat::tonnage = tonnage;
cout<<"載入Boat類構造函數"<<endl;
}
void ShowMe()
{
cout<<"我是船!"<<endl;
}
protected:
float tonnage;
};
class AmphibianCar:public Car,public Boat//水陸兩用汽車,多重繼續的體現
{
public:
AmphibianCar(int weight,int aird,float tonnage)
:Vehicle(weight),Car(weight,aird),Boat(weight,tonnage)
//多重繼續要注重調用基類構造函數
{
cout<<"載入AmphibianCar類構造函數"<<endl;
}
void ShowMe()
{
cout<<"我是水陸兩用汽車!"<<endl;
}
void ShowMembers()
{
cout<<"重量:"<<weight<<"頓,"<<"空氣排量:"<<aird<<"CC,"<<"排水量:"<<tonnage<<"頓"<<endl;
}
};
int main()
{
AmphibianCar a(4,200,1.35f);
a.ShowMe();
a.ShowMembers();
a.SetWeight(3);
a.ShowMembers();
system("pause");
}注重觀察類構造函數的構造順序。
雖然說虛擬繼續與虛函數有一定相似的地方,但讀者務必要記住,他們之間是絕對沒有任何聯系的! 更多內容請看C/C++技術專題專題,或
在過去的學習中,我們始終接觸的單個類的繼續,但是在現實生活中,一些新事物往往會擁有兩個或者兩個以上事物的屬性,爲了解決這個問題,C++引入了多重繼續的概念,C++答應爲一個派生類指定多個基類,這樣的繼續結構被稱做多重繼續。 舉個例子,交通工具類可以派生出汽車和船連個子類,但擁有汽車和船共同特性水陸兩用汽車就必須繼續來自汽車類與船類的共同屬性。
由此我們不難想出如下的圖例與代碼:[url=/bbs/detail_1785261.html][img]http://image.wangchao.net.cn/it/1323424097697.gif[/img][/url]
當一個派生類要使用多重繼續的時候,必須在派生類名和冒號之後列出所有基類的類名,並用逗好分隔。//程序作者:管甯
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle(int weight = 0)
{
Vehicle::weight = weight;
}
void SetWeight(int weight)
{
cout<<"重新設置重量"<<endl;
Vehicle::weight = weight;
}
virtual void ShowMe() = 0;
PRotected:
int weight;
};
class Car:public Vehicle//汽車
{
public:
Car(int weight=0,int aird=0):Vehicle(weight)
{
Car::aird = aird;
}
void ShowMe()
{
cout<<"我是汽車!"<<endl;
}
protected:
int aird;
};
class Boat:public Vehicle//船
{
public:
Boat(int weight=0,float tonnage=0):Vehicle(weight)
{
Boat::tonnage = tonnage;
}
void ShowMe()
{
cout<<"我是船!"<<endl;
}
protected:
float tonnage;
};
class AmphibianCar:public Car,public Boat//水陸兩用汽車,多重繼續的體現
{
public:
AmphibianCar(int weight,int aird,float tonnage)
:Vehicle(weight),Car(weight,aird),Boat(weight,tonnage)
//多重繼續要注重調用基類構造函數
{
}
void ShowMe()
{
cout<<"我是水陸兩用汽車!"<<endl;
}
};
int main()
{
AmphibianCar a(4,200,1.35f);//錯誤
a.SetWeight(3);//錯誤
system("pause");
} 上面的代碼從表面看,看不出有明顯的語發錯誤,但是它是不能夠通過編譯的。這有是爲什麽呢?
這是由于多重繼續帶來的繼續的模糊性帶來的問題。
[url=/bbs/detail_1785261.html][img]http://image.wangchao.net.cn/it/1323424097763.gif[/img][/url] 更多內容請看C/C++技術專題專題,或
先看如下的圖示:[url=/bbs/detail_1785261.html][img]http://image.wangchao.net.cn/it/1323424097775.gif[/img][/url]
在圖中深紅色標記出來的地方正是主要問題所在,水陸兩用汽車類繼續了來自Car類與Boat類的屬性與方法,Car類與Boat類同爲AmphibianCar類的基類,在內存分配上AmphibianCar獲得了來自兩個類的SetWeight()成員函數,當我們調用a.SetWeight(3)的時候計算機不知道如何選擇分別屬于兩個基類的被重複擁有了的類成員函數SetWeight()。 由于這種模糊問題的存在同樣也導致了AmphibianCar a(4,200,1.35f);執行失敗,系統會産生Vehicle」不是基或成員的錯誤。 以上面的代碼爲例,我們要想讓AmphibianCar類既獲得一個Vehicle的拷貝,而且又同時共享用Car類與Boat類的數據成員與成員函數就必須通過C++所提供的虛擬繼續技術來實現。 我們在Car類和Boat類繼續Vehicle類出,在前面加上virtual要害字就可以實現虛擬繼續,使用虛擬繼續後,當系統碰到多重繼續的時候就會自動先加入一個Vehicle的拷貝,當再次請求一個Vehicle的拷貝的時候就會被忽略,保證繼續類成員函數的唯一性。
修改後的代碼如下,注重觀察變化://程序作者:管甯
//站點:www.cndev-lab.com
//所有稿件均有版權,如要轉載,請務必聞名出處和作者
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle(int weight = 0)
{
Vehicle::weight = weight;
cout<<"載入Vehicle類構造函數"<<endl;
}
void SetWeight(int weight)
{
cout<<"重新設置重量"<<endl;
Vehicle::weight = weight;
}
virtual void ShowMe() = 0;
protected:
int weight;
};
class Car:virtual public Vehicle//汽車,這裏是虛擬繼續
{
public:
Car(int weight=0,int aird=0):Vehicle(weight)
{
Car::aird = aird;
cout<<"載入Car類構造函數"<<endl;
}
void ShowMe()
{
cout<<"我是汽車!"<<endl;
}
protected:
int aird;
};
class Boat:virtual public Vehicle//船,這裏是虛擬繼續
{
public:
Boat(int weight=0,float tonnage=0):Vehicle(weight)
{
Boat::tonnage = tonnage;
cout<<"載入Boat類構造函數"<<endl;
}
void ShowMe()
{
cout<<"我是船!"<<endl;
}
protected:
float tonnage;
};
class AmphibianCar:public Car,public Boat//水陸兩用汽車,多重繼續的體現
{
public:
AmphibianCar(int weight,int aird,float tonnage)
:Vehicle(weight),Car(weight,aird),Boat(weight,tonnage)
//多重繼續要注重調用基類構造函數
{
cout<<"載入AmphibianCar類構造函數"<<endl;
}
void ShowMe()
{
cout<<"我是水陸兩用汽車!"<<endl;
}
void ShowMembers()
{
cout<<"重量:"<<weight<<"頓,"<<"空氣排量:"<<aird<<"CC,"<<"排水量:"<<tonnage<<"頓"<<endl;
}
};
int main()
{
AmphibianCar a(4,200,1.35f);
a.ShowMe();
a.ShowMembers();
a.SetWeight(3);
a.ShowMembers();
system("pause");
} 注重觀察類構造函數的構造順序。
雖然說虛擬繼續與虛函數有一定相似的地方,但讀者務必要記住,他們之間是絕對沒有任何聯系的! [url=/bbs/detail_1785261.html][img]http://image.wangchao.net.cn/it/1323424097956.gif[/img][/url] 更多內容請看C/C++技術專題專題,或