Hello everyone, Today in this blog we will discuss what is object oriented programming concept in C++ and implementation of object oriented programming concept using C++.
There four basic pillar of object oriented programming in c++ are:
A class is a data type which is define by user that holds it’s own data member and member function. It can be access and use by creating an instance (object) of that class.
“A class is blueprint for an object”
It is a basic unit of object oriented programming and represents the real life entities like chair, bike, marker etc. An object is an instance of a class.
Defining the class and declaration of object:
Syntax of object:
Basically it is the process of combining of data member and member function into a single entity called class. Moreover it is used to prevent the access of the data directly. Somehow if you want to access the data you have to access it through function of that class.
For example : capsule, it is wrap with different medicines. So this is what we do in encapsulation.
Code implementation of encapsulation:
First of all we will know about the origin of word polymorphism. However polymorphism derived from two word “Poly” and “Morphs”. Where poly means many and morphs means forms. It is the ability to perform two or more than two different task from the same name function within a same class.
Example : A person at the same time can be a father, son, brother, employee etc.
Types of Polymorphism:
Since it is a compile time polymorphism in which name of the function should be the same. However the arguments should be different. Above all in function overloading we can define function with the help of different types of arguments or a different number of arguments. Therefore with the help of these differences compiler can differentiate between the functions.
Code implementation of function overloading:
Operator overloading is the process of making an operator to exhibit different behaviours in different instances. In other words we can say that the operator is overloaded to give special meaning in user-defined class. Since, it is using a single function name to perform different types of tasks.
For example: The operator “+” we can overload it in a class like String in order that we are can concatenate two strings by just using +.
Code implementation of operator overloading:
O utput: The count is: 10
Since it is run time polymorphism which provide a feature that allows us to have a same function in child class which is already present in the parent class. However, the child class inherits all the data members, and the member functions present in the parent class.
In conclusion if we talk about the advantage of function overriding. It helps in saving the memory space, maintain consistency, and readability of our code.
Code implementation of function overriding:
Output: Derived class
As inheritance is the most important feature of object oriented programming concept. Since, inheritance is a process in which one object acquires all the properties and behaviours of its parent object automatically.
In addition if we talk about the advantages of inheritance is it can increase the code reusability.
Fox example: In father-son relationship, Son inherit the physical property of father.
There are total five types of inheritance in c++:
- Single Inheritance: Single inheritance is the inheritance in which a child class is inherited from the only one parent class.
- Multi-Level Inheritance: Multiple inheritance is the process of deriving a new class that inherits the attributes from two or more classes.
- Multiple Inheritance: Multiple inheritance is the process of deriving new class that inherit the property of two or more classes.
- Hierarchical Inheritance: Hierarchical inheritance is a type of inheritance in which more than one sub classes is inherited by the single base class. It is known as hierarchical Inheritance.
- Hybrid Inheritance: Hybrid inheritance is a combination of two or more type of inheritance.
For example: Let us Combine two inheritances i.e Hierarchical inheritance and Multiple Inheritance as show in the picture below.
Since abstraction is also the important feature of object oriented programming concept as it provide data hiding feature. In the other words, It is the property by which we can hide the complete implementation of a program from outside world and show only the relevant information to them.It is use to provide security for the data from an unauthorised access.
For ex: ATM machine: All the performance in the atm machine like cash withdrawl, money transfer, bank statement, etc. But we don’t know the internal working of the ATM.
Advantages of abstraction:
- It avoid the duplication of code and increase the code reusability.
- It help user to avoid writing of low level code.
Code implementation of abstraction:
a = 10
b = 20
In conclusion, In this blog we have learnt about the concept of object oriented programming in C++ language. As we also implemented code for better understanding and talk about real life example as well.
I hope you have liked my blog. If you have any suggestion to make then please drop a comment.
You can also refer to the documentation: https://devdocs.io/cpp/