Learning C/C++ Step-By-Step - Page 14
14. Step-by-Step C/C++ --- C++ Programming - Inheritance
Inheritance
Introduction Derived class and Base class Specifying the Derived Class Derived Class Constructors Access Specifiers Public Private Protected Access Specifiers without Inheritance Protected Access Specifier Scope of Access Specifiers Access Specifiers with Inheritance Types of Inheritance Single Inheritance Multiple Inheritances Multilevel Inheritance Hybrid Inheritance Hierarchy Inheritance |
Introduction
Inheritance is the most powerful feature of Object Oriented programming. Inheritance is the process of creating new classes, called derived classes from existing or bases classes. The derived class inherits all the capabilities of the base class but can add embellishments and refinements of its own.
A class, called the derived class, can inherit the features of another class, called the base class.
To inherit the qualities of base class to derived class is known as inheritance.
Its noun is heritage. We know in our daily lives, we use the concept of classes being derived into subclasses. For E.g. Vehicle is class it's again divided into Cycles, Bikes, Autos, trucks, busses and so on.
Here Vehicle is known as Base class and the derived items are known as derived classes or subclasses.
Generally every base class has a list of qualities and features. The main theme in this inheritance is to share all the common characteristics of base class to derived classes.
Inheritance has an important feature to allow reusability. One result of reusability is the ease of distributing class libraries. A programmer can use a class created another person or company, and, without modifying it, derive other classes from it that are suited to particular situations.
Derived class and Base class
A class, called the derived class, can inherit the features of another class, called the base class.
The derived class can add other features of its own, so it becomes a specialized version of the base class. Inheritance provides a powerful way to extend the capabilities of existing classes, and to design programs using hierarchical relationships.
Accessibility of base class members from derived classes and from objects of derived classes is an important issue. Objects of derived classes can access data or functions in the base class that are prefaced by the keyword protected from derived classes but not. Classes may be publicly privately derived from base classes. Objects of a publicly derived class can access public members of the base class, while objects of a privately derived class cannot.
Diagram shows how Derived class inherits.
A class can be derived from more than one base class. This is called multiple inheritances. A class can also be contained within another class.
Specifying the Derived Class
Class declaration is so easy using the keyword class as well as the derived class declaration is also easy but the class must be ends with its base class id and access specifier.
Syntax to declare a derived class:
Class <Class_name> : <Access_Specifier> <Base_Class_Name> ….
For. E.g. class result : public stud;
/* program to accept and display a student record */ |
Diagramed explanation for the above program
Derived Class Constructors
If a class is declared with its own constructors it is a base class of another. The derived class is also having its own constructors. If an object is declared which is the constructor will be executed? No doubt it executed the constructor of the derived class. It you still want to execute the constructor of Base class or both Derived and Base class constructors simply call the Base constructor in Derived class constructor.
/* Constructors in derived class */ |
ACCESS SPECIFIERS
Access specifiers are used to control, hide, secure the both data and member functions. Access specifiers are of 3 types
- Public Access Specifier
- Private Access Specifier
- Protected Access Specifier.
Public :
If a member or data is a public it can be used by any function with in class and its derived classes also. In C++ members of a struct or union are public by default. Public Member of a class can be inherited to the derived class when the class is inherited publicly but not the member functions(privately). |
Private :
Member functions and friend of the class in which it is declared can only use it. Members of a class are private by default. Private member of a class doesn’t be inherited to a derived class when the base class is inherited publicly or privately. It there is need we have to write member function, which are returns, those values. |
Protected :
It is access as the same as for private in addition, the member can be used by member functions and friends of classes derived from the declared class but not only in Objects of the derived type. The protected member of a class can be inherited to the next derived class only. But not to the later classes. |
Access Specifiers without Inheritance
More About Protected Access Specifier
To provide the functionality without modifying the class. Protected can accessed by it self and derived class-protected members only but in objects or the subderived class or the outside class.
Scope of Access Specifiers
Access Specifier | Accessible from Own class |
Accessible from derived class |
Accessible from Objects outside class |
Public | Yes | Yes | Yes |
Protected | Yes | Yes | No |
Private | Yes | No | No |
Access specifiers with Inheritance
Types of Inheritance
/* Program to demonstrate Multiple Inheritance */ |
If a base class is publicly inherited then the public members, member function can be accessible to the member functions of the derived class and to the Objects also where as If a base class is inherited privately then the public member of base class are inherited to the member functions of the derived class only but not to the objects.
/* A program to demonstrate Multilevel Inheritance */ |
/* Program to demonstrate Hybrid Inheritance */ |
Pictorial representation of the above program:
In the above figure student class inherited to result in two ways. One is via test another one is via sports then two sets of members, member functions of common base class student are inherited to the derived class result at the time of execution the system will get confuse to use what set of member functions of base class.
This can be avoided by making the common base class as virtual base class.
Eg:
class student { }; |
Ref: Object-oriented Programming in Turbo C++: Robert Lafore