Chapter – 3. OBJECT ORIENTED PROGRAMMING

2005 Delhi:

2.a. Define the term Data Hiding in the context of Object Oriented Programming. Give a suitable example using a C++ code to illustrate the same. 2

Ans: A class groups its members into three sections: private, protected and public. The private and protected members remain hidden from outside world. Thus through private and protected members, a class enforces data – hiding.
(The outside world is given only the essential and necessary information through public members, rest of the things remain hidden, which is nothing but abstraction. The act of representing only essential features without including background details is known as abstraction.)

Eg: class ABC
{
private: int a,b;
protected: int c,d;
public: int e,f;
void disp( )
{
----
}
-----
}

In the above class public members(ie e,f and disp( )) only will be available to outside the class.. The other private members (a,b), protected members (c,d) will not be available to outside the class. This concept is called data hiding.

2005 Outside Delhi:

2.a. Define the term Data Encapsulation in the context of Object Oriented Programming. Give a suitable example using a C++ code to illustrate the same.

Ans: Encapsulation is wrapping up of characteristics and behavior into one unit. While implementing encapsulation, following things are taken care:

  1. Anything that an object does not know or cannot do is excluded from the objects.
  2. Encapsulation is used to hide unimportant implementation details from other objects.
  3. Packaging an object’s variables within the protective custody of its methods is called encapsulation and this task is accomplished through classes. Ie the data and associated functions are wrapped up in one unit called class.

A class binds together data and its associated functions under one unit thereby enforcing encapsulation.