Object-oriented programming is one of the most widely used methodologies in programming. It is an approach to programming that is more organized, extensible, and manageable.
C++ offers full support for object-oriented programming(OOP). Therefore, it is essential to get familiar with the concept of OOP before learning to implement it. Consequently, we will discuss the principles and features of OOP in this lesson to understand it well.
Concept of Objects
Everything in the real world can be considered as an object. It can be anything like a tree, a house, a car, etc. Moreover, these real objects can have numerous properties like color, density, material, size, volume, etc.
Properties or attributes are information that helps us to describe an object. Additionally, Some objects have behavior associated with them, such as starts( for a car), talks ( for a human), and so on.
Consider any car; It has a particular color, a make, a model, a price, etc., which are its properties. However, a car can also perform actions like starting its engine, accelerating, turning on headlights, and blowing a horn. These are not properties but actions that a car can perform.
These actions are also known as the behavior of an object.
The Object Model
The object-oriented model of programming tries to model the real world in terms of objects. It emphasizes organizing the data in the best way.
Every data is represented and modeled as an object, such as a person’s bank account, a book, or a product. All these objects contain two fundamental features: attributes (properties) and behavior(actions).
Properties provide the information and state of the object, while its behavior or action allows us to interact and communicate with the object itself.
Inside an object’s definition of C++,
- The properties are implemented using variables and are known as data members.
- The actions/behavior are implemented using functions and are known as member functions.
Features of Object-Oriented Programming
A vast community of programmers adopted object-oriented Programming due to its large number of benefits. Early computer programming was procedural, which followed the “top to bottom” approach of writing programs. It is still used today for writing simple programs but not feasible for any medium to large-scale software. It had a lot of flaws and limitations, which OOP overcomes.
The major features and principles of object-oriented programming are listed below.
An object always hides the implementation details. Thus, it provides a simplified interface for the user to interact with the object. Objects are defined so that their implementation of features and behavior is hidden from the outside world.
For example, suppose we have an object called “Shape,” and we need to find the area of a particular shape. In that case, it should be possible to do so without knowing the mathematical formula and implementation behind it.
Another example is operating a personal computer; it does not require knowing how the circuits and microprocessors work.
An object groups the data members and member functions into one single unit. Only through this unit, these object members can be accessed. This is how an object is represented as a single entity.
Encapsulation also makes an object more secure by allowing different levels of access and scope compared to the code outside the object.
Every object is an independent unit in itself with data and member functions. Each object can live in its file and then imported later to any program wherever required. These are also known as modules, which are parts of the main program.
Breaking a large program into modules makes it more reusable and easy to manage.
Inheritance is the act of creating a new object from the data and functions of another existing object.
For example, if we have a “Rectangle” object, we can simply inherit it to create a new object called “Square”. Since every square is a rectangle, it will have all the features from the rectangle object. This is inheritance.
Moreover, any change in the parent object is also reflected in the child object unless it overwrites the inherited data.
Inheritance makes the code reusable and extensible.
An object can behave in different ways based upon different situations.
Some examples of polymorphism in C++ include:
- Function overloading: The same function can be defined to behave differently for different combinations/types of arguments passed to it.
- Operator overloading: A similar operator can have multiple meanings depending on the context where it is used. For example, the
+sign can add two numbers or concatenate two strings/characters depending on where it is used.
- Inheritance: The same object is extended to add or give a different meaning.
Advantages of Object-Oriented Programming
In addition to the above 5 features of OOP, it comes with these advantages:
1. Reusability of code
Creating objects helps us reuse the member functions and their behavior in various places of our code.
2. Code maintenance & cost
OOP helps to achieve a clean structure of code. This makes the code easier to understand and debug.
Also, code maintenance is easier because each object is a separate module focused on a specific task. These all save a lot of development costs and time.
Points to Remember
- Object-Oriented Programming(OOP) tries to model the real world to provide a more accurate representation of data in programming.
- An object is composed of properties and actions/behavior.
- Properties or attributes describe an object. Actions represent the behavior of an object and its interaction with the outside world.
- In C++, properties are known as “data members,” and actions are known as “member functions”.
- It offers features like abstraction, encapsulation, modularity, inheritance, and polymorphism.
- OOP provides benefits like code reuse, easy maintenance and also saves a lot of time and money.
- All major languages in use today are object-oriented due to the great benefits.