-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path26friendfunc.cpp
More file actions
45 lines (40 loc) · 1019 Bytes
/
Copy path26friendfunc.cpp
File metadata and controls
45 lines (40 loc) · 1019 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
45
#include <iostream>
using namespace std;
class complex
{
int a, b;
public:
friend complex sumofcomplex(complex o1, complex o2);
/*This is called a friend function
whenever a class function is created outside the class scope, it is nessecary to declare the function as part of the class
or as a "friend" to access the private data
It doesn't require any object to access it
There is no difference if the friend call is written in public or private
it usually contains objects as arguments */
void setnum(int n1, int n2)
{
a = n1;
b = n2;
}
void printnum()
{
cout << "z = " << a << " + " << b << "i" << endl;
}
};
complex sumofcomplex(complex o1, complex o2)
{
complex o3;
o3.setnum((o1.a + o2.a), (o1.b + o2.b));
return o3;
}
int main()
{
complex c1, c2, compsum;
c1.setnum(1, 5);
c1.printnum();
c2.setnum(4, 3);
c2.printnum();
compsum = sumofcomplex(c1, c2);
compsum.printnum();
return 0;
}