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:-

 

encapsulation

 

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.

 

Go to the previous lesson or proceed to the next lesson
Table of contents