Adding two objects using binary plus (+) operator overloading program in c++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| /*C++ program to add two objects using binary plus (+) operator overloading.*/ #include<iostream> using namespace std; class NUM { private : int n; public : //function to get number void getNum( int x) { n=x; } //function to display number void dispNum( void ) { cout << "Number is: " << n; } //add two objects - Binary Plus(+) Operator Overloading NUM operator +(NUM &obj) { NUM x; //create another object x.n= this ->n + obj.n; return (x); //return object } }; int main() { NUM num1,num2,sum; num1.getNum(10); num2.getNum(20); //add two objects sum=num1+num2; sum.dispNum(); cout << endl; return 0; } |
Number is: 30