- Encapsulation:
Defining data members and member functions together within a class is refffered as encapsulation. Without encapsulation the object oriented programming cannot be implement.
Data hiding:
In object oriented programming most of the item data members is defined as private and member function will be defined as public. The public members of the class can be accessed from inside of the class as well as from the outside of the class. The private members of the class can be accessed from inside of the class but cannot be accessed from outside of the class. Thus private members remain hidden from outside of the class. Since most of the time the data members are kept private it remains hidden from outside of the class and thus the data hiding is implemented. Since data members are private the class user cannot access them directly but ca access them through predefined functions. Thus the class user can access the data the way class developer likes it.
Program :- The following program will act as an example for encapsulation as well as data hiding. Develop a class called as circle with private data member radius. The class should have following member function.
- A function set radius that accepts value of the radius as parameter.
- A function get radius that will get the value of radius from user.
- A function to calculate and return area of the function.
- A function to calculate and return circumference of the circle.
- Write a suitable main function
# include
# define PI 3.14159
class circle
{
Private:
float radius;
Public:
void setradius (float r )
{
radius = r;
}
void get radius ( )
{
cin >>radius;
}
float area ( )
{
return PI * radius * radius;
}
float circum ( )
{
return 2 * PI *radius;
}
};
Void main ( )
{
circle c1, c2;
c1. Setradius (10.5);
cout << "Area=" << c1. Area ( ) << '\n';)
cout << "Circumference = " << c1 . circum ( ) << '\n';
cout << "Enter radius of the circle=";
c2 . get radius ( );
cout << "Area=" << C 2.area ( ) << '\n';
cout << "Circumference=" << c2.circum ( ) << '\n';
}
Explanation:-
- Encapsulation:
In the above program data member radius and four member function set radius, get radius, area and circum are defined together and thus the encapsulation is obtained
II.Data hiding:
The data member radius is defined as private and hence it remains hidden from outside of the class. In other words we cannot access the data member radius from main function. Thus the data hiding is obtained.
The class circle provides member functions(facilities or methods) for calculating area and circumference but do not provide facility that member function to calculate diameter of the circle.
If class user wants to calculate diameter of the circle when the radius is known it will not be possible with the above mentioned class. This is because no member function is available to calculate diameter and moreover data member radius is private which cannot be accessed directly. Thus class user can access the data only in the manner the class developer wants it. Thus data hiding is implemented.
→ Private and Public access specifier:
The private members of the class can be accessed from inside of the class can be accessed from inside of the class but cannot be accessed from outside of the class. In the previous example the private member radius is accessed within the class and it cannot be access from the main function.
→ Public members:
All the four member function of the class circle asre public and hence can be accessed from outside of the class. Note:- The access specifier private is by default within the class.
Objects and allocated memory.
Prefer the class circle of the previous program. Consider the statement circle c1, c2; written in the main fn using the above statement we are defining two objects of class circle. Consider the following diagram. As the above diagram indicates c1.radius 7 c2.radius will occupy separate memory allocation. This is because they have got different physical existence. The member function of the class circle. i.e set radius( ), get radius ( ), area ( ), circum ( ) are occupied only one in the memory. The data members of the class will occupy separate memory for every object whereas member function will occupy the memory only once irrespective of no of objects. The member function of the class will be shared by all the objects.
Calling the member fn.
Consider the following statement C1. Set radius (10.5); Using the above statement we call the public member fn set radius for the object c1 and we are passing the parameter 10.5. This value 10.5 will be copied into formal parameter. The value of r i.e 10.5 will be copied into radius i.e c1.radius. This is because the member fn c1 11 called for object c1.
Program :-
Declaring the member fn within the class and defining them outside of the class.
Consider the following program:
# define PI 3.14159
# include
class circle;
{
float radius;
public:
void setradius (float);
void getradius ( );
float area ( );
float circum ( );
};
void circle :: setradius (float r)
{
radius = r;
}
void circle :: getradius ( )
{
cin >> radius;
}
float circle :: area ( )
{
return PI * radius * radius;
}
float circle :: circum ( )
{
return 2 * PI * radius;
}
void main ( )
{≡
}
Explanation:
In the above program all the four member fn are declared within the class and are defined outside of the class.
When the member fn are defined outside the class. There are general syntax is given as follows.
Returntype classname ::memberfnnamE(optional list of parameters)
{≡
}
Consider the following function header
void circle : : setradius (float r)
The above fn header indicates that set radius is member fn that belongs to class circle. It accepts one floating point parameter and returns nothing. We use scope resolution operator ( :: ) to indicate that member fn belongs to the class circle.
In the above program all the member fn are physically return outside of the class but their scope belongs to the class only. Hence private member radius can be still accessed within all the member fn.