Object Oriented Programming (OOPS) Concepts in C++

When objected-oriented programming first appeared in the 1980s, it was a radical leap from traditional top-down programming. It introduced new software development methods with features like Inheritance, Encapsulation, Abstraction, Polymorphism, etc. In simple words: It helped us organize code and thinking for large-scale software development.

OOPS binds together the data and the methods in form of an object and selectively exposes data to other objects. The idea revolves around classes and their definition, instantiation, relationship, communication, etc. On the other hand, the building block of procedural programming is procedures or methods that perform operations on data.

Object-oriented programming vs Procedural programming

Four pillers of oops in c++

Class: A blueprint of a real-life structure

OOPS allows us to combine methods and data into a specific group called classes. This idea brings data and methods together in a code and, most importantly, makes the code reusable. It allows us to represent the real-world entities through data and animate that representation through methods.

Why the idea of "class" is essential in OOPS? Here is an analogy to understand: The world around us has a lot more complex structure than just numbers, strings, etc. So we need a mechanism to group values in a meaningful fashion to define and use such structure. In other words, if we're going to solve real-world problems using software, we'd like to mimic the real world's structure with attributes and methods that exhibit the characteristics of that structure. So the idea of classes in oops helps us define structures similar to the real world and build scalable software.

Class in C++ code example

Object: An Instance of a Class

An object is an instance of a class created with specific data, whereas a class is an abstract blueprint used to create specific, concrete objects. So each object contains data and methods working on that data.

This approach allows us to access class variables to the methods of the class while at the same time they aren’t global. It gives us a lot of control of which parts of the program can access what, which could help in improving code clarity.

  • When a class is defined, no memory is allocated.
  • When an object is created, memory is allocated.
  • Objects interact by sending messages to one another.
  • Objects can also interact without knowing each other details.

Objects in c++ code example

Encapsulation: Hiding implementation details

When working with object-oriented programming languages, we know exactly where to look when something goes wrong. In other words, we don’t have to go line-by-line through all our code. That’s the beauty of encapsulation! Encapsulation helps us expose relevant details to the outside world and lets us change the implementation of the code without affecting any other code.

Objects are self-contained, and each functionality does its own thing while leaving the other alone. This modularity allows an IT team to work on multiple objects simultaneously while minimizing the chance that one person might duplicate someone else’s functionality. In other words, we often need to take care of the accessibility of our data in software projects. To achieve this, we use the concept of encapsulation!

  • Encapsulation hides implementation details of an object from the users and protects an object's data. Technically, the class data are hidden from other classes and can be accessed only through the member methods of the class in which it is declared.
  • Encapsulation combines data and methods to make our code cleaner and easy to read. In simple words, it provides an abstraction between an object and users of the object.
  • Encapsulation can be achieved by declaring all the class variables as private and writing public methods in the class to set and get the values of variables.

Encapsulation in c++ code example

Inheritance: Reusing code and extending functionalities

On large-scale software projects that involve millions of code lines, we often need to reuse existing code or extend functionalities according to requirements. To achieve this, we use the concept of Inheritance. It is a process in which one class acquires all the properties and behaviors of its parent class.

In Inheritance, we create one parent class and define subclasses to inherit the parent class traits. Each subclass could implement separate methods for itself, and at the same time, it can reuse the parent class's existing methods. Even if we change our parent class, all sub-classes will inherit the new code.

Inheritance in c++ code example

Polymorphism: Performing a single action in different ways

OOPS allows us to have many methods, all with the same name, doing a similar job on different data or data types. Suppose we need a few methods with similar traits in each subclass, but the parameters are different. This is where polymorphism comes into play.

We define a single method inside the parent class, and based on need, we separately define this common method for all other sub-classes.

  • Polymorphism presents the common interface to perform a single action in different ways. It is crucial for code reusability and extensibility.
  • Instead of complex conditional statements describing the different courses of action, we can create interchangeable objects that we select based on our needs. So one benefit of using polymorphism with inheritance is - code working with the different classes does not need to know which class it uses!

Polymorphism via Function Overloading

Function overloading allows us to have more than one function having the same name but a different parameter list or type. For example, the parameters list of a function myfun(int a, int b) is (int, float) which is different from the function myfun(float a, int b) parameter list (float, int).

The main advantage of function overloading is to improve code readability and allow code reusability. In the code below, we can see how we are able to have more than one function for the same task(addition) with different parameters. This allows us to add two integer numbers and three integer numbers, and if we want, we could have some more functions with the same name and four or five arguments. 

Function overloading in c++ code example

Polymorphism via Operator Overloading

Operator overloading means that the operation performed by the operator depends on the type of operands provided to the operator method. It is similar to function overloading, where we have many versions of the same function differentiated by their parameter lists.

OOPS lets us extend operator overloading to user-defined types (classes) i.e., a programmer can provide their own operator to a class by overloading the built-in operator and perform some specific computation when the operator is used on objects of that class.

Operator overloading in c++ code example

Operator overloading code example in c++

Abstraction: Exposing essential details

In an object-oriented language, abstraction is implemented through classes that define properties of an object like data and methods. It has the mechanism of using class methods through related objects without going into details about how the methods are written. So abstraction means providing only the essential or relevant information and hiding the background or technical implementation details. For example: to drive a car, one only needs to know the driving process and not the mechanics of the car engine.

Abstraction makes the modifications easier as the interface to access the method remains the same. For example, if the type and working of the car engine change, it does not change the driving process.

Abstraction in c++ code example

Advantages of OOPS

  • Makes code easy to maintain and modify.
  • Helps us write reusable code.
  • Provide data security and control data accessibility.
  • Provide a mechanism to support high code cohesion.
  • Provide a mechanism to support minimizing code coupling or dependency.
  • Improve code readability and ease in the documentation.
  • Simplify large-scale software development.

Additonal bolgs to explore

Enjoy learning, Enjoy OOPS!

More From EnjoyAlgorithms

Our weekly newsletter

Subscribe to get free weekly content on data structure and algorithms, machine learning, system design, oops design and mathematics.

Follow Us:

LinkedinMedium

© 2020 EnjoyAlgorithms Inc.

All rights reserved.