Write a C++ program to demonstrate example of hierarchical inheritance?

vijaya shankar.k | 3864 Views | c++ | 11 Aug 2016

 

Inheritance :

  • Inheritance have the one of the most important features of object oriented programming is code-re usability and it is achieved through inheritance.
  • Inheritance is the process by which objects of one class acquire the properties of another class. By this we can add additional features to an existing class without modifying it.
  • This is possible by deriving a new class from the existing one. The new class will have combined features of both the classes.

Merits Of Inheritance :

  • Code reusablitiy as the same base class could be used by many child classes adding their specific properties.
  • Accessbility, while inheriting a parent class the child class's declaration specifies the access ( public, private or protected ) .So, one could restrict the visibility of the parent class to enhance security.

Types Of Inheritance :

There are different types of inheritance:
  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid (Virtual) Inheritance

Hierarchical Inheritance :

  • When a  class  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 more than one classes are derived from a single base class, such inheritance is known as Hierarchical Inheritance, where features that are common in lower level are included in parent class. 

Syntax :

class base_class_name
{
private:
…………….
public:
…………….
};
class derived_class_name1 : public base_class_name
private:
……………
public:
……………
};
class derived_class_name2 : public base_class_name
private:
…………..
public:
…………..
};

Example :

#include<iostream.h>
#include<conio.h>
class person
{
 private:
 char name[20];
 long int phno;
 public:
 void read()
 {
  cout<<"\n Enter name ";
  cin>>name;
  cout<<"\n Enter Phno=";
  cin>>phno;
 }
 void show()
 {
  cout<<"\n Name="<<name;
  cout<<"\n Phone="<<phno;
 }
};
class student : public person
{
 private:
 int rollno;
 char course[20];
 public:
 void read()
 {
  person::read();   // call the member function of the class person
  cout<<"\n Enter Roll No=";
  cin>>rollno;
  cout<<"\n Enter Course=";
  cin>>course;
 }
 void show()
 {
  person ::show();  // call the member function of the class person
  cout<<"\n Roll No ="<<rollno;
  cout<<"\n Course="<<course;
 }
};
class teacher:public person
{
 private:
 char dept_name[10];
 char qual[10];
 public:
 void read()
 {
  person::read();  // call the member function of the class person
  cout<<"\n Enter dept_name and Qualification=";
  cin>>dept_name>>qual;
 }
 void show()
 {
  person::show();  // call the member function of the class person
  cout<<"\n Departement="<<dept_name;
  cout<<"\n Qualififcation="<<qual;
 }
};
void main()
{
 clrscr();
 student s1;   // Object created for student class
 cout<<"\n************************ Enter student Information******************";
 /* Call member function from class 'person' */
 s1.read();
 cout<<"\n************************ Displaying Student Information*************";
 /* Call member function from class 'student' */
 s1.show();
 teacher t1;  // Object created for teacher class
 cout<<"\n************************** Enter Teacher Information*****************";
 /* Call member function from class 'person' */
 t1.read();
 cout<<"\n*****************************Displaying Teacher Information************";
 /* Call member function from class 'teacher' */
 t1.show();
 getch();
}

Sample Output :