C++ Single Inheritance Example

Here is an example program to illustrate single inheritance.

// example of single inheritance
// parent class
#include<iostream>
#include<iomanip>
using namespace std;
class person
{
private:
    char name[15], address[15];
public:
    void getdata()
    {
        cout<<"\nEnter Name: ";
        cin>>name;
        cout<<"\nEnter address: ";
        cin>>address;
    }
    void showdata()
    {
        cout<<endl<<setw(15)<<"Name: "<<name;
        cout<<endl<<setw(15)<<"Address: "<<address;
    }
};

// derived class from parent class person
class employee:public person
{
private:
    int empID;
public:
    void getdata()
    {
        person::getdata();
        cout<<"\nEnter employee ID ";
        cin>>empID;
    }
    void showdata()
    {
        person::showdata();
        cout<<endl<<setw(15)<<"Employee ID: "<<empID;
    }
};
int main()
{
    employee obj;
    obj.getdata();
    obj.showdata();
    return 0;
}

The output of the program is:

single inheritance cpp

C++ Multiple Inheritance Example