Inheritance is a mechanism in which one object acquires all the properties and behaviours of the parent object.
Introduction to C++ Inheritance
The word "inherit" means to receive. In software inheritance, a new class is written such that it can access or use members of existing class. The new class that can access existing class is known as subclass or derived class (also called child class). On the other hand, the existing class that is reused is called the super class or base class (also known as parent class).Inheritance is the most important feature of object oriented programming that allows one data type to acquire properties of other data types. It is a form of software reuse ability in which code of existing classes is used for making new classes.
Inheritance from a base class may be declared as public, protected, or private.
In the above diagram, arrow is drawn from derived class to base class. The direction of the arrow indicates that derived class can access members of the base class but the base class can't access the members of its derived class. The derived class inherits all the capabilities of the base class (it can use member functions and data members of base class). It can also have its own data members and member functions. The derived class can be larger than its base class.
Categories of Inheritance
A new class can be derived from one or more existing classes based upon the number of base classes from which a class can be derived. The inheritance is divided into two categories:- Single Inheritance
- Multiple Inheritance
Inheritance Accessibility
The accessibility of members of the base class can be controlled by using specifiers. The access specifier determines whether unrelated and derived classes can access the inherited. These specifiers are used based upon accessing members of the base class, the inheritance accessibility is divided into three types:- Public Inheritance
- Private Inheritance
- Protected Inheritance
Declaration of Derived Class Using Single Inheritance
When a derived class is defined, the base class is also connected with new derived class. There is slightly difference between declaring a derived class and base class.The general syntax to declare derived class is:
class d_class : spr b_classWhere:
{
body of the derived class
};
d_class | It represents the name of derived class. |
: (colon) | The colon indicates that the d_class is derived from the b_class. It indicates the relationship between derived class and the base class. |
spr | It represents the access specifier that can be public, private or protected. |
b_class | It represents the name of base class that will be inherited. |
class CodeHimTo derive a new class "X" from existing class CodeHim (base class). The syntax are:
{
body of class CodeHim
};
class X : public CodeHimIn the above syntax, class X is derived from the existing class CodeHim. So the class X is called derived class and calss CodeHim called base class.
{
body of class X
};
In header of derived class declaration, the colon (:) indicates inheritance. Before the base class name, the keyword "public" indicates type of inheritance.
Inheritance Example Program
The following C++ program prepares simple result card of a student by using single inheritance.
#include
#include
class student_data
{
private:
int serial;
char name[30], phone[11];
public:
void input_dt (void);
void print_dt (void);
};
class student_card : public student_data
{
private:
float math, chem, phys, total, avg;
public:
void input_marks (void);
void result_card (void);
};
main ()
{
student_card res;
clrscr();
res . input_dt();
res . input_marks();
res . print_dt();
res . result_card();
getch();
}
void student_data : : input_dt ()
{
cout
cin>>serial;
cout
cin>>name;
cout
cin>>phone;
}
void student_data : : print_dt()
{
cout
cout
cout
cout
}
void student_card : : input_marks()
{
cout
cin>>math;
cout
cin>>chem;
cout
cin>>phys;
total = math+chem+phys;
avg = total /3;
}
void student_card : : result_card()
{
cout
cout
cout
cout
cout }