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
#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;
}
Comments
Leave a comment