C++
  Home arrow C++ arrow Page 4 - 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++ - Where to Use Protected


    (Page 4 of 5 )

    While inheriting from the base classes, you may be tempted to specify protected access for the base variable members, so you can call them directly in the derived classes. As convenient as this may seem at first, I want to ring the warning bell; this is a dangerous practice, and one that can lead to error nodes and hard-to-maintain code.

    Inheritance, to be efficient and software design friendly, should not provide direct access to the base class data. The point of inheritance is to provide services rather than dependency. By this, I mean that it should benefit from the base's functions only.

    Suppose you declare a protected member variable in the base class. By definition, all classes derived from it, and friends of those, will have direct access to the variable. You will spare a few lines that you should have written for the get/set functions. Imagine now that you've finished the full hierarchy and implemented it into an application.

    After a period, someone comes along, finds the name of the variable inappropriate, and changes it to a new one. Moreover, after this he simply tries to build the application. Countless errors will be generated if he forgot to change the names in the derived classes as well. Changing those is a troublesome task.

    In addition, this is only the smaller issue. If you code in this fashion, you will find yourself in a situation where someone has changed the variable at a point and you have no tool with which to catch it.

    The way I wrote the class on the first page is the solution. The members should always be private, so no one can access it besides the class itself, offering its services instead through the name and ID function.

    The implementation looks like this: 

    int& GeEntity::ID()

    {

    return m_ID;

    }

     

    const int& GeEntity::ID() const

    {

    return m_ID;

    }

     

    Do it in the same way for the name also. These return the reference to the parameter both non-const and const so you can change and just read the data from the variables. If someone changes its value inappropriately, you can debug it easily by putting a break point inside the non-const call. It is simple and efficient.

    Some people say that this will further slow down the application, because it will use a function call for accessing a variable and returning a value, wasting both CPU and memory. However, this is false. Today’s compilers are smart enough to detect get/set functions and reference returns like this, and they will do the optimization.

    At the compilation, there is a stage called inliner. This will take all functions that will not increase the size of the build significantly and put them directly inside the application. For instance, you may have a long function, although it is called only at a specific place. We can just take out that segment and put it where it is called, and economize a function call with absolutely no size change.

    When you have a long function that is called a million times, for example, we had better leave it, as the extra size generated is too much to pay for. The compilers are once again behaving intelligently, striving to resolve situations like this. This encourages software engineering and a better readability/maintainability of the code.

    More C++ Articles
    More By Gabor Bernat


       · 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