Programmers seem to hate coding something from scratch; why code the same thing over and over again? Fortunately, it is possible to reuse code, thanks to one of the key concepts of object-oriented programming. That is inheritance, which is the subject of this article. So if you're looking for a clear explanation of how inheritance works in C++, keep reading.
Inheritance in C++ - Constructor, Deconstructor (Page 5 of 5 )
You can override the functions from the base class. Declaring a function in the base class and in the derived class in the same form is possible. This will lead to an override of the function inside the base. To illustrate, we may want to write a print function about the internal data. For the GeEntity this is the name and ID.
In both cases, you can write just a print function, because in the derived class the base function will be overwritten. Then again, reusability is a prime factor, so you can use the written print code for the base just by explicitly calling that print function inside the new print, like this:
void GePoint::print() const
GeEntity::print(); // print the base
// now print our data
In the same manner, you can force to call the derived function by specifying it:
Only one more detail remains to exploit. How do the constructions/deconstructions work in derived classes? To make it easier to find the answer, I made a small project with a base Entity class that has a GePoint derived class.
To make it a little more interesting, the Point is a member of another class that is derived from the base class: GeCircle. As the base has a unique ID that should be allocated automatically and is obligatory at construction for this purpose, no default constructors (no arguments) are allowed.
I wrote in Visual Studio 9.0 (2008), but as long as you create a new solution, it should also work in older versions. The code is commented in detail, so please observe it. However, the main point of interest remains the main function, where you can just create objects and observe in which order they are constructed/deconstructed. On the screen will be printed which constructors entered the application and which deconstructors exited it.
When you play around a little, you will observe that the construction of objects proceeds from top to bottom in the hierarchy tree. When you construct a point, first it will construct an entity, and after that the point itself. Deconstruction goes in the opposite direction, from bottom to top, so the entity will be the last to be destroyed.
Entity constructor with parameters
Point Constructor with full argument list
Default Entity De-constructor
Press any key to continue . . .
Here is the source code:
All that it remains is for me is to thank you for your patience and invite you to post your messages here on the blog and/or over the friendly ever-growing DevHardware forum. Thank you for your attention and “Live with Passion!”
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.