Â
In this program, we are going to add three numbers using Data Encapsulation in C++ Programming Language.Â
Â
The Code given below can be used in gcc/g++ Compilers: -
#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()
{
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
Adder add;
add.addNum(a);
add.addNum(b);
add.addNum(c);
cout << "Total = " << add.getTotal() << endl;
return 0;
}
//.......Coded by RISHAB NAIR
The Code given below can be used in Turbo C++ Compilers: -
#include <iostream.h>
#include <conio.h>
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;
};
void main()
{
clrscr();
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
Adder add;
add.addNum(a);
add.addNum(b);
add.addNum(c);
cout << "Total = " << add.getTotal() << endl;
getch();
}
//.......Coded by RISHAB NAIR
#ENJOY CODING
Â
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP