【C++基礎】類的組合
阿新 • • 發佈:2019-01-24
所謂類的組合是指:類中的成員資料是另一個類的物件或者是另一個類的指標或引用。通過類的組合可以在已有的抽象的基礎上實現更復雜的抽象。 例如:
1、按值組合
2、按引用組合#include<iostream.h> #include<math.h> class Point { public: Point(int xx,int yy)//建構函式 { x=xx; y=yy; cout<<"Point's constructor was called"<<endl; } Point(Point &p);//拷貝建構函式 int GetX(){return x; int GetY(){return y;} ~Point() { cout<<"Point's destructor was called"<<endl; } private: int x,y; }; Point::Point(Point &p) { x=p.x; y=p.y; cout<<"Point's copyConstructor was called"<<endl; } class Distance { private: Point p1,p2; //按值組合,將類Point的物件宣告為類Distance的資料成員 double dist; public: Distance(Point a,Point b);//包含Point類 double GetDis(void) { return dist; } ~Distance() { cout<<"Distance's destructor was called"<<endl; } }; Distance::Distance(Point a,Point b):p1(a),p2(b) { double x=double(p1.GetX()-p2.GetX()); double y=double(p1.GetY()-p2.GetY()); dist=sqrt(x*x+y*y); cout<<"Distance's constructor was called"<<endl<<endl; } void main() { Point myp1(1,1),myp2(4,5); Distance myd(myp1,myp2); cout<<'\n'<<"the distance is: "<<myd.GetDis()<<endl<<endl; }
class ZooAnimal
{
public:
// ....
private:
Endangered *_endangered1 ; //按指標組合
Endangered &_endangered2 ; //按引用組合
};
另外再看一個例子:
如果鳥是可以飛的,那麼鴕鳥是鳥麼?鴕鳥如何繼承鳥類?[美國某著名分析軟體公司2005年面試題]
解析:如果所有鳥都能飛,那鴕鳥就不是鳥!回答這種問題時,不要相信自己的直覺!將直覺和合適的繼承聯絡起來還需要一段時間。
根據題幹可以得知:鳥是可以飛的。也就是說,當鳥飛行時,它的高度是大於0的。鴕鳥是鳥類(生物學上)的一種。但它的飛行高度為0(鴕鳥不能飛)。
不要把可替代性和子集相混淆。即使鴕鳥集是鳥集的一個子集(每個駝鳥集都在鳥集內),但並不意味著鴕鳥的行為能夠代替鳥的行為。可替代性與行為有關,與子集沒有關係。當評價一個潛在的繼承關係時,重要的因素是可替代的行為,而不是子集。
答案:如果一定要讓鴕鳥來繼承鳥類,可以採取組合的辦法,把鳥類中的可以被鴕鳥繼承的函式挑選出來,這樣鴕鳥就不是“a kind of”鳥了,而是“has some kind of”鳥的屬性而已。程式碼如下:
#include<string> #include<iostream> using namespace std; class bird { public: void eat() { cout<<"bird is eating"<<endl; } void sleep() { cout<<"bird is sleeping"<<endl; } void fly(); }; class ostrich { public: eat() { smallBird.eat(); } sleep() { smallBird.sleep(); } private: bird smallBird; //在這裡使用了組合,且是按值組合:將bird的一個物件宣告為另一類的資料成員 }; int main() { ostrich xiaoq; xiaoq.eat(); xiaoq.sleep(); return 0; }