Single Inheritance
A Single inheritance is the procedure of creating new classes from an existing base class. An existing class is known as the direct base class and the newly created class is known as a singly derived class.
Single inheritance is the capability of a derived class to inherit the member functions and variables of the existing base class.
Example
There is a program to read the derived class data members like as name, roll, sex, height, weight from the keyboard and show the contents of the class on the screen. That program describes a single inheritance concept, that consists of a base class and a derived class.
#include <iostream.h>
#include <iomanip.h>
class basic_info
{
private:
char name[20];
long int rollno;
char sex;
public:
};
void getdata();
void display();
class physical_fit: public basic_info
{
private:
float height;
float weight;
public:
};
void getdata();
void display();
void basic_info::getdata()
{
cout<<"Enter a name ?\n";
cout>>name; cout<<"Roll No.?\n";
cin>>rollno;
cout<<"Sex ?"\n";
cin>>sex;
}
void basic_info::display()
{
cout<<name<<" ";
cout<<rollno<<" ";
cout<<sex<<" ";
}
void physical_fit::getdata()
{
basic_info::getdata();
cout<<"Height ?\n";
cin>>height;
cout<<"Weight ?\n";
cin>>weight;
}
void physical_fit::display()
{
basic_info::display();
cout<<setprecision(2);
cout<<height<<" ";
cout<<weight<<' ";
}
void main()
{
physical_fit a;
cout<<"Enter the information \n";
a.getdata();
cout<<"-----------------------------------------------\n";
cout<<"Name RollNo Sex Height Weight \n";
cout<<"-----------------------------------------------\n";
a.display();
cout<<endl;
cout<<"------------------------------------------------\n";
}