C++ is an object-oriented language which is developed by Bjarne Stroustrup. All the concepts of the object-oriented approach are very interesting. But sometimes we have problems understanding the concept and many confusion occurs when we try to understand it. Let’s see all of the object-oriented concepts introduced in C++ with real-world examples which provide ease to understand the concepts of C++.
Object-oriented C++
C++ is not a pure object-oriented language because we can write the code without the classes. It is object-oriented because it supports the features provided by the object-oriented approach so it is a multi-paradigm language.
Important Concepts of Object-oriented in C++
- Classes
- Objects
- Inheritance
- Encapsulation
- Abstraction
- Polymorphism
- Interface etc.
Do the above names look like some medicines’ names? Are you frightened after reading them? Don’t worry, I will be explaining them most easily. These are the main concepts that are supported by C++ to implement Object Oriented approach. All the topics are with a suitable example that is connected with the real world.
Classes
A class is a user-defined data type that includes data members and functions. It contains a similar type of object. It is considered as a blueprint of the object.
Example:
When we talk about birds then that is a class and when we talk about a particular bird like a sparrow then we are talking about the object.
Objects
These are the real-world entities. Who exists in the real world that called objects like pens, pencils, etc? And its class will be stationary.
Inheritance
Inheritance is the mechanism by which you can use the existing code. It is used to decrease the program’s length. It means you can use all the properties of a function or a complete class by inheriting it. It is based on the parent-child relationship. The existing class is considered as Parent and the new class is considered as child class. A child class can use the functions and variables of the parent class.
Syntax to inherit the existing class:
class classname: access_specifier classname
Different types of Inheritance:-
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
Example:
To understand the concept of inheritance, please look at the parent-child relationship in the real world. A child can use any of the properties of the parents, similarly, a child class can use all the properties of the parent class.
Encapsulation
Encapsulation is an important concept of OOP. It binds the functions and data together. It can be achieved by using classes with different access specifiers.
Example:
It is similar to a capsule. A capsule contains many of the ingredients and contains them in a single unit. So encapsulation means wrapping data and functions in a single unit.
Abstraction
Abstraction is used to hide the information which helps in avoiding the interference of any external. It hides the information which is not necessary for the user, but it performs its function as well. It is called data hiding.
Example:
For the concept of data hiding, you can look at the fan. You just know about the fan that it revolves and gives you the cooling without the knowledge of the internal working or mechanism of the fan, it is data hiding. It means when the user only wants functionality and is not interested in a mechanism or information that is not necessary for the user data hiding comes into use.
Polymorphism
Polymorphism is a combination of two words i.e. Poly and morphs. Poly means many and morph means forms. It is achieved by overloading and overriding either operator overloading/overriding or function overloading/overriding.
Different types of Polymorphism
There are two types of Polymorphism in C++:
- Compile-time Polymorphism (It is achieved by overloading)
- Run-time Polymorphism (It is achieved by overriding)
Example:
You can understand this concept by Switchboard. There are many buttons but each button performs different tasks. You can say it is polymorphism. All are the buttons but working is different, similar A function or operator has the same name but performs a different task.
Learning C++ from the best C++ institute in Rohini, Delhi is fun and its object-oriented concepts are really interesting. I tried to relate all the concepts with real life which help you to understand the requirements of these topics and inspire you to do an experiment to learn it perfectly. C++ is a good language, go for it.