#include using namespace std; class A { public: A(); // default constructor A(int n); // second constructor ~A(); // destructor int aa() const; // accessor int ma(int n); // mutator private: int *ap; }; A::A() { cout << "Default constructor of A called.\n"; ap = new int; *ap =0; } A::A(int n) { cout << " Second constructor of A called, set " << n << endl; ap = new int; *ap =n; } A::~A() { cout << *ap << ": an object of class A destroyed.\n"; delete ap; } int A::aa() const { return *ap; } int A::ma(int n) { *ap = n; return *ap; } ////////////////////////////////////////////// class B: public A { public: B(); // default constructor B(int n); // second constructor ~B(); // destructor int ab() const; // accessor int mb(int n); // mutator private: int *bp; }; B::B() { cout << "Default constructor of B called.\n"; bp = new int; *bp =0; } B::B(int n) { cout << " Second constructor of B called, set " << n << endl; bp = new int; *bp =n; } B::~B() { cout << *bp << ": an object of class B destroyed.\n"; delete bp; } int B::ab() const { return *bp; } int B::mb(int n) { *bp = n; return *bp; } ////////////////////////////////////////////// class C: public B { public: C(); // default constructor C(int n); // second constructor ~C(); // destructor int ac() const; // accessor int mc(int n); // mutator private: int *cp; }; C::C() :B(1) // Can't call A's constructor. { cout << "Default constructor of C called.\n"; cp = new int; *cp =0; } C::C(int n) :B(2) // Can't call A's constructor. { cout << " Second constructor of C called, set " << n << endl; cp = new int; *cp =n; } C::~C() { cout << *cp << ": an object of class C destroyed.\n"; delete cp; } int C::ac() const { return *cp; } int C::mc(int n) { *cp = n; return *cp; } ////////////////////////////////////////////// class D { public: D(); // default constructor ~D(); // destructor int ad() const; // accessor int md(int n); // mutator protected: int *d; }; D::D() { cout << "Default constructor of D called.\n"; d = new int; *d = 0; } int D::ad() const { return *d; } int D::md(int n) { *d = n; return *d; } D::~D() { cout << *d << ": an object of class D destroyed.\n"; delete d; } class E: public D { public: E(); // default constructor int ae() const; // accessor int me(int n); // mutator }; E::E() { cout << "Default constructor of E called.\n"; } int E::ae() const { return *d; // E can touch *d!! } int E::me(int n) { *d = n; return *d; } //////////////////// void f() { A a(7); B b(77); C c(777); D d; E e; cout << "\nf is finishing..\n"; } int main() { char akey; A a(1); B b(10); // Default constructor of the base class will be called. C c(100), d; cout << a.aa() << endl; cout << b.aa() << " " << b.ab() << endl; cout << c.aa() << " " << c.ab() << " " << c.ac() << endl; cout << d.aa() << " " << d.ab() << " " << d.ac() << endl; cout << b.ma(2) << endl; cout << b.mb(5) << endl; // b.A(4); // Constructor is not inherited // b.A::~A(); // Destructor is inherited; but not a good idea to use. // b.~B(); // Not good to use. cout << "\n\nCall function f:\n"; f(); cout << "f is done!\n"; { cout << "\n\nHere is a little scope:\n"; C c; cout << "Leaving this little scope\n\n"; } cout << "Press return to see how this program ends.\n"; cin.get(akey); cout << "\nEnd of the program.\n\n"; return 0; }