Why C++ is Object Oriented Programming Language?
Any programming language that makes use of objects, classes, added to its abstraction, encapsulation, and polymorphism are said to be Object-Oriented Programming Language or OOPs.
C++ also makes use of these concepts, but some of the above concepts are not fully optimized by C++ therefore it is sometimes also called partial Object-Oriented Programming Language.
We will see why it is called partially object-oriented but first let us understand what other terms are.
What is a class in a programming language?
A class is the blueprint of anything on which you want to work, for example, suppose you are the head of the country and you want to tell your people that they can start manufacturing automobiles industries, then since you are the head of the country you need to declare some rules and guidelines for these automobile manufacturing industries to follow.
Now suppose your rules might be like this that car must have 4 doors, it must have 2 headlights, it must only contain only a diesel engine and no petrol engines.
Now then all the car manufacturing industries in your country must have to follow these rules to work in your country.
Now let us assume you want to start the car manufacturing industry and name it speed_on_wheels. And your friend also wants to start his own car manufacturing company and he names it Auto_engineers and so on too many people created their own industries.
Now here the head of the country will declare a class name Car and the things which we discussed that it should have 4 doors, 2 headlights, diesel engine and so. All these will come under that Car class as a method. Till this is class.
What is Object in a Programming language?
As discussed earlier you wanted to create your own car manufacturing industries and you name it speed_on_wheels. So now you will create another class naming speed_on_wheels which will still follow the methods of the Car class created by the head of the country.
Now you start manufacturing and you named your first model SOW1.0 and I (the writer of this article) bought this car model from you and eventually, million other peoples bought this same model from your company.
In this case, the new class is speed_on_wheels and object SOW1.0 because it has been created from your class likewise you can have too many different models of cars and each model will be a unique object. But when I buy a car from you it became an instance for me irrespective of millions of same cars being out there in the market so when other people say harry buys the same mode SOW1.0 it becomes another instance for him.
What is inheritance in a programming language?
Inheritance simply means reusability of code, meaning you write one code and someone encounters a similar situation where he needs to write the same code like you so despite writing the same code, he/she must use your code instead. Example
In the above scenario, the head of the country has already declared a class called Car with methods in it, now every other manufacturing company in the country simply has to do is reuse his class that is extend his speed_on_wheels class so it can access Car class’s methods and functionality.
In this way, the code from the Car class is inherited into the speed_on_wheels class.
What is encapsulation in a programming language?
Now in the Car class, we told that there must only have a diesel engine, now as head of the country you don’t want to change this thing or don’t want anyone from outside that is car manufacturing company to change, so what you do is you protect those methods by writing certain keywords based on your programming language. So now other class members cannot change the method of the diesel class.
What is polymorphism in a programming language?
Polymorphism means ‘the same form’. This concept comes into the picture when we have too many classes and all those perform the same operations in some way or the other. Meaning we can have one method from a class and change it a little bit makes our own new working method example.
In the above Car class, we told cars must have 4 doors so all other classes like speed_on_wheels inherited this method into their class and they must follow these methods but still, they can alter somethings like the color of the door or thickness, etc, this concept is known as polymorphism.
Why is C++ partially Object-Oriented Programming Language?
Every programming language starts execution from the main class but in C++ the main function that starts initiates the execution is outside the main class.
This is java class execution.
In this, the execution of code begins with the main class that is hello.
But in C++ we can write and execute a fully functional code even without declaring a single class because in C++ the execution begins from the main class which comes by default and outside any class.
This is C++ execution.
In this, the execution begins from the main class, and note that there is no class declared in this code even then C++ is capable of giving output.
This is the reason why C++ is also called partially object-oriented programming.