A combination of so-called classes refers to the fact that member data in a class is an object of another class or a pointer or reference to another class. The combination of classes allows for a more complex abstraction based on an existing abstraction. For example:
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);//contains 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 also an example:
If the bird can fly, then the ostrich is 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 this question, don't trust your intuition! It will take some time to connect intuition with appropriate 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: If you must let the ostrich to inherit birds, you can take a combination of methods, The birds can be the function of the ostrich to choose out, so that the ostrich is not "a kind of" bird, but "has some kind of" bird properties. The code is as follows:
#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: declaring 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