Deriving a Class from another Derived Class

Many of the peoples think that deriving a class from other derived classes
is a confusing thing, that’s why I have written this article to let them
know that deriving such classes is no different. To the compiler it doesn’t
matter whether the class from which a new class is derived is itself derived
or not.


The example program below illustrates this:



// -- INHERITANCE --
// Example program to illustrate
// the derivation of a class
// from another derived class
#include<iostream.h>

// base class
class one
{
int a;

public:
void setone(int num){a=num;}
int getone(){return a;}
};

// derived from base class 'one'
class two:public one
{
int b;

public:
void settwo(int num){b=num;}
int gettwo(){return b;}
};

// derived from derived class
// 'two'
class three:public two
{
int c;

public:
void setthree(int num){c=num;}
int getthree(){return c;}
};

void main(void)
{
// declare objects
one o1;
two o2;
three o3;

// set values
o1.setone(1);

o2.setone(10);
o2.settwo(20);

o3.setone(100);
o3.settwo(200);
o3.setthree(300);

// display values
cout<<o1.getone()<<endl<<endl;

cout<<o2.getone()<<endl;
cout<<o2.gettwo()<<endl<<endl;

cout<<o3.getthree()<<endl;
cout<<o3.getthree()<<endl;
cout<<o3.getthree()<<endl<<endl;
}


Related Articles:


Check out this stream