// A2.4.cpp : Defines the entry point for the console application. // // Given the code fragments below, where the ellipsis (…) represents // code that has not been disclosed to you: // // class X { … }; class Y { public: explicit Y(const X& x); … }; // // what can you say about the compilation and execution of each of // the following statements? Describe each of the operations that // occur as this code executes. // // X x1, x2; // Y y2 = x1; // Y y[4] = { Y(x1), Y(x2), Y(X()) }; #include "stdafx.h" #include using namespace std; class X { public: X() { cout << "\n in X::X()"; } X(const X& x) { cout << "\n in X::X(const X& x)"; } ~X() { cout << "\n in X::~X()"; } }; class Y { public: // main won't compile if any of these constructors are not defined Y() { cout << "\n in Y::Y()"; } explicit Y(const X& x) /* : nothing to initialize */ { cout << "\n in Y::Y(const X& x)"; } Y(const Y& y) /* : nothing to initialize */ { cout << "\n in Y::Y(const Y& y)"; } ~Y() { cout << "\n in Y::~Y()"; } }; int _tmain(int argc, _TCHAR* argv[]) { std::cout << "\n Exam A2 - Question #4 " << "\n =======================\n"; cout << "\n\n-- first statement --"; X x1, x2; cout << "\n\n-- second statement --"; // Y y2 = x1; // won't compile - needs explicit conversion Y y2 = Y(x1); cout << "\n\n-- third statement --"; Y y[4] = { Y(x1), Y(x2), Y(X()) }; //// answer //////////////////////////////// // // as shown in the output of this program // //////////////////////////////////////////// cout << "\n\n"; return 0; }