Showing posts with label encapsulation. Show all posts
Showing posts with label encapsulation. Show all posts

9.3.14

Excercise : Encapsulation

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.

20.7.13

C++ : Encapsulation


Definition:

  • Data Hiding is also known as Encapsulation.
  • Encapsulation is the process of combining data and function into a single unit called class.
  • Data Hiding is the mechanism where the details of the class are hidden from the user.
  • The user can perform only a restricted set of operations in the hidden member of the class.
  • Encapsulation is a powerful feature that leads to information hiding,abstract data type and friend function.
  • They encapsulate all the essential properties of the object that are to be created.
  • Using the method of encapsulation the programmer cannot directly access the class.


Access Specifier:

There are three types of access specifier.They are

  • Private :Within the block.
  • Public:Whole over the class.
  • Protected:Act as a public and then act as a private.

Within a class members can be declared as either public protected or private in order to explicitly enforce encapsulation.
The elements placed after the public keyword is accessible to all the user of the class.
The elements placed after the protected keyword is accessible only to the methods of the class.
The elements placed after the private keyword are accessible only to the methods of the class.
The data is hidden inside the class by declaring it as private inside the class. Thus private data cannot be directly accessed by the object.

For example,
In order to make the design and maintenance of a car reasonable the complex of equipment is divided into modules with particular interfaces hiding design decisions. 


General Form:

class class name
{
private:
datatype data;
public:
Member functions;
};

main()
{
classname objectname1,objectname2……………;


Example:

class Square
{
private:
int Num;

public:
void Get()    {
    cout<<"Enter Number:";
    cin>>Num;
}
void Display()    {
    cout<<"Square Is:"<<Num*Num;
}
};

void main()
{
Square Obj;
Obj.Get();
Obj.Display();
getch()
}


Features and Advantages of Data Encapsulation:

The advantage of data encapsulation comes when the implementation of the class changes but the interface remains the same.
It is used to reduce the human errors. The data and function are bundled inside the class that take total control of maintenance and thus human errors are reduced.
Makes maintenance of application easier.
Improves the understand-ability of the application.
Enhanced Security.
Note:Thus the concept of encapsulation shows that a non member function cannot access an object’s private or protected data.

In details we can say that Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.
Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.
C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private. For example:
class Box
{
   public:
      double getVolume(void)
      {
         return length * breadth * height;
      }
   private:
      double length;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
};
The variables length, breadth, and height are private. This means that they can be accessed only by other members of the Box class, and not by any other part of your program. This is one way encapsulation is achieved.
To make parts of a class public (i.e., accessible to other parts of your program), you must declare them after the public keyword. All variables or functions defined after the public specifier are accessible by all other functions in your program.
Making one class a friend of another exposes the implementation details and reduces encapsulation. The ideal is to keep as many of the details of each class hidden from all other classes as possible.

Data Encapsulation Example:

Any C++ program where you implement a class with public and private members is an example of data encapsulation and data abstraction. Consider the following example:
#include <iostream>
using namespace std;

class Adder{
   public:
      // constructor
      Adder(int i = 0)
      {
        total = i;
      }
      // interface to outside world
      void addNum(int number)
      {
          total += number;
      }
      // interface to outside world
      int getTotal()
      {
          return total;
      };
   private:
      // hidden data from outside world
      int total;
};
int main( )
{
   Adder a;
   
   a.addNum(10);
   a.addNum(20);
   a.addNum(30);

   cout << "Total " << a.getTotal() <<endl;
   return 0;
}
When the above code is compiled and executed, it produces following result:
Total 60
Above class adds numbers together, and returns the sum. The public members addNum and getTotal are the interfaces to the outside world and a user needs to know them to use the class. The private member total is something that is hidden from the outside world, but is needed for the class to operate properly.