Addition subtraction using class and object in C++


#include<iostream>
using namespace std;
class addSub{
public:
        int num1, num2;
        void inPut(){
                cout<<"Enter two values: "<<endl;
                cin>>num1>>num2;
        }
        void sum()
        {
                int s = num1 + num2;
                cout<<"Summation is: "<<s<<endl;
        }
         void sub()
        {
                int s = num1 - num2;
                cout<<"Subtraction is: "<<s<<endl;
        }
};
int main()
{
        addSub obj;
        int n;
        cout<<"Press 1 for summation, 2 for subtraction: ";
        while(cin>>n){
                obj.inPut();
                if(n == 1){
                       obj.sum();
                }
                else if(n == 2){
                        obj.sub();
                }
        }
        return 0;
}

No comments:

Post a Comment