C++
  Home arrow C++ arrow Page 2 - 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++ - Inheritance Types


    (Page 2 of 5 )

    Just like the member functions, variables can be of three types; inheritance can also be made in three different ways. The effect of the type of the inheritance you use will reflect on the inherited type’s traits.

    The table below shows how this works.

     

     

    In order to explicitly specify the type of inheritance, you are going to need to add the public/private/protected keyword in front of the derived class. By default, this is private. Therefore, the correct way to inherit from the GeEntity is by using the public keyword:

    class GeCircle : public GeEntity

    {

    // write the class specifics };

    In this way we can use all of the base’s members, just like we would if we own the class, unless that was private. Private members are not inherited regardless of the inheritance type because that would violate the private statement. It would not be private, as a derived class can access it freely. Specify protected access when you want to hide some functions from the user but not from its base classes. This lets you access any internal data/functions of the base class by the derived class and its friends.

    Here it is important for you to understand the “is a” and “has a” relationship between classes. When an object inherits a class that is a type of the base class, it is that class; for example, a Circle is a Geometric Entity. But when you declare, as a member variable, a class inside a new class, you're looking at a "has a" relationship. A car has a steering wheel, one engine, and so forth. Private inheritance essentially provides another way to create the composition.

    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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek