C++ Programming

Inheritance in C++, Different Types of Inheritance and Their uses, examples

INHERITANCE in C++

The second most important aspect of object-oriented programming is inheritance. The code of existing classes is used in inheritance for making new ones that saves time for a new class to write and debug the entire code. Inheriting means getting a new class is written in inheritance that it can access or use the members of an existing class. The new one that can reach existing class members is called the derived class, or child class.

The existing class is referred to as base class or parent class.The derived class can take advantageof the base class data members and member functions. It may also have members and member functions of its own data type. Consequently a derived class can be even larger than a base class.

inheritance in C++

A relation between the derived class and the base class is shown in the figure above. The arrow head points to the base classes A and B. The arrow head means that the derived class will access all the members of the base class. But the base classes cannot access the derived class data/members. The derived class has only one individual member,

inheritance in C++

In the figure shown above, the derived class has only one member of its own. The two members are shown enclosed in the green rectangle are the members of the base class. The derived class can only access these two members of the base class. Thus, whereas an object of the base class can access only two members, an object of the derived class can access three members.

A new class can be derived from one or more existing classes. Based upon the number of base classes from which a class is derived, the inheritance is divided into two categories.

  • Single Inheritance in c++
  • Multiple Inheritance c++



Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Single Inheritance  in c++

In single inheritance, the new class is derived from only one base class.

Multiple Inheritance in c++

In multiple Inheritance, the new class is derived from more than one base classes.

Protected Access Specifier 

The public members of a class are accessible by all functions in the program and the private members of a class are accessible only by member functions and friend functions of that class. Similarly, the protected members of a class are accessible by the member functions and friend functions of that class.

The protected members of a base class are, however, accessible by members of its derived classes but the private members of the base class are not accessible directly by members of its derived classes. This is the main difference between the protected and the private access specifiers.

The protected members of a base class fall between private and public member. These members are public for the derived class but for the rest of program, these are treated as private.


Defining Derived Classes

The syntax used to describe a derived class varies significantly from the base class definition syntax. The declaration of a derived class also includes the name of the base class it derived from.

The following is the general syntax for defining a derived class is:

class sub_class_name : specifier base class_name

{

members of derived class

};

where

sub_class_name-------represents the name of the derived class

 :(colon)----------------- sets a relation between the classes

 specifier----------------represents the access specifier. It may be public, private or protected.

base_class_name ------represents the name of the base class.

For example, a class "student" is defined as.

class student

 private:

char name [15], address [15];

public:

 void input (void);

 void show(void);

The student class has two data members and two member functions Assume the marks received by a student in three different subjects are to be included as new data members in the above class and the cumulative marks of these subjects. This is done by introducing new class members. Those new members can be added to the class in two ways.

These are:

Add new members in the original class.

or

Create a new class that includes the new members and then incorporates them in the current student’s class. The concept of inheritance is to use the members of an existing class. The new class is the derived class. The original class serves as the base class for the derived class.

Deriving a new class from the existing class reduces the size of program. it also eliminates duplication of code within the program.

For example, let the name of the new class be marks. This class uses the members of the existing student class.

class marks: public student

 private:

int s1, s2, s3, total;

public:

void inputmarks (void);

void show_detail (void);

The class marks is derived from the class student. The class marks is the derived class. The class student is the base class.

The derived class marks has four data members of integer type and two member functions. It also uses the code of the base class student.

The derived class cannot directly access the private data members of the base class student by using the dot operator. These members are only accessible to the derived class through the interface functions within the base class. The program given below explains the above example.


Derived Classes example:

Example: write a program in which take the student marks and sum them and show the total marks on-screen using inheritance in c++

#include<iostream.h> 
#include<conio.h> 
class student 
{
private:
 char name [15], address [15]; 
void input (void)
{
 cout<<"Enter  the student name "; 
cin>>name, 
cout<<"Enter the student  Address";
 cin>>address;
}
 void show(void) 
{
clrscr();
 cout<<"Student Name: "<<name<<endl; 
cout<<"Student Address: "<<address<<endl; 
}
class marks : public student
{
 private:
 int eng, urd, math, total; 
public: 
void inputmarks (void)
{
 cout<<"Enter marks of  English"; 
cin>>eng; 
cout<<"Enter marks of Urdu "; 
cin>>urd; 
cout<<"Enter marks of  Math"; 
cin>>math;
total=eng+urd+math;
}
void show detail (void);
};
 main()
{
 marks m; 
clrscr(); 
m. input(); 
m.inputmarks();
 m. show detail(); 
getch(); 
}
void marks :: show_detail() 
{
show(); 
cout<<"Marks of  Englis : "<<eng<<endl; 
cout<"Marks of  Urdu: "<<urd<<endl;
 cout<<"Marks of  Math "<<math<<endl;
 cout<<"Total marks : "<<total<<endl: 
}

Programming explanation:

The class “marks” is defined as a derived class. The keyword “public” and the name of the base class “student” followed by colon(:) are written while defining the derived class. This shows that the objects of the derived class are able to access public members of the base class. It is called Public Inheritance.

The derived class “marks” can access the “input” & “show” member functions of the base class. It cannot access other private members of the base class.

An object “m” of class “marks” is created. The member function input of the class “student” is called through “m” object of class marks”. Similarly, the “show” function is also called in the “show_detail” member function of the class “marks” since the derived class “marks” has been declared as public of the student” class. The objects of the “marks” class can access only the public members of base class student.


 Types of Inheritances in c++

There are three kinds of inheritance in c++.

  • Public inheritance in c++.
  • Private inheritance in c++.
  • Protected inheritance in c++.

Public Inheritance in c++

In Public Inheritance, the public members of the base class become the public members of the derived class. thus the base class member(both data and function ) is publicly accessible for the object of the derived class. Similarly, the protected data members of the base class also become the protected members of the derived class.

The general syntax for deriving a public class from a base class is:

class sub_class_name  : public base_class_name

{

Expression;

},

where

public—————- specifies the public inheritance

sub_class_name—-represents the name of the derived class

base_class_name—represents the name of the base class

Example: write a program which explains the concept of public inheritance in c++:

#include<iostream.h> 
#include<conio.h> 



class A 
{
private: 
int al, a2; 
protected:
 int pa1, pa2; 
public: 
void ppp (void) 
{
cout<<"Value of pal of class A="<<pal<<endl; 
cout<<"Value of pa2 of class A="<<pa2 <<endl; 
}
};
class B : public A 
{
public: 
void get (void) 
{
cout<<"Enter value pal ? *; 
cin>>pa1;
cout<<"Enter value pa2 ? *;
 cin>>pa2; 
cout<<"Value of pal of class A="<<pal<<endl; 
cout<<"Value of pa2 of class A="<<pa2 <<endl; 
  }
};

main() 
{
B ob;
 clrscr();
 ob.get(); 
ob.ppp();
getch();
}
Programming explanation:

In the above program, the class B is derived as private from the base case A The objects of the class B:

  • cannot access the private data members al & a2 of the base class A.
  • can access the public function member ppp of the base class A.
  • can access the protected data members pal & pa2 of  the base class A.



Private Inheritance in c++

 In Private Inheritance, the objects of the derived class cannot access the public members of the base class. Its objects can only access the protected data members of the base class.

 The general syntax for deriving a private class from a base class is:

class sub class_name : private base_class_name.

{

Expressions;

}

 where

private —————specifies private inheritance

sub_class_name —-represents the name of the derived class

 base class_name—-represents the name of the base class

Example: write a program which explains the concept of private inheritance in c++:

#include<iostream.h>
#include< conio.h> 
class A 
{
private: 
int al, a2; 
protected:
 int pa1, pa2; 
public: 
void ppp (void) 
{
cout<<"Value of pal of class A="<<pal<<endl; 
cout<<"Value of pa2 of class A="<<pa2 <<endl; 
}
};
class B : private A 
{
public: 
void get (void) 
{
cout<<"Enter value pal ? *; 
cin>>pa1;
cout<<"Enter value pa2 ? *;
 cin>>pa2; 
cout<<"Value of pal of class A="<<pal<<endl; 
cout<<"Value of pa2 of class A="<<pa2 <<endl; 
  }
};

main() 
{
B ob;
 clrscr();
 ob.get(); 

getch();
}
Programming explanation:

In the above program, class B is derived as private for the base class A. The objects of class B:

  • cannot access the private data members al & a2 of base class A.
  • cannot access the public function member ppp of basc class A.
  • can only access the protected data members pal & pa2 of base class A.


Protected Inheritance in c++

The object of the class that is derived as protected can access only the protected member of the base class.

The general syntax for deriving a protected class from basc class is:

class sub_class_name : protected base_class_name

{

Expressions;

};

where

protected—————–specifics protected inheritance

sub_class_name——–represents the name of the derived class

base_class_name——-represents the name of the base class

Example: write a program which explains the concept of protected inheritance in c++:

#include<iostream.h>
#include< conio.h> 
class A 
{
private: 
int al, a2; 
protected:
 int pa1, pa2; 
public: 
void ppp (void) 
{
cout<<"Value of pal of class A="<<pal<<endl; 
cout<<"Value of pa2 of class A="<<pa2 <<endl; 
}
};
class B : protected A 
{
public: 
void get (void) 
{
cout<<"Enter value pal ? *; 
cin>>pa1;
cout<<"Enter value pa2 ? *;
 cin>>pa2; 
cout<<"Value of pal of class A="<<pal<<endl; 
cout<<"Value of pa2 of class A="<<pa2 <<endl; 
  }
};

main() 
{
B ob;
 clrscr();
 ob.get(); 

getch();
}
Programming explanation:

In the above program, the class B is derived as protected from the base class A. The objects of class B:

  • can only access the protected data members pal & pa2 of base class A

Derived Class Constructors

In inheritance, a constructor of the derived class as well the constructor functions of the base class are automatically executed when an object of the derived class is created.

The following example explains the concept of execution of constructor functions in single inheritance.


Simple program example of constructor functions in single inheritance in c++

#include<iostream.h>
#include<coni.h>
class BB
{
public: 
 BB (void)
{
 cout<<” Constructor of Base Class"<<endl; 
}
};
class DD : public BB 
public:
 DD (void)
{ 
cout<<"Constructor of Derived Class"<<endl;
} 
}; 
main()
{
 DD abc; 
getch();
}
 Output of Program 
Constructor of Base Class
 Constructor of Derived Class 

In the above program, when an object of the derived class created, the constructors of both the derived class as well as the base class are executed.

Constructors in Single Inheritance in c++ with Arguments

 In the case of constructor functions with arguments, the syntax to define constructor of the derived class is different. To execute a constructor of the base class that has arguments through the derived class constructor, the derived class constructor is defined as:

  • write the name of the constructor function of the derived class with parameter.
  • place a colon immediately after this and then write the name of the constructor function of the base class with parameters.

Example: how to write a constructor function with arguments in single inheritance in c++:

#include<iostream.h>
#include<conio.h>
#include<string.h>
Class student
{
   Private:
    Char name[15], address[15];
Public:
    Student (char nm[], char adr[])
        {

            Strcpy(name, nm);
            Strcpy(address, adr);       
    
}
Void show(void)
{
Clrscr();
Cout<<”Name : ”<<name<<endl;
Cout<<”Address: ”<<address<<endl;

}
};

Class marks: public student
{
Private:
Int eng,urd,math,total;
Public:
Marks(char nm[], char adr[], int e, int u, int m) : student(nm, adr)
{
Eng=e;
Urd=u;
Math=m;
Total=e+u+m;
}
Void show_detail(void);

};

Main()
{
Marks mm(“john”,”USA”,50,80,60);
Clrscr();
mm.show_detail();
}
Void marks :: show_detail()
{
Show();
Cout<<”marks of  English is : ”<<eng<<endl;
Cout<<”marks of  urdu is : ”<<urd<<endl;
Cout<<”marks of  Math is : ”<<math<<endl;
Cout<<”total marks   : ”<<total<<endl;
}


Multiple Inheritance in c++

 In multiple inheritance in c++, a class is derived by using more than one classes, In multiple inheritance, the derived class receives the members of two or more base classes. Multiple inheritance in c++ is a powerful feature of Object-Oriented Programming. This technique reduces the program size and saves the programmer’s time. The programmer uses the existing base classes in the program coding to solve problems, The figure shown below illustrates the relation of the derived class

inheritance in c++

In the figure class C is derived from two base classes A & B The syntax of multiple inheritance in c++ is similar to that of single inheritance  class the name of base classes  are written separated by commas(,)

 The general syntax is:

{

Expressions;

}

 where:

 sp1——specifies the access specifier of the first base class

 sp2——- specifies the access specifier of the second base class



Example: how to write a program in multiple inheritance in c++:

#include<iostream.h> 
#include<conio.h>
#include<string.h>
class student 
{
private: 
char name [15], address [15]; 
public:
 void input (void) 
{
cout<<"Enter name ? "; 
cin>>name;
 cout<<"Enter address ? “;
 cin>>address;
}
 void print (void)
{ 
cout<<"Name: "<name<<endl; 
cout<<"Address: “<<address<<endl;
}
};
 class marks 
private:
int eng, urd , math, total;
 public: 
void inputmarks (void)
{ 
cout<<"Enter marks of  English ? “;
 cin>>eng
 cout<<"Enter marks of  Urdu”; 
cin>>Urdu;
 cout<<"Enter marks of  Math ";
 cin>>math;
 total=eng+urd +math;
}
 void showmarks (void)
{ 
cout<<"Marks of  English: "<<eng<<endl; 
cout<<"Marks of  Urdu: "<<urd<<endl;
 cout<<"Marks of 3rd Math: "<<math<<endl; 
cout<<"Total marks: “<<total<<endl;
}
};

class show: public student, public marks 
{
public: 
show_rec()
{ 
cout<<"\nStudent detail marks sheet is :"<<endl;
 cout<<"====================="<<endl; 
print(); 
showmarks(); 
}
};
 main()
{ 
Show  mo; 
clrscr(); 
mo.input ();
 mo.inputmarks(); 
mo.show_rec();
 getch();
}

Constructors in Multiple Inheritance in c++

When a class is derived from more than one base class, the constructor of the derived class as well as of all its base classes are executed when an object of the derived class is created. If the constructor functions have no parameters then first the constructor functions of the base classes are executed and then the constructor function of the derived class is executed. A program is given below to explain the execution of the constructor without parameters.

Simple program example of Constructors in Multiple Inheritance in c++:

#include<iostream.h>
#include<conio.h>
Class aaa
{
Public:
aaa()
{
Cout<<”class aaa”<<endl;
}
};
Class bbb

{
Public:
Bbb()
{
Cout<<”class bbb”<<endl;
}
};
Class c : public aaa , public bb
{
Public:
Ccc()
{

Cout<<”Class ccc”<<endl;
}
};

Main()
{
Ccc obj;
}

Output:
Class aaa
Class bbb
Class ccc



Programming explanation:

In the above program, three classes are defined. The eee class is publicly derived from two classes aaa & bbb. All the classes have constructor functions. When the object of the derived class ccc is created,

the constructor functions are executed in the following sequence:
• the constructor of class aaa is executed first.
• the constructor of class bbb is executed second.
the constructor of derived class ccc is executed in the end.

Constructors in Multiple Inheritance in c++ with Arguments

To execute constructors of base classes that have arguments through the derived constructor functions, the derived class constructor is defined
as:
• write the constructor function of the derived class with parameters.
place a colon (:) immediately after this and then write the
constructor function names of base classes with parameters
separated by commas.


Example: how to write a constructor function with arguments in multiple inheritance in  c++:

#include<iostream.h>
#include<conio.h>
#include<string.h>
Class student
{
   Private:
    Char name[15], address[15];
Public:
    Student (char nm[], char adr[])
        {

            Strcpy(name, nm);
            Strcpy(address, adr);       
    
}
Void print(void)
{
Clrscr();
Cout<<”Name : ”<<name<<endl;
Cout<<”Address: ”<<address<<endl;

}
};

Class marks:
{
Private:
Int eng,urd,math,total;
Public:
Marks( int e, int u, int m) 
{
Eng=e;
Urd=u;
Math=m;
Total=e+u+m;
}
Void show_marks(void);
{
cout<<"Marks of  English: "<<eng<<endl; 
cout<<"Marks of  Urdu: "<<urd<<endl;
 cout<<"Marks of  Math: "<<math<<endl; 
cout<<"Total marks: “<<total<<endl;


}
};

Class show: public student, public marks
{
Public:
Show(char nm[], char addr[],int eeng, int uurd, int mmath,): student(nm,addr), marks(eeng, uurd,mmath)
{
Print();
Showmarks();
}
};


Main()
{
Clrscr();

show mm(“khan”,”turkey”,89,66,50);
}

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

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

Back to top button