Encapsulation, private public. Sections
The packaging of data values and member functions
within one object is called an encapsulation. For example an object of
class cube contains data member as side of the cube and member function
volume of the cube. It is also called data hiding which helps to
maintain the integrity of the object. It saves the data from misuse and
outside interference. The data cannot be accessed directly but access
controls can be specified in order to obtain the information. The data
or object can be made public or private depending on the needs. The
data which is private is not accessible outside the scope of the
object. When the data is public it can be accessed by the other parts of
the program.
Here is a program which shows how private and
public members are accessed. The program consists of a class rectangle
which has two data members such as length and breadth and the member
functions area() and len(). The private data member length cannot be
accessed directly. It is accessed using a function len() which is
public and which returns the private data member length.
#include<iostream>
using namespace std;
class rectangle
{
private:
double length;
public:
double breadth;
double area()
{
return(length*breadth);
}
double len()
{
return(length);
}
rectangle(double lenght1,double breadth1)
{
length=lenght1;
breadth=breadth1;
}
};
int main()
{
rectangle r1(3.5,4.6);
double a=r1.len();
double b=r1.breadth;
cout << "The lenght is : " << a << endl;
cout << "The breadth is : " << b << endl;
cout << "The area is : " << r1.area() << endl;
return(0);
}
The result of the program is:-
The statement
private:
double length;
declares that data member length of type double
which has access specifier as private. It cannot be accessed directly.
The statements
public:
double breadth;
double area()
{
return(length*breadth);
}
double len()
{
return(length);
}
declares that data member breadth and member
functions len() and area() are public. The member function len() is
used to return the data member length which cannot be accessed
directly. The statement
rectangle r1(3.5,4.6);
declares an object r1 of rectangle. The constructor
initializes the length and breadth of the object as soon as it is
created. The statement
double a=r1.len();
returns the length of the object. The data member
length cannot be accessed directly as it is declared private therefore
member function len() is used to return the value of length. The
statement double a=r1.length in main() function is invalid as data
member length is inaccessible. The statement
double b=r1.breadth;
equates the value of b to the value of breadth of object r1. The statement
cout << "The area is : " << r1.area() << endl;
displays the area of the rectangle.
#include
ReplyDeleteusing namespace std;
class box
{
public:
double length;
double breadth;
double hight;
double volume()
{
return(length*breadth*hight);
}
box(double l,double b,double h)
{
cout << "A constructor is called" << endl;
length=l;
breadth=b;
hight=h;
}
box()
{
cout << "A default constructor is called " << endl;
}
~box()
{
cout << "Destructing " << length << breadth << hight << endl;
}
};
int main()
{
box b1(4.14,4.43,3.14);
box b2;
cout << "The Box length is: " << b1.length << endl;
cout << "The Box breadth is: " << b1.breadth << endl;
cout << "The Box hight is: " << b1.hight << endl;
cout << "The area of the first Box is : " << b1.volume() << endl;
cout << "Enter the length of the second Box : " ;
cin >> b2.length;
cout << "Enter the breadth of the second Box : " ;
cin >> b2.breadth;
cout << "Enter the hight of the second Box : " ;
cin >> b2.hight;
cout << "The area of second Box is : \n " << b2.volume() << endl;
return(0);
}