When a class object is constructed , the following sequence takes place.
When a class object is destroyed the following sequence takes place.
So the cMyChild constructor automatically calls the default cMyParent constructor before getting inside its own code. A handy way to think of this is to imagine that a cMyChild object has a cMyParent object as a member. The parent class and the members all get their constructors called. Now, it may be that you want to feed some arguments into the base class or member constructors. To do this, you write out these constructors in an 'initializer list' that follows a colon after the constructor. Here's an example of a class definition. cTeacherProgrammer : public cProgrammer { private: int _ugliness; cGollywog *_pimaginaryfriend; public: cTeacherProgrammer(int flubba, float gleep); ~cTeacherProgrammer(); } And here's a constructor using an initializer list. cTeacherProgrammer::cTeacherProgrammer(int flubba, float gleep): cProgrammer(flubba, gleep), _ugliness(1000000) { _pimaginaryfriend = new cGollywog(this); } And here's how the destructor would be defined. cTeacherProgrammer::~cTeacherProgrammer(){delete _pimaginaryfriend);} Note that the cTeacherProgrammer destructor first does the delete _pimagi naryfriend , and then calls the parent cProgrammer destructor. You can remember the sequence by thinking in terms of working your way down the hierarchy at construction, and working your way back up at destruction. When you need to code up several different forms of a constructor, it can be useful to have an initialization helper function that the different constructors in both the parent and the child class can call. |