COMPUTER SCIENCE OBJECT ORIENTED PROGEAMING

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,fand 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.

2

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

a) Anything that an object does not know or cannot do is excluded from the objects.
b) Encapsulation is used to hide unimportant implementation details from other objects.
c) 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.

Eg:

class Rectangle
{ private: float len,bre,area;
public: void readData( )
{ cout<<”\nEnter the length and
breadth..”;
cin>>len>>bre;
}
void calculate( )
{ area=len*bre;
}
void display( )
{ cout<<”\nThe area of the rectangle =
“<<area;
}
};

Eg:

Here in the above class the data members ie len,bre,area and the member functions ie
readData( ), calculate( ), display( ) are bind together in a class named as Rectangle. Ie The member functions can access any data member in the class.

Benefits with encapsulation:

(i) Modularity.
(ii) Information hiding.

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )