// A5.cpp - what contructors are needed? Describe all operations. #include using namespace std; class X { public: X(int val) { x = val; } X operator-() { X temp(-x); return temp; } int value() { return x; } void store(int val) { x = val; } private: int x; }; class Y : public X { public: Y(int y) : X(y) { store(y); } }; X aFunction(X &x) { return -x.value(); } void main() { ///////////////////////////////////////////////////////////////////// // In the statement below: // - promotion constructor Y::(int) is called which in turn calls // promotion constructor X::(int) Y y1(5); cout << "\n y1's value is " << y1.value(); ///////////////////////////////////////////////////////////////////// // In the statement below: // - y1 is passed by reference, so no constructor is called // - x, inside aFunction(...), is a reference to y1, so, the // inherited X::value() is called. // - value returns an int, so aFunction must return it by calling // the X::X(int) // - x2 is copy constructed, using the returned X value and compiler // generated copy constructor. X x2 = aFunction(y1); cout << "\n x2's value is " << x2.value(); cout << "\n\n"; }