///////////////////////////////////////////////////////////////////// // MT1Q8andMT4Q6.cpp #include #include #include class B { public: B(std::string s) : s_(s) {} std::string& str() { return s_; } /* ... */ private: std::string s_; }; class D : public B { public: D() : B(""), i_(0) {} D(const D& d); D& operator=(const D& d); int& value() { return i_; } private: int i_; }; D::D(const D& d) : B(d), i_(d.i_) {} D& D::operator =(const D& d) { if(this == &d) return *this; static_cast(*this) = d; i_ = d.i_; return *this; } void main() { std::cout << "\n Creating an instance of D:"; D d1; d1.str() = "a D type"; d1.value() = 1; std::cout << "\n " << d1.str() << " with value " << d1.value(); std::cout << "\n Creating a D and assigning previous instance to it:"; D d2; d2 = d1; std::cout << "\n " << d1.str() << " with value " << d1.value(); std::cout << "\n Creating an instance of D as a copy of previous instance:"; D d3 = d2; std::cout << "\n " << d1.str() << " with value " << d1.value(); std::cout << "\n\n"; } Creating an instance of D: a D type with value 1 Creating a D and assigning previous instance to it: a D type with value 1 Creating an instance of D as a copy of previous instance: a D type with value 1