Object Oriented Programming in C++
Did you know?
The C++ language is an evolution of the C language that was invented at the American Telephone and Telegraph Bell Laboratories, USA.
One major feature that was introduced in C++ after its advent was object-oriented programming. This feature was not present in the C programming language. Therefore, OOPS with C++ formed the stepping stone for the development of commercial and technical software.
Considering the importance of OOPS pioneering software innovations, every programmer needs to be familiar with its basics in C++. OOPS is considered a programming paradigm that revolves around the concept of objects. These objects can contain certain code and data elements.
Just like OOPS in Python, OOPS in C++ is also supported by 4 pillars — Encapsulation, Inheritance, Abstraction, and Polymorphism.
Let’s take a look at each of these pillars in detail.
- Encapsulation
In OOPS, encapsulation can be defined as bundling together the data elements and the methods that operate on them into one entity.
Let’s understand the term through a real-life example. Imagine a college wherein you have different departments like teaching departments, administrative departments, etc. Suppose you want to get access to some type of student data.
You will first have to contact a person in the administration department about it and then request him/her to provide you with the data. This is exactly what encapsulation is.
In this example, the student data under the administration department and the employees of the department are wrapped under one single section.
The advantages of encapsulation are as follows:
- Greater Flexibility: In OOPS in C++, you can set your variables in two formats — write-only and read.
- Ability to Reuse: You can easily adapt and change the data elements according to new requirements.
- Hiding Data Elements: Since data elements are encapsulated in single units along with their functions, the software users will not have any clue about how classes in the program are being stored or implemented.
Encapsulation in C++ helps you to write cleaner code by encapsulating related data elements and their methods together. It helps you to modify your data elements as you like.
- Inheritance
Inheritance in C++ can be defined as the ability of a class to draw its characteristics and properties from another class. It is a critical concept of object-oriented programming in C++.
If you know OOPS in Python then you must be familiar with the concepts like parent class and child class in inheritance. Here, new classes are derived from existing classes.
The pre-existing classes are known as the parent classes and the newly created classes are called derived classes. A derived class inherits all the properties of the parent class. The best part is that you can also add certain new features to the derived class.
Inheritance helps overcome the challenges of writing a code multiple times and dealing with the problems of redundancy. Continue Reading...
Comments
Post a Comment