You are on page 1of 2

/* C++ Data Encapsulation - Example Program */

#include<iostream.h>
#include<conio.h>

class ENCAP
{
public:
ENCAP (int temp=0)
{
tot=temp;
}
void add_num(int num)
{
tot = tot+num;
}
int get_tot()
{
return tot;
}
private:
int tot; };

int main()
{ ENCAP a;
a.add_num(100);
a.add_num(200);
a.add_num(300);
cout<<"Sum = "<<a.get_tot() <<endl;
return 0; }

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

Page 1
/* C++ Data Encapsulation - Example Program */

class CALC
{ public:
CALC(int i=0)
{res=i;}
void addnumber(int num1, int num2)
{ res=num1+num2; }
void subnumber(int num1, int num2)
{ res=num1-num2; }
void mulnumber(int num1, int num2)
{ res=num1*num2; }
void divnumber(int num1, int num2)
{ res=num1/num2; }
int getresult()
{ return res; }
private: // hidden from outside the world
int res; };
void main()
{
clrscr();
CALC cob;
int a, b;
char ch;
cout<<"Enter two number: ";
cin>>a>>b;
cout<<"Enter the operator(+,-,/,*): ";
cin>>ch;
switch(ch)
{ case '+':
cob.addnumber(a, b);
case '-':
cob.subnumber(a, b);
cout<<"Subtraction = "<<cob.getresult()<<"\n"; break;
case '*':
cob.mulnumber(a, b);
cout<<"Multiplication = "<<cob.getresult()<<"\n"; break;
case '/':
cob.divnumber(a, b);
cout<<"Division = "<<cob.getresult()<<"\n"; break;
default:
cout<<"Wrong operator...Exiting..Press a key..";
getch();
exit(1);
}
cout<<"Summation = "<<cob.getresult()<<"\n"; break;
getch(); }

Page 2

You might also like