Inheritance is the process of inheriting properties of objects of one class by objects of another class. The class which inherits the properties of another class is called Derived or Child or Sub class and the class whose properties are inherited is called Base or Parent or Super class. When a single class is derived from a single parent class, it is called Single inheritance. It is the simplest of all inheritance.
For example,
- Animal is derived from living things
- Car is derived from vehicle
- Typist is derived from staff
Syntax of Single Inheritance
class base_classname { properties; methods; }; class derived_classname : visibility_mode base_classname { properties; methods; };
Ambiguity in Single Inheritance in C++
If parent and child classes have same named method, parent name and scope resolution operator(::) is used. This is done to distinguish the method of child and parent class since both have same name.
For example,
C++ program to create and display properties of a typist from a staff using Single Inheritance.
#include<iostream> #include<conio.h> using namespace std; class staff { private: char name[50]; int code; public: void getdata(); void display(); }; class typist: public staff { private: int speed; public: void getdata(); void display(); }; void staff::getdata() { cout<<"Name:"; gets(name); cout<<"Code:"; cin>>code; } void staff::display() { cout<<"Name:"<<name<<endl; cout<<"Code:"<<code<<endl; } void typist::getdata() { cout<<"Speed:"; cin>>speed; } void typist::display() { cout<<"Speed:"<<speed<<endl; } int main() { typist t; cout<<"Enter data"<<endl; t.staff::getdata(); t.getdata(); cout<<endl<<"Display data"<<endl; t.staff::display(); t.display(); getch(); return 0; }
Output
Enter data Name:Roger Taylor Code:13 Speed:46 Display data Name:Roger Taylor Code:13 Speed:46
In this example, typist class is derived and staff class is the base class. Public members of class staff such as staff::getdata() and staff::display() are inherited to class typist. Since the child is derived from a single parent class, it is single inheritance.
Example of Single Inheritance in C++
C++ Program to Inherit a Student class from Person Class printing the properties of the Student
#include <iostream> #include <conio.h> using namespace std; class person /*Parent class*/ { private: char fname[100],lname[100],gender[10];
protected: int age; public: void input_person(); void display_person(); }; class student: public person /*Child class*/ { private: char college_name[100]; char level[20]; public: void input_student(); void display_student(); }; void person::input_person() { cout<<"First Name: "; cin>>fname; cout<<"Last Name: "; cin>>lname; cout<<"Gender: "; cin>>gender; cout<<"Age: "; cin>>age; } void person::display_person() { cout<<"First Name : "<<fname<<endl; cout<<"Last Name : "<<lname<<endl; cout<<"Gender : "<<gender<<endl; cout<<"Age : "<<age<<endl; } void student::input_student() { person::input_person(); cout<<"College: "; fflush(stdin); gets(college_name); cout<<"Level: "; cin>>level; } void student::display_student() { person::display_person(); cout<<"College : "<<college_name<<endl; cout<<"Level : "<<level<<endl; } int main() { student s; cout<<"Input data"<<endl; s.input_student(); cout<<endl<<"Display data"<<endl; s.display_student(); getch(); return 0; }
Output
Input data First Name: Harry Last Name: Potter Gender: Male Age: 23 College: Abc International College Level: Bachelors Display data First Name : Harry Last Name : Potter Gender : Male Age : 23 College : Abc International College Level : Bachelors
In the above example, we have a class person with attributes fname (first name), lname (last name), gender and age and methods input_person() to input data and display_person() to display data. Another class student is derived from person which has college_name and level as attributes and input_student() and display_student() as methods to input and display data respectively.
Here, person is base class and student is derived class. Since, person has publicly inherited student members, the private members fname, lname and gender are not inherited. The protected member age is inherited as a protected member in student, and public member functions input_person() and display_person() are inherited as public members. This is the simplest example of inheritance.