1. In Class A The object of Class B is a member variable, called a combination of classes (composition).
2. When combined, the class B is typically initialized in the constructor initialization list of Class A (a constructor of Class B is called).
If Class B is not initialized, a constructor that can pass arguments in class B is called.
#include <iostream>using namespacestd; #include<string>//--------------------------------------classa{ Public: intM_ax; intM_ay; A (); A (intValintvy);}; A::a () {}a::a (intVxintvy) { This->m_ax =VX; This->m_ay =vy;}//--------------------------------------classb{ Public: intM_BX; B (); B (intval);}; B::b () {}b::b (intval) { This->M_BX =Val;}//--------------------------------------classc{ Public: A ac; B BC; C (); C (intVaxintVayintVBX); C (intVAX,intVay); C (intVBX); voidshow ();}; C::c () {}c::c (intVAX,intVayintvbx): AC (Vax,vay), BC (VBX) {}c::c (intVAX,intvay): AC (Vax,vay) {}
C::c (intvbx): BC (VBX)//non-Initialized internal class object, can not be written out, can also be written outac (). c::c (int vbx): BC (VBX), ac ()
{}voidc::show () {cout<<"Ac.m_ax ="<<ac.m_ax<<Endl; cout<<"Ac.m_ay ="<<ac.m_ay<<Endl; cout<<"BC.M_BX ="<<bc.m_bx<<Endl;}//--------------------------------------intMain () {C C1 (1,2,3 ); C1.show (); while(1); return 0 ;}//Ac.m_ax = 1//Ac.m_ay = 2//BC.M_BX = 3
C + +, a combination of classes