// A5.cpp - Can you find problems with this code? #include using namespace std; class X { public: X() : val(-3) { }; // creates X object { -3 } X(int i) : val(i) { } void mf() { cout << "\n mf() - value = " << val; } private: int val; }; class Y : public X { public: Y() : X(), val(5), dbl(3.5) { } // creates Y object { { -3 }, 5, 3.5 } Y(int d, int i) : X(), dbl(d), val(i) { } private: double dbl; int val; }; ///////////////////////////////////////////////////////////////// // This function only works correctly for arrays of base types // You could correct the function by passing an array of base // class pointers to objects, rather than an array of objects. // This, however would change the signature of aFunction to // aFunction(X **x) void aFunction(X *x) { x[1].mf(); } void main() { X x[2]; // creates { -3 }{ -3 } aFunction(x); Y y[2]; // creates { { -3 }, 5, 3.5 }{ { -3 }, 5, 3.5 } aFunction(y); cout << "\n\n"; }