Object-Oriented Programming binds together the data and the methods in the form of an object and selectively exposes the data to other objects. It primarily revolves around classes and objects - definition, instantiation, relationship, communication, etc. On the other hand, the building block of procedural programming is procedures or functions that perform data operations. Explore and Think.
OOPS brings the concept of class, a collection of data and methods that relies on the operation of an object. The idea of class and object brings the dynamics within a code and, most importantly, makes the code reusable, unlike procedural language. OOPS is important in modern software development as it introduces many features such as Inheritance, Encapsulation, Abstraction, Polymorphism.
Why the idea of "class" is essential in OOPs? Here is an analogy to understand - The world around us has a lot more structure than just numbers, strings, etc. There are cars, people, trees, all kinds of things that are the structure involved in real-world design. So if we don't want to be limited, then: how can we group values in a meaningful fashion to define such structure and define methods to use them?
The idea is simple - If we want to solve real-world problems, we'd like to mimic the real world's structure with values that exhibit the same structure. The idea of "Class" helps us define structures similar to the real world and build scalable and reusable software. A class definition provides us two main capabilities:
Objects are instances of classes created with specific data, where a class is an abstract blueprint used to create more specific, concrete objects. Each object contains data and methods to manipulate the data.
On large-scale software projects, we often need to take care of the accessibility and readability of our data. To achieve this, we use the concept of encapsulation.
On large-scale software projects that involve millions of code lines, we often need to re-use existing code or extend functionalities according to requirements. To achieve this, we use the concept of Inheritance.
Inheritance is a process in which one object acquires all the properties and behaviors of its parent object.
Why is polymorphism important in OOPS? Here are some critical reasons to think and explore:
Function overloading in C++
Function overloading is a C++ programming feature that allows us to have more than one function having the same name but a different parameter list. It means the data type and sequence of the parameters, 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 the code readability and allows 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.
Operator overloading in C++
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.
C++ 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 to perform some specific computation when the operator is used on objects of that class.
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.
In an object-oriented language, data abstraction is implemented through classes that define properties of an object like data and functions. OOPS has the mechanism of using class functions through related objects without going into details about how the functions are written. Abstraction makes the modifications easier as the interface to access the function remains the same. For example, if the type and working of the car engine change, it does not change the driving process.
Advantages of data abstraction:
Get well-designed application and interview centirc content on ds-algorithms, machine learning, system design and oops. Content will be delivered weekly.