Encapsulation, Inheritance and Polymorphism In C++
Object oriented programming is a design philosophy. In programming paradigm, object oriented programming is the process by which a problem is decomposed into a number of entities called object and then data and functions are built around these objects. The main advantages of OOP are:
Encapsulation is one of the building blocks in OOP concept. It performs the operation of data hiding. It is a way to customize access to different portion of the data. Some data are hidden behind the access methods and some are open to all. But which data are open and which are hidden? How can we achieve this?
We can control the access on data by defining the access modifier. Three commonly used access modifier are public, private and protected.
Access specifier: Here is a list of different access modifier and their accessibility.
Public : To any member function in a class as well as to member objects.
Protected : To the class which has defined it and any derived classes.
Private : Only to the class which has defined it.
Let’s clarify the encapsulation concept with a C++ program. Consider the following program:
So, how can we access the private member of a class from the class object? We can do this by the creating public member function of that class. Member functions are the access point or interface by which a class object can access values of private members.
Consider the following example:
In general term, Inheritance is the process to inherit the properties or behavior from an existing instance. It is one of the most important building blocks in object oriented programming. In OOP, inheritance is the process of inheriting the properties and methods of an existing class and making a new class with some extra properties and methods.
Let’s understand with an example.
Consider a class, Player. A player has certain properties and behavior. Now we may want to more precisely define different types of players. So, for different game, we can create different player classes: Footballer, Cricketer, RugbyPlayer etc. Here, Player is the base class and Footballer, Cricketer, RugbyPlayer etc are the derived class from Player. These classes inherit the properties and behavior of the class Player and then add some specific properties and behavior of their own. The Cricketer class can further be inherited by other classes like: Bowler, Batsman, Wicketkeeper etc. Base class and derived class are also known as superclass and subclass.
Inheritance can be of many types.
When a derived class inherits properties and behaviors of only one base class, it is called single inheritance. Look at the following figure. Cricketer is a derived class from the base class Player.
When a derived class inherits properties and behaviors of more than one base class, it is called multiple inheritance. In following figure, AllRounder is a derived class from two base classes: Bowler and Batsman.
When properties and behaviors of one base class are inherited by more than one derived class, it is called hierarchical inheritance. In following figure, Bowler and Batsman are two derived classes from same base class Cricketer.
When properties and methods of a derived class are inherited by another class, it is called multilevel inheritance. In following figure, Cricketer is derived class from Player base class. Then Cricketer acts as base class for the Bowler class.
It is combination of multiple and multilevel inheritance.
Inheritance is of great use for re-usability and extensibility of a module. If we have already defined a class and we need another class with all the characteristics of that class along with some extra properties and behavior, we can use the concept of inheritance.
Consider the previous example. First we have thought of a class named Player. Then we need the Footballer,Cricketer, RugbyPlayer classes. If you analyze, you can get the point that every Footballer, Cricketer, RugbyPlayer are indeed a player. They must hold all the properties and behavior of a player. So, you don’t need to make the class from the scratch. Rather, you can extend the player class and add individual properties and behavior of a Footballer, Cricketer,RugbyPlayer in these class.
C++ syntax of inheritance of a class is:
The access level can be public, protected and private. Let us see how the access level differ in inheritance?
Public: Public and protected members of base class become public and protected members of derived class respectively, while private members of base class remain private to the base class only and members of the derived class cannot access the derived class.
Private: Public and protected members of base class become private members of derived class. Private members of base class remain private to base class members and can’t be accessed from derived class.
Protected: Public and protected members of base class become protected members of derived class. Private members of base class remain private to base class members and can’t be accessed from derived class.
A sample showing how inheritance works.
Polymorphism is another building block of object oriented programming. The philosophy that underlies is “one interface, multiple implementations.” It allows one interface to control access to a general class of actions. Polymorphism can be achieved both in compile time and run time.
Polymorphism through virtual function
Virtual function : While declaring a function as virtual, the keyword virtual must precede the signature of the function. Every derived class redefines the virtual function for its own need.
Uses of virtual function enable run time polymorphism. We can use base class pointer to point any derived class object. When a base class contains a virtual function and base class pointer points to a derived class object as well as the derived class has a redefinition of base class virtual function, then the determination of which version of that function will be called is made on run time. Different versions of the function are executed based on the different type of object that is pointed to.
The following example shows polymorphism through virtual function.
At first, pPl is assigned the address of p1, which is a base class object. If showInfo is now called using pPl, showInfo function of base class is executed. Next, pPl points to address derived class (Footballer & Cricketer) . If showInfo is called now, the redefined showInfo function of Footballer & Cricketer class are executed. The key point is, which version of the showInfo function will be executed depends on which object is currently pointed by base class pointer. This decision is taken in run time, so it is an example of a run time polymorphism. This type of runtime polymorphism using virtual function is achieved by the base class pointer.
One way of achieving polymorphism is function overloading. When two or more functions share the same name with different parameter list, then this procedure is called function overloading and the functions are called overloaded function.
The following example shows polymorphism using function overloading.
Encapsulation, Inheritance and Polymorphism are the three concepts which must be needed to know while approaching to object oriented programming. In this article, we tried to clarify the basic knowledge of these concepts. Hopefully, you have enjoyed it.
|All times are GMT +5.5. The time now is 13:28.|