////////////////////////////////////////////////////////////////// // DEM_AGG3.CPP -- demonstrates aggregation // // initialization of arrays // ////////////////////////////////////////////////////////////////// #include class base { public: base(void); base(const base& b); base(int x); virtual ~base(void); base& operator= (const base& b); friend ostream& operator<<(ostream& ostrm, const base& b); private: int data; }; class aggr { public: aggr(void); aggr(const aggr &a); aggr(int x1, int x2); aggr(int x); virtual ~aggr(void); aggr& operator=(const aggr& a); friend ostream& operator<<(ostream& ostrm, const aggr& a); private: base b1; base b2; }; //----< void constructor >------------------------------------- base::base(void) { cout << " base void constructor called\n"; } //----< copy constructor >------------------------------------- base::base(const base& b) : data(b.data) { cout << " base copy constructor called\n"; } // //----< promotion constructor >-------------------------------- base::base(int x) : data(x) { cout << " base(int) constructor called\n"; cout << " set data = " << data << "\n"; } //----< destructor >------------------------------------------- base::~base(void) { cout << " base destructor called\n"; } //----< assignment >------------------------------------------- base& base::operator= (const base& b) { cout << " base operator= called\n"; data = b.data; return *this; } //----< insertion >-------------------------------------------- ostream& operator<<(ostream& ostrm, const base& b) { ostrm << b.data; return ostrm; } //----< void constructor >------------------------------------- aggr::aggr(void) { cout << " aggr void constructor called\n"; } //----< copy constructor >------------------------------------- // note initialization - it's more efficient aggr::aggr(const aggr &a) : b1(a.b1), b2(a.b2) { cout << " aggr copy constructor called\n"; } //----< 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; } //----< promotion constructor taking only one argument >------- aggr::aggr(int oneSize) : b1(oneSize), b2(oneSize) { cout << " aggr(int) constructor called\n"; cout << " set b1 = " << b1 << endl; cout << " set b2 = " << b2 << endl; } // //----< destructor >------------------------------------------- aggr::~aggr(void) { cout << " aggr destructor called\n"; } //----< assignment >------------------------------------------- aggr& aggr::operator=(const aggr &a) { cout << " aggr operator= called\n"; b1 = a.b1; b2 = a.b2; return *this; } //----< annunciator >------------------------------------------ void message(char *s) { cout << endl << s << " executable statement of main\n"; } void main() { //---------------------------------------------- message("1st"); aggr a1; message("2nd"); aggr a2(6,4); cout << "\f\n"; message("3rd"); aggr a3[2]; message("4th"); aggr a4[3] = { aggr(1,2), aggr(3) }; message("no more"); } // in statement 4: // we defined an array of three elements and initialized // just the first two elements using promotion constructor // the initialization uses explicit calls to the promotion // ctors aggr(int,int) and aggr(int). // the C++ language guarantees that any unitialized elements // are constructed using a void ctor for the array element