As C++ follows the Object Oriented Programming paradigm, it does implement the concepts of Abstraction and Encapsulation. These are interrelated concepts. First let us define them.
Abstraction
Abstraction is a concept where we talk about showing only the necessary part to the outside world and hide the details of implementation. Abstraction can be done for both data members as well as for the methods or functions.
For. eg. for Wall-Clock the abstraction part is the time display while details part is the machine movement gears, battery etc.
Encapsulation
Encapsulation is a concept where we combine the data members, methods or functions in a single unit and provide controlled access to each element. This is in a way a technique to implement abstraction properly.
For e.g. in the Wall-Clock example above all the things movement gears, battery, time display needles are enclosed in a single unit called Wall-Clock. In this only time display needles are shown to external world while other things are not directly accessible.
C++ Code Example
class AMOUNT
{
float PR,SI;
void interest(float p, float r, float t)
{
PR=p;
SI=p*r*t/100;
}
public:
float amount(float p, float r, float t)
{
Interest(p,r,t);
return PR+SI;
}
};
In the above example, there are data members PR, SI and function interest which are not accessible to outside functions.Β Β The amount function can be called in an outside function being declared as public. So the class AMOUNT is an abstract unit which has been made to encapsulate various above data members and functions with required access as explained. The main program can easily call simple amount function with parameters of p, r and t and get the result of total amount after adding the interest part. How the interest part is getting calculated is hidden from the main routine.