Learning Object-Oriented Programming in C++ ___Data Encapsulation & InheritancesteemCreated with Sketch.

in programming •  5 years ago 

Hello Beautiful people!

I hope everyone is fine. Today we are going to discuss Data Encapsulation and Inheritance In OOP(Object-Oriented Programming).

What is Data Encapsulation in OOP?

Data Encapsulation is an Object Oriented Programming concept that binds a group of related properties, functions, and other members are treated as a single unit. Class is the best example of Data Encapsulation. It sometimes referred to as data hiding that prevents the user to access the implementation details. Encapsulation, therefore, guarantees the integrity of the data contained in the Object.

What is Inheritance?

Inheritance is one of the core concepts of object-oriented programming (OOP) languages. It is a mechanism where you can derive a class from another class for a hierarchy of classes that share a set of attributes and methods.

You can use it to declare different kinds of exceptions, add custom logic to existing frameworks, and even map your domain model to a database.

Progam to understand the above Definitions.

To understand the above definition we need to write a simple program to understand the Encapsulation and Inheritance at the same time. As in the definition of Data Encapsulation it helps us protect our data from user and help us secure the information which we want to keep in secret. So we will write a program in which we will first declare some variable and function in public and then we will change them to protected than private and we will check what happens

image.png
I have declared a protected string type variable first we will access it from within the class and then outside of the class.

image.png

I am trying to call the variable within the body of the class.

image.png

Hence the variable is accessible within the class body. Now we will try to access the same variable from another class.

image.png
As we can see that the variable is not available from outside of the class. Therefore we can say that a protected variable is only available within the body of the class. how can we access the protected variable from another class? Well, it is pretty simple with the help of Inheritance we can do this kind of work. We will make the second class the child class of the first one. so let's do this.

image.png

As we can see that the protected variable will work in the body of the child class as well. We know that the public variables or public functions are available to every class so no need to try this. Let's change the access specifier of data members or variables to private and then check if it works.

image.png
Now let's run the program.

image.png

And it is telling us that the data member is private and we are accessing from outside of the class. Actually, private data members are not even accessible within the class body.

Ok, that much is enough for today we will discuss the inheritance in detail in the upcoming posts.

Take care til that.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!