Learn Object-Oriented Programming in C++

Object Oriented Programming in C++
Object Oriented Programming in C++


Object-Oriented programming uses objects in programming which are entities holding some characteristics and behavior. The aim of OOPs is to put together both the data and the function so that the data can be accessed by that function only.

It refers to combining data and function that manipulates the data into a capsule. So, the data gets hidden from being accessed directly from outside the class.

A real-life example of encapsulation is a washing machine. When we turn it off the machine stops and when we turn it on, the machine starts again. We do not know what mechanism is
involved in it. Similarly, we keep the data members hidden i.e. we keep the characteristics of
the object hidden but the member functions are public so the data can be accessed using those
functions only.

A class is an example of encapsulation: If we create some class and have data member and member function then it is an example of encapsulation. Let us understand this using a code snippet:

The variable x which is the data is made private. The variable x can only be accessed by using the functions getX() and setX() which are present inside the class and are public. Thus, the variable x and the functions getX() and setX() are combined together which is nothing but encapsulation. Encapsulation leads to data hiding since encapsulation also hides the data.

Data Hiding
It is an OOP concept which hides the data from user so that accidental changes are avoided. It
maintains the integrity of the data. Data hiding is an advance function of encapsulation as not only do you want the data to be accessed by the user, but you also want to hide the data from the user.

Let us understand the concept of data hiding using a code snippet:

We have made the data as private because we don’t want accidental changes. We’re giving the interface or function to the user to change this width and height using setHeight, setWidth class member function, this way we can impose a restriction on the parameter that it cannot be negative or zero. So, according to this code, you can only pass positive values for width and height and you can also display the message in case of negative values when entered by the user.

The word ‘polymorphism’ means having multiple forms. Similarly, in OOPs, the concept of polymorphism allows an entity like a variable, a function or an object to be of many forms. A real-life example of polymorphism is can be a human being. For example, a man is a son to his mom, a husband to his wife, a father to his kids and an employee at his office.

Compile-Time Polymorphism
Sometimes there are functions that have the same type and number of arguments called overloaded functions. When the code gets compiled, the overloaded functions are known by the compiler, therefore, compiler picks the appropriate function at the compile time. Thus, it is known as compile-time polymorphism. It is attained by function overloading and operator overloading (also known as static binding or early binding).

Function Overloading: It is an example of compile-time polymorphism where more than one function has the same name of the functions. Why function overloading? Reason being sometimes we want function names similar but want to get different behaviour by depending on the parameters we’re passing to the function. Let us understand this using a code snippet. Let’s suppose we have two functions add and another is also added but the parameters are different. One is taking integer x, y and other is taking double x, y then we say the function is overloaded.

Operator Overloading: In C++ it is possible to change the behaviour of operators. (+, -, *…), but we can change the behaviour for user-defined types (class, struct) only. Let us understand this using a code snippet. Supposing we have a requirement of user-defined data in which the user wants to store coordinates (x, y). So, we’ll fill this requirement either by class or by struct and then we want to add these points.

Run-time Polymorphism
Run time polymorphism is attained when the object’s function is invoked at the run time instead of compile time. It is achieved by method overriding (also known as dynamic binding or late binding).

It is a concept in OOPs in which a class acquires all the properties of its parent class. When the properties of an existing class are inherited by a new class, the new class can reuse the function and data members of the parent class. Moreover, new functions and data members can also be added to it.

Inheritance represents a is-a relationship. It is mainly used for code reusability. A real-life example of inheritance is a phone. The first discovered phone was only used for talking, then advanced version came which reused the concept of talking but had an extra function of messaging someone, then there were extra functions added in the new versions including games, music etc. Subclass/ Child class/ Derived class/ Extended class- A class which inherits the properties and behaviour of other class. Superclass/ Parent class/ Base class- A class from where the properties are inherited.

C++ supports five types of inheritance:
✔ Single Inheritance
✔ Multiple Inheritance
✔ Hierarchical Inheritance
✔ Multilevel Inheritance
✔ Hybrid Inheritance

Single Inheritance: In this type of inheritance, there is just one base class and one derived class. This is the simplest form of inheritance.

Multiple Inheritance: In this, a single derived class can inherit from two or more base classes.

Hierarchical Inheritance: Here, multiple derived classes get inherited from a single base class.

Multilevel Inheritance: In multilevel inheritance, the classes can be derived from the classes that are already derived.

Hybrid Inheritance: This is a mixture of some or all other types of inheritance.

To read more on C++, click here.

By Harshit Pradhan