/////////////////////////////////////////////////////////////// // DEM_AGG1.CPP -- demonstrates aggregation, // // compiler generated copy constructor and // // compiler generated assignment operator // /////////////////////////////////////////////////////////////// #include class base { public: base(void); base(base& b); base(int x); virtual ~base(void); base& operator= (base& b); friend ostream& operator<<(ostream& ostrm, base& b); private: int data; }; class aggr { public: aggr(void); aggr(int x1, int x2); // no copy constructor defined yet // no operator=() defined yet // no destructor defined private: base b1; // can't initialize as shown below base b2; // base b2(5); compile error // base b2 = 5; " " }; // //----< void constructor >------------------------------------- base::base(void) { cout << " base void constructor called\n"; } //----< copy constructor >------------------------------------- base::base(base& b) : data(b.data) { // note initialization cout << " base copy constructor called\n"; } //----< promotion constructor >-------------------------------- base::base(int x) : data(x) { // note initialization cout << " base(int) constructor called\n"; cout << " set data = " << data << "\n"; } //----< destructor >------------------------------------------- base::~base(void) { cout << " base destructor called\n"; } //----< assignment >------------------------------------------- base& base::operator= (base& b) { cout << " base operator= called\n"; data = b.data; return *this; } //----< insertion >-------------------------------------------- ostream& operator<<(ostream& ostrm, base& b) { ostrm << b.data; return ostrm; } //----< void constructor >------------------------------------- aggr::aggr(void) { cout << " aggr void constructor called\n"; } // no operator= defined yet // no copy constructor defined for aggr //----< promotion constructor with initialization >------------ aggr::aggr(int size1, int size2) : b1(size1), b2(size2) { cout << " aggr(int,int) constructor called\n"; cout << " set b1 = " << b1 << endl; cout << " set b2 = " << b2 << endl; } // no destructor defined for aggr //----< annunciator >------------------------------------------ void message(char *s) { cout << endl << s << " executable statement of main\n"; } /* */ void main() { //---------------------------------------------- message("1st"); base b1; message("2nd"); base b2 = b1; message("3rd"); b1 = b2; message("4th"); aggr a1; message("5th"); aggr a2(6,4); message("6th"); aggr a3 = a1; // no copy ctor message("7th"); a3 = a2; // no assignment oper message("no more"); } // in 6th statement a compiler generated copy ctor does element // by element copy of aggr's data - first b1 then b2 // in 7th statement a compiler generated assignment operator does // element by element assignment // first two destructions are b1 and b2 // now base dtors are called by compiler generated aggr dtor // next two are a1's data elements // next two are a2's data elements // last two are a3's data elements