Creating a calculator using Data Encapsulation || Data Encapsulation || C++





In this program, we are going to create a Calculator using Data Encapsulation in C++ Programming Language. 
 


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <stdlib.h>
using namespace std;

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)
    {
        if(num2==0){
        	cout<<"Cannot Divide by Zero!";
            res = 0;
		}
		else{
			res = num1 / num2;
		}
    }
    int getresult()
    {
        return res;
    }

    private:
    // hidden from outside the world
    int res;
};

int main()
{
    CALC cob;
    int a, b;
    char ch;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    cout << "Enter the operator(+,-,/,*): ";
    cin >> ch;
    switch (ch)
    {
    case '+':
        cob.addnumber(a, b);
        cout << "Sum is = " << cob.getresult() << "\n";
        break;
    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..";
        exit(1);
    }
}

//.......Coded by RISHAB NAIR


The Code given below can be used in Turbo C++ Compilers: -

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

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)
    {
        if(num2==0){
        	cout<<"Cannot Divide by Zero!";
            res = 0;
		}
		else{
			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 numbers: ";
    cin >> a >> b;
    cout << "Enter the operator(+,-,/,*): ";
    cin >> ch;
    switch (ch)
    {
    case '+':
        cob.addnumber(a, b);
        cout << "Sum is = " << cob.getresult() << "\n";
        break;
    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..";
        exit(1);
    }
    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

Previous Post Next Post