Saturday, 12 July 2014

What is Encapsulation in C++

Structure of the Problem Requirements

Encapsulation is a very Important Concept in Programming. All the Programming Language Facilitate their Developers with Encapsulation to Save their Codes. In Encapsulation We hide the some parts of our Codes and only Give Some of the  Functionality of our Codes to user for usage . in this problem we Just hide the Add function which is very simple Example of Encapsulation.

Source Code

#include <iostream>       
#include <cstdlib>
using namespace std;

This Program Represent a Very Simple Example of Encapsulation

class addition{ // Make a class to encapsulate a data
               int number1; // declare an integer
               int number2; // declare an integer
                      int add(void); //this method is public mean you can access it another class
                      void initilize(int,int); //Not reurning anything method which is recieve input
      int addition::add(void)
          return number1+number2; // return a sum of two number
      void addition::initilize(int init_number1,int init_number2)
           number1=init_number1; //value assign to a number 1 
           number2=init_number2; //value assign to a number 2 
int main()

int num1 = 0;
int num2 =0;
cout<<endl<<endl<<"******************** ENCAPSULATION IN C++ ****************"<<endl<<endl;
cout<<"Enter Number_1: ";
    cout<<endl<<"Enter Number_2: ";
addition A; // Make a instance of class addition
    A.initilize(num1, num2); // call a method of claas addition
    cout<<endl<<endl<<"Answer : "<<A.add()<<endl<<endl;
    return EXIT_SUCCESS;

Output of the Program 

Shows the Encapsulation of Programme
Encapsulation mean Hide your Data

Share it Please

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.


Post a Comment