Answer to Question #297395 in C++ for Sonu

Question #297395

Write a c++ program to add two objects using binary plus(+) operator overloading .create a class NUM in which contains data members as n and member functions are (i) getNum(int)-get input value(ii)dispNum(int)-print result (iii) operator+(NUM)-perform the addition operation

1
Expert's answer
2022-02-13T14:43:59-0500
#include <iostream>
using namespace std;


class Num {
    int n;


public:
    void getNum(int n) { this->n = n; }
    void dispNum()     { cout << n; }
    Num operator+(Num other) { 
        Num result;
        result.n = this->n + other.n;
        return result;
    }
};


int main() {
    Num a, b, c;
    int x;


    cout << "Enter an integer: ";
    cin >> x;
    a.getNum(x);
    cout << "Enter another integer: ";
    cin >> x;
    b.getNum(x);


    c = a + b;


    cout << endl;
    a.dispNum();
    cout << " + ";
    b.dispNum();
    cout << " = ";
    c.dispNum();


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog