///////////////////////////////////////////////////////////////////// // MT1Q5.cpp - Compound Object also answers MT2Q5 // // // // Jim Fawcett, CSE687 - OOD : Midterm Exam, Fall 2018 // ///////////////////////////////////////////////////////////////////// #include #include "../Utilities/CodeUtilities/CodeUtilities.h" #include "../Utilities/StringUtilities/StringUtilities.h" class C { public: C() { std::cout << "\n C void constructed"; } C(const C& c) { std::cout << "\n C copy constructed"; } C(C&& c) { std::cout << "\n C moved constructed"; } C& operator=(const C& c) { std::cout << "\n C copy assigned"; } C& operator=(C&& c) { std::cout << "\n C move assigned"; return *this; } ~C() { std::cout << "\n C destroyed"; } }; class B { public: B() : pC_(new C) { std::cout << "\n B void constructed"; } B(const B& b) : pC_(new C(*b.pC_)) { std::cout << "\n B copy constructed"; } B(B&& b) : pC_(new C(std::move(*b.pC_))) { b.pC_ = nullptr; std::cout << "\n B moved constructed"; } B& operator=(const B& b) { if (this == &b) return *this; delete pC_; pC_ = new C(*b.pC_); std::cout << "\n B copy assigned"; } B& operator=(B&& b) { if (this == &b) return *this; delete pC_; pC_ = b.pC_; b.pC_ = nullptr; std::cout << "\n B move assigned"; } ~B() { delete pC_; std::cout << "\n B destroyed"; } private: C* pC_; }; class U { public: U() { std::cout << "\n U void constructed"; } U(const U& u) { std::cout << "\n U copy constructed"; } U(U&& u) { std::cout << "\n U moved constructed"; } U& operator=(const U& u) { return *this; std::cout << "\n U copy assigned"; } U& operator=(U&& u) { return *this; std::cout << "\n U move assigned"; } void say() { std::cout << "\n U here"; } ~U() { std::cout << "\n U destroyed"; } }; class D : public B { public: D() : B() { std::cout << "\n D void constructed"; } D(const D& d) : B(d) { std::cout << "\n D copy constructed"; } D(D&& d) : B(std::move(d)) { std::cout << "\n D moved constructed"; } D& operator=(const D& d) { if (this == &d) return *this; B::operator=(d); std::cout << "\n D copy assigned"; } D& operator=(D&& d) { B::operator=(std::move(d)); std::cout << "\n D move assigned"; } void UsesU(U* pU) { pU->say(); } ~D() { std::cout << "\n D destroyed"; } }; int main() { Utilities::Cosmetic cos; Utilities::Title("MT1Q5 - Construction of Compound Object"); D d1; D d2 = d1; // copy construction D d3 = std::move(d2); // move construction U u; d3.UsesU(&u); }