Structure Inheritance in C++ with Example

Hey, guys today we are going to learn about inheritance in C++. Inheritance is a crucial characteristic of Object-Oriented Programming (OOPs).It shows the relation between two or more classes/structures. Using inheritance one class/structure can inherit the properties of another class/structure i.e the functions and data members of one class/structure are inherited by another. The derived class/structure inherits from the base class/structure also known as parent class/structure. The derived class/structure i.e class/structure which inherits the properties is called the sub class/structure or child class/structure.

Types of inheritance in C++

  1. Single inheritance: When sub structure inherits only from one base structure.
  2. Multiple inheritance: When sub structure inherits from multiple base structures.
  3. Hierarchical: When many sub structures inherit from a single base structure
  4. Multilevel: It shows the transitive nature of inheritance. When one substructure inherits from a structure which itself is inherited.
  5. Hybrid: It is a combination of two or more forms of inheritance.

Inheritance allows us to reuse the code which helps us to create similar classes using one basic class. Let understand with the help of the following example: Consider a program in which we have to create different classes/structures for storing information of students, teachers and admin staff of a school. All of these have a basic structure(Name, ID_no) but require some special features(students require class, teacher require a subject and admin staff department) also. Thus we can reuse the basic code which is common to all and add the special data members which are different using inheritance.

You can read: Structure Inheritance vs Class Inheritance in C++

Visibility modes used with Inheritance

These control access of inherited members within sub class/structure. Visibility modes available are:

  • Public: The public and protected members of the base class/structure are accessible(visible) in respective public and protected areas of sub class/structure.
  • Protected: The public and protected members of the base class/structure can be accessed(and are visible) protected area of sub class/structure.
  • Private: The public and protected members of the base class/structure are accessible(visible) in the private area of sub class/structure. The private members of the parent class/structure are not inheritable.

The difference between structure inheritance and class inheritance is that the default access specifier for inheriting a class is private whereas the default access specifier for inheriting a structure is public.

Structure Inheritance in C++

It is very similar to class inheritance in C++. The only difference is that structure access specifier is public by default. Syntax of Structure Inheritance is :

struct base_structure_name
{ 
 //body of structure
}
struct sub_structure_name : visibility_mode base_structure_name
{
 //body of sub_structure
}

Let understand with the help of an example. Let us consider we need to create different structures for storing information of students, teachers and admin staff. All of these have a basic structure consisting of Name, ID_no  but these also require some special features(students require class, teacher require a subject and admin staff department) also. Thus we create a base structure ‘school’ which includes data members Name, ID_no and a member function display(). We further created 3 derived structures ‘student’, ‘teacher’ and ‘admin’ which store information unique to each structure.
Visibility modes of ‘student’, ‘teacher’ and ‘admin’ are set as public (default), protected and private respectively.

#include <iostream>
using namespace std;
//base structure school
struct school
{
    string Name;
    int ID_no;
    void display()
    {
        cout<<Name<<endl;
        cout<<ID_no<<endl;
    }
};
//sub structure student with default(public) visibility mode
struct student:school
{
    int Class;
    char section;
    void disp()
    {
        display();
        cout<<Class<<section<<endl;
    }
};
//sub structure teacher with protected visibility mode
struct teacher:protected school
{
    string subject;
    float salary;
     void set(string n,int i)
    {
        Name=n;
        ID_no=i;
    }
    void disp()
    {
       display();
       cout<<subject<<endl<<salary<<endl;
    }
};
//sub structure admin with private visibility mode
struct admin :private school
{
    string department;
    float salary;
    void set(string n,int i)
    {
        Name=n;
        ID_no=i;
    }
    void disp()
    {
        display();
        cout<<department<<endl<<salary<<endl;
    }
};
int main()
{
    string x;
    int y;
    student s1;
    cout<<"Enter student deatils"<<endl;
    cin>>s1.Name>>s1.ID_no>>s1.Class>>s1.section;
    s1.disp();
    teacher t1;
    cout<<"Enter teacher deatils"<<endl;
    cin>>x>>y>>t1.subject>>t1.salary;
    t1.set(x,y);
    t1.disp();
    admin a1;
    cout<<"Enter admin staff deatils"<<endl;
    cin>>x>>y>>a1.department>>a1.salary;
    a1.set(x,y);
    a1.disp();
    return 0;
}

Output:

Enter student details
Karan 1509 12 A
Karan
1509
12A
Enter teacher details
Robert 1357 Mathematics 75000
Robert
1357
Mathematics
75000
Enter admin details
William 1102 Fees 120000
William
1102
Fees
120000
                                                                                                                                                                                                                                                                       

‘student’ structure has ‘Class’ and ‘section’ and ‘disp()’as additional data members.’ teacher’ structure has ‘subject’ and ‘salary’ as additional data members and disp() and set() as member function. .’ admin’  structure has ‘department’ and ‘salary’ as additional data members and disp() and set() as member function.
Kindly note: The data members Name and ID_no are inherited as protected and private data members in ‘teacher’ and ‘admin’ respectively. Thus there are inaccessible outside the objects and we use setter(Mutator Function) to assign their value

Also, refer:

Leave a Reply

Your email address will not be published. Required fields are marked *