Object-Oriented Programming (OOPS) Concept in C++

what are oops concepts in c++

OOPs, or Object-oriented programming is an approach or a programming pattern where the programs are structured around objects rather than functions and logic. It makes the data partitioned into two memory areas, i.e., data and functions, and helps make the code flexible and modular. Object-oriented programming mainly focuses on objects that are required to be manipulated. In OOPs, it can represent data as objects that have attributes and functions.

what you need object oriented programming ?

The earlier approaches to programming were not that good, and there were several limitations as well. Like in procedural-oriented programming, you cannot reuse the code again in the program, and there was the problem of global data access, and the approach couldn’t solve the real-world problems very well. In object-oriented programming, it is easy to maintain the code with the help of classes and objects. Using inheritance, there is code reusability, i.e., you don’t have to write the same code again and again, which increases the simplicity of the program. Concepts like encapsulation and abstraction provide data hiding as well.

Object

An Object can be defined as an entity that has a state and behavior, or in other words, anything that exists physically in the world is called an object. It can represent a dog, a person, a table, etc. An object means the combination of data and programs, which further represent an entity.

Classes

Class can be defined as a blueprint of the object. It is basically a collection of objects which act as building blocks. classes example A class contains data members (variables) and member functions. These member functions are used to manipulate the data members inside the class. data member(variable)

Abstraction

Abstraction helps in the data hiding process. It helps in displaying the essential features without showing the details or the functionality to the user. It avoids unnecessary information or irrelevant details and shows only that specific part which the user wants to see.

Encapsulation

The wrapping up of data and functions together in a single unit is known as encapsulation. It can be achieved by making the data members' scope private and the member function’s scope public to access these data members. Encapsulation makes the data non-accessible to the outside world.

Inheritance

Inheritance is the process in which two classes have an is-a relationship among each other and objects of one class acquire properties and features of the other class. The class which inherits the features is known as the child class, and the class whose features it inherited is called the parent class. For example, Class Vehicle is the parent class, and Class Bus, Car, and Bike are child classes. inheriance image

Polymorphism

Polymorphism means many forms. It is the ability to take more than one form. It is a feature that provides a function or an operator with more than one definition. It can be implemented using function overloading, operator overload, function overriding, virtual function. Let’s have a look at an example where we are implementing function overriding. inheriance eg As you can see in the above example, there are three classes. Class Animal is the parent class, Class Cheetah, which is the derived class, and Class Dolphin is again the derived class of Animal class. All three classes have a function of the same name, i.e., speed, but the definition of this speed function is different in all three classes. Inside the main function, firstly, you are invoking the speed function using the object of the parent class, then using the object of child class Cheetah, you are again invoking the function, and similarly, you are invoking the function using the object of dolphin class. Below is the output of the above program. You can see that when you call the function using the object of the parent class, it invokes the function of the parent class. But when you call the function using the object of the child class, it overrides the parent class function and prints the function of the child class.

Advantages of oops

There are various advantages of object-oriented programming. OOPs provide reusability to the code and extend the use of existing classes. In OOPs, it is easy to maintain code as there are classes and objects, which helps in making it easy to maintain rather than restructuring. It also helps in data hiding, keeping the data and information safe from leaking or getting exposed. Object-oriented programming is easy to implement.