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 > Simple Inheritance

November 27, 2009 6:27 pm

   

Simple Inheritance

To clarify the calling order of constructors and destructors in the base class and the derived classes here is an example:

 

 
//tq.h

//Example of simple inheritance

//base class

class Reptile { public: Reptile(); ~Reptile();

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; };

//Constructor

Reptile::Reptile() { cout <<"Calling Base Class constructor(Reptile)....nn"; }

//Destructor

Reptile::~Reptile () { cout <<"Calling Base Class destructor(Reptile)....n"; }

//superste, derived class from base class(reptile)

class Lizard: public Reptile { public: Lizard(); ~Lizard();

void setAge(int age) { itsAge = age;} int getAge() { return itsAge;}

void RegenerateTale() const { cout<<"Regenerating Tale....n";} void RunFromPredator() const { cout <<"Running...n";} protected: int itsAge; };

//Constructor

Lizard::Lizard() { itsAge = 4; cout <<"Calling Derived Class Constructor(Lizard)....nn"; }

Lizard::~Lizard() { cout <<"Calling Derived Class Destructor(Lizard)....n"; }

In this file, the constructor of both the base and the derived class will be shown when they are called.

Here is the .cpp file:

 

 
#include <iostream.h>
#include "tq.h"

int main() {

cout <<"Using Derived class, Creating a Lizard...." << endl; Lizard KamotoDragon;

cout <<"Name: Kamoto Dragonn";

cout <<"Age: " << KamotoDragon.getAge() << endl;

KamotoDragon.RegenerateTale(); KamotoDragon.RunFromPredator();

return 0; }

The output of this program would be this:

Using Derived class, Creating a Lizard… Calling Base Class constructor(Reptile)….

Calling Derived Class Constructor(Lizard)….

Name: Kamoto Dragon Age: 4 Regenerating Tale…. Running… Calling Derived Class Destructor(Lizard)…. Calling Base Class destructor(Reptile)….

As you can see the Base Class Constructor was called first when the object was created followed by the Derived Class Constructor. When the object was destroyed, first the Derived Class Destructor was called followed by the Base Class Destructor. This should give you a better understanding of how constructors and destructors are called. Please Rate this Code:


    Comments for: Simple Inheritance
Annonymouse User says:
simple and clearfgjtymjdtnyktrymkthnghnfnmghmfhkhyuuuuyktukyttttukytukkkkk

Annonymouse User says:
simple and clearfgjtymjdtnyktrymkthnghnfnmghmfhkhyuuuuyktukyttttukytukkkkk says:
NOOOOOOOOOOOOOOO!!!!!!!!!! HELPPPPPPPP MEEEEEEEEE THE DRAGON IS COMINGGGGGGGG

BUT I HAVE PHOENIX MIRROR, WONT IT WORK?????????

LEME TRY..........NNNNNNNNN ARGHHHHHHHHHHHHHHHHHHHHHHHHHHH WOT THE %^%&%*&&((* IS THIS!!!!!!!!!!!!!!!!!!!!!!!!! MUST USE INHERITANCE, ANYWAY VERY COOL SITE, EXPLAINS IT LIKE 2 YEAR OLDD KIDS R READING IT BUT I STILL DONT GET IT :'(





Add Comment:
Name:
Email:


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