Encapsulation in C++

One of the main objectives of Object Oriented Programming is to keep related things together. Encapsulation in C++ is about to bind the data and the functions that operate on them into a single entity called class.

In traditional procedural programming like C, the data and functions are sort of independent.

Code manageability can increase significantly if we bind the data and the related functions together.

Lets consider that we have two distinct set of objects – animals and cars. So, the animal related data and the functions can be kept in Animal class and the car related data and functions in Car class.

The Animal class can have data like – legs, tail, color, age, breed etc and functions like walk(), makeSound(), eat(), sleep() etc.

Whereas, the Car class can have data like – wheels, make, fuel type, fuel capacity etc and functions like, speedup(), slowdown(), applyBreak() etc.

It is much more convenient for the programmers if the car objects have access only to the car related data and functions and not the animal ones. Chances of making mistake will also be much lower.

Once the data and functions are encapsulated inside a class, the their accessibility can further be enhanced by access specifier and data abstraction.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
0
0
0
0
0