-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass14.cpp
More file actions
44 lines (40 loc) · 810 Bytes
/
Copy pathclass14.cpp
File metadata and controls
44 lines (40 loc) · 810 Bytes
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
43
44
//overloading of operator as a friend function
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class complex
{
private:
int a,b;
public:
void setData(int x,int y)
{
a = x; b = y;
}
void showData()
{
cout<<" a = "<<a<<" b = "<<b<<endl;
}
//friend function declared
friend complex operator+(complex A,complex B);
};
complex operator+(complex A,complex B)
{
complex c;
c.a = A.a+ B.a;
c.b = A.b+ B.b;
return c;
}
int main(int argc, char const *argv[])
{
system("cls");
complex c1,c2,c3;
c1.setData(20,30);
c2.setData(21,49);
// c3 = operator+(c1,c2); //we can write like this
c3 = c1+c2; //after overloading + operator
c3.showData();
system("pause");
return 0;
}