C++
  Home arrow C++ arrow Page 5 - Inheritance in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Inheritance in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2009-06-09

    Table of Contents:
  • Inheritance in C++
  • Inheritance Types
  • Inheritance in General
  • Where to Use Protected
  • Constructor, Deconstructor

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    GePoint alfa;

    alfa.GeEntity::print();

    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

    Point De-Constructor

    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.

       · Please do not use inheritance for code reuse! Inheritance for code reuse leads to...
       · Thanks for your comment, As usual the coin has two sides. And you just touched...
     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek