Inheritance
The mechanism of creating a new classes from an existing classes is called inheritance. It is a property due to which object of one class can acquire the features of object of another class.
Inheritance is used to support the feature of code reusability. If there comes a situation in which need to write some program codes for more than one time then we can implement the concept of inheritance
Base Class and Derived Class
The existing class whose properties are inherited by other class are called Base Class.
The class that inherit the properties of base class are called Derived Class.
Defining Derived Class
Syntax:
class derived_class_name : access specifier base_class_name
{
//body of derived class
};
There are three different types of modes according to which a derived class can derive the features of base class:
i. Private Mode: Public and Protected members of base class will appear in Private section of derived class. It means the Data members and Member Functions of base class defined with Public or Protected access specifier will be inherited as Private member when gets inherited in derived class.
ii. Public Mode: Public member of base class will appear in Public section of derived class and Protected member of base class will appear in Protected section of derived class.
iii. Protected Mode: Both Public members and Protected members of base class will appear in Protected section of derived class.
Note: Private members of base class will never get inherited in derived class.
Types of Inheritance
i. Single-level Inheritance
ii. Multi-level Inheritance
iii. Multiple Inheritance
iv. Hierarchical Inheritance
v. Hybrid Inheritance
i. Single-level Inheritance: If a class is derived from only one base class then it is called Single-level Inheritance.
Example:
#include<iostream>
using namespace std;
class student
{
public:
int roll;
char name[50];
void set_val ( )
{
cout<<"Enter name and roll no. "<< endl ;
cin>> name >> roll ;
}
};
class exam : public student
{
public:
int mark;
void get_val ( )
{
cout<< "Enter mark " << endl ;
cin>>mark ;
}
void display ( )
{
cout<<"Name: "<<name<<"Roll: " <<roll<<"Mark: "<<mark<< endl;
}
};
int main ( )
{
exam s;
s.set_val ( );
s.get_val ( );
s.display ( );
return 0;
}
ii. Multi-level Inheritance: If a class is made from another derived class then it is called Multi-level Inheritance.
Syntax:
class child1_class_name : access specifier base_class_name
class child2_class_name : access specifier child1_class_name
iii. Multiple Inheritance: If a class is derived from multiple base class then it is called Multiple Inheritance.
Syntax:
class base1_class_name
class base2_class_name
class child_class_name : public base1_class_name : public base2_class_name
iv. Hierarchical Inheritance: If more than one class is inherited from a single base class then it is called Hierarchical Inheritance.
Syntax:
class base_class_name
class child1_class_name : public base_class_name
class child2_class_name : public base_class_name
v. Hybrid Inheritance: The mixture of two or more types of inheritance is called Hybrid Inheritance. It means if a single C++ program contains two or more than two types of inheritances mentioned above then such program will said to be have Hybrid Inheritance.
0 Comments