// A1.1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include class B { public: B() { x = 0; } virtual ~B() { std::cout << "\n in B::~B()"; } int& value() { return x; } virtual void say() { std::cout << "\n B::x = " << x; } private: int x; }; class D : public B { public: D(const std::string &s, std::istream &is); D(const D&d); ~D() { std::cout << "\n in D::~D()"; } virtual void say() { B::say(); std::cout << "\n D::s = " << s; std::cout << "\n Reading from istream D::is:\n"; std::string line; while(is.good()) { std::getline(is,line); if(line.size() == 0) break; std::cout << line << std::endl; } } private: std::string s; std::istream &is; }; D::D(const std::string &str, std::istream &in) : B(), s(str), is(in) { } //// answer ///////////////////////////////////// D::D(const D &d) : B(d), s(d.s), is(d.is) { } ///////////////////////////////////////////////// int _tmain(int argc, _TCHAR* argv[]) { std::cout << "\n Exam A1 - Question #1 " << "\n =======================\n"; B b; b.value() = 3; b.say(); D d("please enter some lines of text then enter to quit:",std::cin); d.value() = -5; std::cout << "\n d's value = " << d.value(); D d2 = d; d2.say(); return 0; }