C++ Inheritance

Inheritance is the ability of one class to acquire the properties of another class. The class legs inherits the properties of another class of Dogs, which have themselves inherited from other class Animal. Inheritance is one of the most important feature of Object Oriented Programming. The advantage of inheritance is code reusable.

Sub Class:A class that inherit the property of another class is known as a Sub class.

Super Class:A class whose property is inherited by another subclass is known as a Super class.

Syntax :

class subclass-name : visibility-mode base-class-name
{
//body of subclass
};


Subclass name :- Name of the subclass
Visibility-mode :- Mode in which you want to inherit this subclass.
Base-class-name :- Base class from which you want to inherit the subclass.


Example
#include <bits/stdc++.h> 
using namespace std; 
class emp
{
public:
int salary;
};
class dev : public emp
{
emp e;
public:
void salary()
{
cout<<"Enter employee salary: ";
cin>>e.salary;   // access base class data member
cout<<"Employee salary: "<< e.salary;
}
};
main()
{
dev obj;
obj.salary();
return 0;
}  
Output
Enter employee salary: 5000
Employee salary: 5000


Types of Inheritance in C++

Single Inheritance

Single inheritance is a type of inheritance which can inherit only one class, i.e. a sub class is inherited by only one base class.

Syntax :

class sub-class-name : visibility-mode base-class-name
{
//member of derived class
};


Example
#include <iostream> 
using namespace std; 
// base class 
class Animal { 
public: 
Animal() 
{ 
cout << "This is a Animal" << endl; 
} 
}; 
// sub class derived from two base classes 
class Dog: public Animal{ 
}; 
// main function 
int main() 
{    
// creating object of sub class will 
// invoke the constructor of base classes 
Dog obj; 
return 0; 
} 
Output
This is a Animal

Multiple Inheritance

Multiple inheritance is a type of inheritance which can inherit multiple classes, i.e. a sub class is inherited from more than one base classes.

Syntax :

class sub-class-name : visibility-mode base-class1, visibility-mode base-class2, ....
{
//member of derived class
};


Example
#include <iostream> 
using namespace std; 
// base class 
class Animal { 
public: 
Animal() 
{ 
cout << "This is a Animal" << endl; 
} 
}; 
class Mammal  { 
public: 
Mammal() 
{ 
cout << "This is a Mammal" << endl; 
} 
}; 
// sub class derived from two base classes 
class Dog: public Animal, public Mammal{ 
}; 
// main function 
int main() 
{    
Dog obj; 
return 0; 
} 
Output
This is a Animal
This is a Mammal

Multilevel Inheritance

Multiple inheritance is a type of inheritance in which sub-class-name is created from another sub-class.

Example

#include  
using namespace std; 
// base class 
class Animal  
{ 
public: 
Animal() 
{ 
cout << "This is a Animal" << endl; 
} 
}; 
class Mammal: public Animal 
{  public: 
Mammal() 
{ 
cout<<"Objects with 4 legs are Animal"<< endl; 
} 
}; 
// sub class derived from two base classes 
class Dog: public Mammal{ 
public: 
Dog() 
{ 
cout<<"Dog has 4 legs"<< endl; 
} 
}; 
// main function 
int main() 
{    
//creating object of sub class will 
//invoke the constructor of base classes 
Dog obj; 
return 0; 
}  
Output
This is a Animal
Objects with 4 legs are Animal
Dog has 4 legs

Hierarchical Inheritance

Hierarchical inheritance is a type of inheritance in which more than one sub-class-name is inherited from one base-class.

Example

#include  
using namespace std; 
// base class 
class Animal  
{ 
public: 
Animal() 
{ 
cout << "This is a Animal" << endl; 
} 
}; 
// first sub class  
class Dog: public Animal 
{ 
}; 
// second sub class 
class Lion: public Animal 
{ 
}; 
// main function 
int main() 
{    
// creating object of sub class will 
// invoke the constructor of base class 
Dog obj1; 
Lion obj2; 
return 0; 
} 
Output
This is a Animal
This is a Animal

Hybrid Inheritance

Hybrid inheritance is a type of inheritance in which more than one type of inheritance is being used.

Example

#include  
using namespace std; 
// base class  
class Animal  
{ 
public: 
Animal() 
{ 
cout << "This is a Animal" << endl; 
} 
}; 
//base class 
class Legs 
{ 
public: 
Legs() 
{ 
cout<<"Legs of Animal\n"; 
} 
}; 
// first sub class  
class Dog: public Animal 
{ 
}; 
// second sub class 
class Lion: public Animal, public Legs 
{ 
}; 
// main function 
int main() 
{    
// creating object of sub class will 
// invoke the constructor of base class 
Lion obj2; 
return 0; 
} 
Output
This is a Animal
Legs of Animal

Exercise:-

1. How many basic types of inheritance are provided as OOP feature?

A. 2
B. 3
C. 4
D. 5

View Answer


2. Which programming language doesn’t support multiple inheritance?

A. C++ and Java
B. C and C++
C. Java and SmallTalk
D. Java

View Answer



Program

C++ Program to Check Whether a Number can be Express as Sum of Two Prime Numbers

#include <iostream>
using namespace std;
bool checkPrime(int n);
int main()
{
int n, i;
bool flag = false;
cout << "Enter a positive  integer: ";
cin >> n;
for(i = 2; i <= n/2; ++i)
{
if (checkPrime(i))
{
if (checkPrime(n - i))
{
cout << n << " = " << i << " + " << n-i << endl;
flag = true;
}
}
}
if (!flag)
cout << n << " can't be expressed as sum of two prime numbers.";
return 0;
}
// Check prime number
bool checkPrime(int n)
{
int i;
bool isPrime = true;
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0)
{
isPrime = false;
break;
}
}
return isPrime;
}


Output
Enter a positive integer: 34
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17  



Visit :


Discussion



* You must be logged in to add comment.