c++类

来源:互联网 发布:甲骨文中国官网 mysql 编辑:程序博客网 时间:2024/06/08 10:24

转自:http://blog.csdn.net/suool/article/details/41344025

所谓类的组合是指:类中的成员数据是另一个类的对象或者是另一个类的指针或引用。通过类的组合可以在已有的抽象的基础上实现更复杂的抽象。 例如:

1、按值组合

[cpp] view plain copy
 print?
  1. #include<iostream.h>  
  2. #include<math.h>  
  3. class Point  
  4. {  
  5. public:  
  6.     Point(int xx,int yy)//构造函数  
  7.     {  
  8.         x=xx;  
  9.         y=yy;  
  10.         cout<<"Point's constructor was called"<<endl;  
  11.     }  
  12.     Point(Point &p);//拷贝构造函数  
  13.     int GetX(){return x;  
  14.     int GetY(){return y;}  
  15.     ~Point()  
  16.     {  
  17.         cout<<"Point's destructor was called"<<endl;  
  18.     }  
  19. private:  
  20.     int x,y;  
  21.     };  
  22.     Point::Point(Point &p)  
  23.     {  
  24.         x=p.x;  
  25.         y=p.y;  
  26.         cout<<"Point's copyConstructor was called"<<endl;  
  27.     }  
  28.     class Distance  
  29.     {  
  30.     private:  
  31.         Point p1,p2;  //按值组合,将类Point的对象声明为类Distance的数据成员  
  32.         double dist;  
  33.     public:  
  34.         Distance(Point a,Point b);//包含Point类  
  35.         double GetDis(void)  
  36.         {  
  37.             return dist;  
  38.         }   
  39.         ~Distance()  
  40.         {  
  41.             cout<<"Distance's destructor was called"<<endl;  
  42.         }  
  43.     };  
  44.     Distance::Distance(Point a,Point b):p1(a),p2(b)  
  45.     {  
  46.         double x=double(p1.GetX()-p2.GetX());  
  47.         double y=double(p1.GetY()-p2.GetY());  
  48.         dist=sqrt(x*x+y*y);   
  49.         cout<<"Distance's constructor was called"<<endl<<endl;  
  50.     }  
  51.     void main()  
  52.     {  
  53.         Point myp1(1,1),myp2(4,5);  
  54.         Distance myd(myp1,myp2);  
  55.         cout<<'\n'<<"the distance is: "<<myd.GetDis()<<endl<<endl;  
  56.     }  
2、按引用组合

[cpp] view plain copy
 print?
  1. class ZooAnimal  
  2. {  
  3. public:  
  4.     // ....  
  5. private:  
  6.     Endangered *_endangered1 ; //按指针组合  
  7.     Endangered &_endangered2 ; //按引用组合  
  8. };  

另外再看一个例子:

    如果鸟是可以飞的,那么鸵鸟是鸟么?鸵鸟如何继承鸟类?[美国某著名分析软件公司2005年面试题]
解析:如果所有鸟都能飞,那鸵鸟就不是鸟!回答这种问题时,不要相信自己的直觉!将直觉和合适的继承联系起来还需要一段时间。
    根据题干可以得知:鸟是可以飞的。也就是说,当鸟飞行时,它的高度是大于0的。鸵鸟是鸟类(生物学上)的一种。但它的飞行高度为0(鸵鸟不能飞)。
    不要把可替代性和子集相混淆。即使鸵鸟集是鸟集的一个子集(每个驼鸟集都在鸟集内),但并不意味着鸵鸟的行为能够代替鸟的行为。可替代性与行为有关,与子集没有关系。当评价一个潜在的继承关系时,重要的因素是可替代的行为,而不是子集。
      答案:如果一定要让鸵鸟来继承鸟类,可以采取组合的办法,把鸟类中的可以被鸵鸟继承的函数挑选出来,这样鸵鸟就不是“a kind of”鸟了,而是“has some kind of”鸟的属性而已。代码如下:

[cpp] view plain copy
 print?
  1. #include<string>  
  2. #include<iostream>  
  3. using namespace std;  
  4. class bird  
  5. {  
  6. public:  
  7.     void eat()  
  8.     {  
  9.         cout<<"bird is eating"<<endl;  
  10.     }  
  11.     void sleep()  
  12.     {  
  13.         cout<<"bird is sleeping"<<endl;  
  14.     }  
  15.     void fly();  
  16. };  
  17.   
  18. class ostrich  
  19. {  
  20. public:  
  21.     eat()  
  22.     {  
  23.         smallBird.eat();  
  24.     }  
  25.     sleep()  
  26.     {  
  27.         smallBird.sleep();  
  28.     }  
  29. private:  
  30.     bird smallBird;  //在这里使用了组合,且是按值组合:将bird的一个对象声明为另一类的数据成员  
  31. };  
  32.   
  33. int main()  
  34. {  
  35.     ostrich xiaoq;  
  36.     xiaoq.eat();  
  37.     xiaoq.sleep();  
  38.     return 0;  
  39. }  

原创粉丝点击