Encapsulation in C++

  • Encapsulation is simple concept which prevents the access to data that is not essential.
  • It is a process of binding the variable and methods together required by an object in a single unit that is class.
  • Encapsulation is a technique of hiding internal details of a class to prevent data to be accessed from the outside world.
  • When we combine the state and behavior into a container then it is called as encapsulation.
  • In C++, encapsulation is achieved through class concept.
  • We can understand this concept using following diagram:
  • cpp-program
  • The most popular and real time example of the encapsulation is capsule as shown above.

Advantage of Encapsulation:

  • It provides data security.
  • It provides the abstraction between objects and its clients.
  • It provides the security from the unwanted access.
  • The example of the encapsulation is as follows:
#include <iostream.h>

#include <conio.h>

class sum

{

private:

int a,b,c;

public:

void addition()

{

cout<<"Enter any two numbers: ";

cin>>a>>b;

c=a+b;

cout<<"Sum: "<
}

};

void main()

{

sum s;

clrscr();

s.addition();

getch();

}

In the above example, all the data members and functions are declared and defined or bind inside the class. Here we cannot access the class members directly. Here we can only access the elements of class using the object of the class.

Abstraction in C++:

  • Abstraction is the process of providing the essential features of an object without providing its background details or explanation.
  • Classes use the concept of abstraction that is why classes are known as abstract data type.
  • Abstraction is the process of just highlighting the set of services what we are offering and hiding the internal details.
  • For example, when we use ATM machine to withdraw money then there is only screen is displayed in-front of us. Here we have to enter the detail about pin and we enter into the account to get money; but here there is no internal details are shown to user such as which languages are used to develop the ATM system, how machine gets the input etc.
  • When we hide the data then it is called as data abstraction.
  • Abstraction provides the view which we want to use and does not display the internal functionality.
  • The following image shows the data abstraction:
  • cpp-program
  • The example is as follows:
  • #include <iostream.h>

    #include <conio.h>

    class sum

    {

    private:

    int a,b,c;

    public:

    void addition()

    {

    cout<<"Enter any two numbers: ";

    cina>>b;

    c=a+b;

    cout<<"Sum: "<
    }

    };

    void main()

    {

    sum s;

    clrscr();

    s.addition();

    getch();

    }

  • In the above example, all the data members and functions are declared and defined or bind inside the class. Here we cannot access the class members directly. Here we can only access the elements of class using the object of the class. Here we get only the addition but how it is performed not displayed to user.

Polymorphism in C++:

  • Polymorphism is the ability for classes to provide different implementation to the method that are called by same name.
  • Polymorphism is a generic term that means 'many shapes'. In C++ the simplest form of Polymorphism is overloading of functions and operators. This is known as function overloading and operator overloading respectively.
  • Polymorphism is a way to define the several same name methods in a class. It is called as polymorphism.
  • When we define two or more same name functions with different parameters or with different return type in same class then it is called as function overloading.
  • When we define two or more same name functions with same arguments and same return type in different classes then it is called as function overriding or method overriding.
  • We can study about function overloading and operator overloading in our chapter "C++ Operator Overloading".
  • The following diagram shows the polymorphism:
  • cpp-program

Method overriding: When we define any method with same name, same return type and with same arguments in the base class and child class then it is called as method overriding. When we implement this concept then we cannot guess which method will execute at the runtime because method names are same. The following figure shows this concept:

cpp-program

The example of method overriding is as follows:

#include <iostream.h>

#include <conio.h>

class Parent

{

public:

void show()

{

cout<<"Parent class";

}

};

class Child:public Parent

{

public:

void show()

{

cout<<"Child Class";

}

};

int main()

{

clrscr();

Child c; //Child class object

c.show();

getch();

return 0;

}

In the above example we have two classes Parent and Child. In both classes we have implemented same method. Here class Child extends the Parent class publically. Here when execution will starts from the main() function then here we have created object of the Child class and we are calling the show() method. Here Child class can call the Parent class methods by using the object. The output is as follows:

Child class