Saturday, 2 October 2010

SINGLE INHERITANCE





SINGLE INHERITANCE:
If a class is derived from only one base class it is called as single inheritance.
In single inheritance only one base class and one derived class are present.
Here X is a base class and Y is a derived class. Further, no class is derived from Y. This is a single inheritance.
The general form of single inheritance is as follows:
class <name of the derived class>: <access specifier> <name of the base class>
Example 3:
#include<iostream.h>
#include<conio.h>
class A
{
private:
int a, b;
public:
void setVal(int x, int y)
{
a=x;
b=y;
}
};

class B: public A  //class B inherits class A
{
private:
int c;
public:
B(int z)        //one argument constructor of B.
{
c=z;
}

void show()
{
cout<<”a=”<<a<<”\nb=”<<b<<”\nC=<<c”;
}
};

void main()
{
B bObj(30);
bObj.setVal(10,20)   //
clrscr();
obj.show ();
}

Output:
A=10
B=20
C=30

In the above example program class B is inherited by class A. So the member variables and member functions of class A are passed to class B.


Types of Inheritance




No comments:

Post a Comment