Single Inheritance


The process of deriving a new class from already existing class is known as single inheritance. Thus in single inheritance there is one base class and one derived class.

Single Inheritance

Consider Following Example of Single Inheritance:

     

#include<iostream.h>
class student
{
int rollno;
char name[20];
public:
void inputst()
{
cout<<"Enter Roll Number:";
cin>>rollno;
cout<<"Enter Name:";
cin>>name;
}
void outputst()
{
cout<<"RollNumber:"<<rollno<<endl;
cout<<"Name:"<<name<<endl;
}
};
class result:public student
{
int totalmark;
public:
void inputmark()
{
cout<<"Enter marks:";
cin>>totalmark;
}
void outputmark()
{
outputst();
cout<<"Total Mark:"<<totalmark;
}
};
int main()
{
result r1;
r1.inputst();
r1.inputmark();
r1.outputmark();
return 0;
}
Output:
Enter Roll Number: 1
Enter Name: YESHA
Enter Mark: 500
Roll Number: 1
Name: YESHA
Total Mark: 500

In above example class result is publicly derived from student class. So all the public member of class student becomes public member of class result. Private member of the student class can not be inherited.

Download Projects


Download Programs