Tuesday 2 September 2014

INHERITANCE

INHERITANCE

Reusability is yet another important fearture of oop. Fortunatelty,C++ strongly supports the concept of reusability.The C++ class es can be reused in reused in several ways.

The mechanism of deriving a new class from old one is called inheritance.The OLD CLASS is referred to as the BASE CLASS and The NEW CLASS is DERIVED CLASS.

FORMS OF INHERITANCE
 
  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance
Lets take a example of Single Inheritance and we understand the concept of Inheritance.

Class XYZ

{
int a,b,c;

public:

void get()
{
cout<<"Enter the values"

cin>>a>>b;
c=a*b;
cout<<c;
}

};

class ABC : public XYZ            // symbol of Inheritance
{
int x,y,z;

public:

void show()
{
cout<<"Enter the values"
cin>>x>>y;
c=x*y;
cout<<z;
}

};

void main()
{
ABC t;
t.get();
t.show();
getch();
}

I hope that you understand concept of Single Inheritance .Lets take another example of Inheritance and Now we study concept of Multilevel Inhertance.

Class A

{
int a,b,c;

public:

void add()
{
cout<<"Enter the values"

cin>>a>>b;
c=a+b;
cout<<c;
}

};

class B : public A           //Inheritance
{
int x,y;

public:

void mul()
{
cout<<"Enter the values"
cin>>x;
y=c*x;
cout<<y;
}

};

class C: public B           //Inheritance
{
int p,q;

public:

void div()
{
cout<<"Enter the values"
cin>>p;
q=y/p;
cout<<q;
}

};

void main()
{
C t;
t.add();
t.mul();
t.div();
getch();
}
 

No comments:

Post a Comment