#include #include using namespace std; // BaseC is the "base class" or "super class" or "parent class" // -- it defines some member functions and a single attribute, n class BaseC { public: BaseC(string name = "") : n(name) { cerr << " BC constructor "; } /*virtual*/ ~BaseC() { cerr << " BC destroy " << n << endl;} void setName(string name) { n = name; } string getName() { return n; } /*virtual*/void prt() { cerr << "Name is " << n; } protected: string n; }; // DerivedC is a "derived class" or "sub class" or "child class" of BaseC // -- it inherits the member functions* and attributes of BaseC // -- it also defines new member functions and an attribute, s // -- Note: // -- *constructors, destructors, and assignment operator are not inherited // -- prt() is redefined (or overridden) // -- See how the parent class constructor is use in base initialization class DerivedC : public BaseC { public: DerivedC(string name = "", int size = 1) : BaseC(name), s(size) { cerr << " DC constructor "; } ~DerivedC() { cerr << " DC destructor "; } int getSize() { return s; } void setSize(int size) { s = size; } void prt() { cerr << "Name is " << n << " size = " << s; } private: int s; }; enum color { red, yellow, green }; // AnotherDC is a "derived class" or "sub class" or "child class" of DerivedC // AnotherDC is also a "derived class" or "sub class" or "child class" of BaseC // -- it inherits the member functions* and attributes of DerivedC // -- it also defines new member functions and an attribute, c // -- Note: // -- *constructors, destructors, and assignment operator are not inherited // -- prt() is once again redefined (or overridden) // -- Again, see how the parent class constructor is used // -- Although prt() is redefined, we can still acccess the parent version // -- Watch what happens when we write prt() another way // -- Why is there a "using" clause in this class? class AnotherDC : public DerivedC { public: AnotherDC(string n = "", int s = 1, color clr = red) : DerivedC(n, s), c(clr) { cerr << " ADC constructor\n"; } ~AnotherDC() { cerr << " ADC destructor "; } int getColor() { return c; } void setColor(color clr) { c = clr; } void setName (char *c) { string str = c; str += '-'; setName(str); } void prt() { DerivedC::prt(); cerr << " color = " << ((c==red) ? "r":((c==yellow) ? "y":"g")); } //void prt() { cerr << "Name is " << n << " size = " << s; // cerr << " color = " << // ((c==red) ? "r":((c==yellow) ? "y":"g")); } using BaseC::setName; private: color c; }; // -- Now notice below we have five of our own class variables, a-e // -- See how *d ISA BaseC and *e ISA DerivedC (FYI *e ISA BaseC also) // -- Notice the prt() calls for these variables // -- Notice the constructor and destructor calls for these variables // -- What happened to the destruction of c-e? int main () { string s("a"); // static (or automatic) objects AnotherDC a("A" ); AnotherDC b("B", 2, yellow); // note, we can't access attribute n in the main program //a.n = "a"; // note, two different (overloaded) versions of setName() //a.setName("a"); //a.setName( s ); // dynamic objects BaseC *c = new BaseC ("C" ); cerr << endl; BaseC *d = new DerivedC ("D", 4 ); cerr << endl; DerivedC *e = new AnotherDC("E", 5, green); a.prt(); cerr << endl; b.prt(); cerr << endl; c->prt(); cerr << endl; d->prt(); cerr << endl; e->prt(); cerr << endl; //delete c; //delete d; //delete e; return 0; }