Code About Us Tutorial


  Inheritance
    What is Inheritance?
    Creating a Derived Class
    Creating an Object
    Simple Inheritance
    Overloading Derived Classes
    Dominating Member Functions
    Polymorphism & Virtual Functions


   
 

Home > Inheritance > What is Inheritance?

November 27, 2009 1:58 am

   

What is Inheritance?

Hello everyone, welcome to another exciting chapter in C++. As you can see by the title, this chapter will cover the topic inheritance. Inheritance in C++ is similar to Inheritance in Biology. For instance, some people think that reptiles evolved from the dinosaurs. The reptiles inherited the dinosaur’s scaly skin and tail. This type of association can also be used in programming, for instance, say you have a class named Reptile, and in this class you had traits such as member functions and variables. The base class would look like this:
 
 
//tq.h
//Example of simple inheritance

//base class class Reptile { public: Reptile(): itsHeight(100),itsWeight(2000){};

void setHeight(int height) { itsHeight = height;} int getHeight() {return itsHeight;}

void SetWeight(int weight) { itsWeight = weight;} int getWeight() { return itsWeight;}

protected: int itsHeight; int itsWeight; };

This looks like a regular class, except for the new keyword protected. Protected is used when you are using inheritance. The reason we don not use private when we use inheritance is because private is not available to derived or supersets of the base class. If you create a derived class from Reptile and named it Lizard, the Lizard class would have all the member variables and functions that the base class has, even the constructor would affect the derived class. Please Rate this Code:


    Comments for: What is Inheritance?
Annonymouse User says:
Inheritance is GOOD!

Gavin [gavin@yahoo.com] Posted: 2 times. says:
Andy has a horn for inheritance.




Add Comment:
Name:
Email:


 «6» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.