A combination of so-called classes refers to the fact that member data in a class is an object that has a class or a pointer or reference to a class. The combination of classes enables more complex abstractions based on existing abstractions. Like what:
1, by value combination
#include <iostream.h> #include <math.h>class point{public:point (int xx,int yy)//constructor {x=xx;y=yy;cout< < "Point ' s constructor is called" <<ENDL;} Point &p;//copy constructor int GetX () {return x;int GetY () {return y;} ~point () {cout<< "Point's destructor was called" <<ENDL;} Private:int x, y;}; Point::P oint (Point &p) {x=p.x;y=p.y;cout<< "Point's copyconstructor was called" <<ENDL; Class Distance{private:point p1,p2; By value combination, the object of class point is declared as a data member of Class Distance double dist;public:distance (point a,point B);//includes the point class double Getdis (void) { return dist;} ~distance () {cout<< "Distance ' s destructor was called" <<endl;}};D Istance::D istance (point a,point B):p 1 (a), p2 (b) {double x=double (P1. GetX ()-p2. GetX ());d ouble y=double (p1. GetY ()-p2. GetY ());d ist=sqrt (x*x+y*y); cout<< "Distance ' s constructor was called" <<ENDL<<ENDL;} void Main () {point myp1, myp2 (4,5);D istance myd (myp1,myp2);cout<< ' \ n ' << "The Distance is:" <<myd . Getdis () <<enDl<<endl;} 2, by reference combination
Class zooanimal{public://private:endangered *_endangered1;//By pointer combination endangered &_endangered2;//by reference combination};
See another example:
Suppose the bird is capable of flying, then is the ostrich a bird?" How do ostriches inherit birds? [2005 Test of a famous American analysis software company]
analysis: If all the birds can fly, the ostrich is not a bird! When answering such questions, don't trust your intuition! It will take some time to connect intuition with proper inheritance.
According to the problem can be learned: birds can fly. In other words, when a bird flies, its height is greater than 0. Ostrich is a kind of bird (biology). But it has a flight height of 0 (ostriches can't fly).
do not confuse alternatives with subsets. Even if the ostrich set is a subset of the bird set (each ostrich set is within the bird set), it does not mean that the ostrich's behavior can replace the bird's behavior. Substitution is related to behavior, and is not related to subsets.
answer: Assume that the ostrich to inherit birds, can take the combination of methods, The birds can be the function of the ostrich to pick out, so that the ostrich is not "a kind of" bird, but "has some kind of" bird properties. Code such as the following:
#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; The combination is used here, and is combined by value: Declare an object of bird as a data member of another class};int main () {ostrich xiaoq;xiaoq.eat (); Xiaoq.sleep (); return 0;}
Combination of the C + + base class