![]() |
VOOZH | about |
Object-Oriented Programming (OOP) is a programming paradigm that organizes programs using classes and objects. It helps developers create modular, reusable, and maintainable applications by modeling real-world entities in code.
Before Object-Oriented Programming (OOP), most programs followed a procedural approach where the focus was mainly on functions and step-by-step instructions. As programs became larger, managing and reusing code became difficult.
OOP in C++ was introduced to solve this problem by organizing programs into classes and objects, making applications easier to understand, reuse, and maintain.
Object-Oriented Programming in C++ is built on concepts like classes, objects, inheritance, encapsulation, abstraction, and polymorphism that help in designing modular and reusable programs.
A class is a user-defined blueprint used to create objects. It defines the data members and member functions that objects of the class will have. Using classes, multiple objects with similar properties and behavior can be created without rewriting code repeatedly.
Components of a Class
Geeksforgeeks
Explanation: In this example, Student is a class and s1 is an object of that class. The object accesses the data member name and calls the member function display().
An Object is a basic unit of Object-Oriented Programming that represents real-life entities. A typical C++ program creates many objects, which interact with each other by invoking methods. The objects are what perform your code, they are the part of your code visible to the user. An object mainly consists of:
Employee: Geek Salary: 10000
Explanation: Here, emp is an object of the Employee class. The constructor initializes object data, and the member function displays employee details.
Abstraction in C++ is the process of hiding the implementation details and only showing the essential details or features to the user. It allows to focus on what an object does rather than how it does it. In C++ abstraction is achieved using abstract classes (classes that have at least one pure virtual function).
Drawing Circle
Explanation
Encapsulation is the process of bundling data and methods into a single unit (class) and restricting direct access to some of its components. It acts as a protective shield for the data.
90
Explanation: The data member marks is private and can only be accessed using public member functions.
Inheritance is a mechanism in C++ where a class (derived) acquires the properties and behaviors of another class (base), forming an "is-a" relationship.
Animal makes sound
Explanation: The Dog class inherits the sound() function from the Animal class.
The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity can take many forms. In C++, polymorphism allows the same method or object to behave differently based on the context, specially on the project's actual runtime class.
Drawing Circle
Explanation: The same function draw() behaves differently depending on the object type.
Object-Oriented Programming provides several advantages compared to procedural programming.
Object-Oriented Programming may not be ideal in the following situations: