forked from codebloded/BackToBasics.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfriendFunc.cpp
More file actions
77 lines (61 loc) · 1.76 KB
/
friendFunc.cpp
File metadata and controls
77 lines (61 loc) · 1.76 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
using namespace std;
class Complex;
class Calculator{
public:
int add(int a, int b)
{
return (a+b);
}
int sumRealComplex(Complex, Complex);
int sumImgComplex(Complex,Complex);
};
class Complex{
private:
int a,b;
//declaring the friend fuction for sum of the real and imaginary numbers individually
// friend int Calculator::sumRealComplex(Complex,Complex);
// friend int Calculator::sumImgComplex(Complex,Complex);
// another method is make a friend class in case of many freind functions
friend class Calculator;
public:
void setNumber(int n1,int n2){
a=n1;
b=n2;
}
//This is the friend function which is grant to access the private members of the class
friend Complex sumComplex(Complex o1, Complex o2);
void printNumber(void)
{
cout<<"The complex number is "<<a<<"+"<<b<<"i"<<endl;
}
};
int Calculator :: sumRealComplex(Complex o1, Complex o2){
return(o1.a+o2.a);
}
int Calculator :: sumImgComplex(Complex o1, Complex o2)
{
return (o1.b+o2.b);
}
//Declaration of friend fucntion
Complex sumComplex(Complex o1, Complex o2)
{
Complex o3;
o3.setNumber((o1.a+o2.a) , (o1.b+o2.b));
return o3;
}
int main(){
Complex c1, c2, sum ;
c1.setNumber(1,2);
c1.printNumber();
c2.setNumber(3,6);
c2.printNumber();
sum = sumComplex(c1,c2);
sum.printNumber();
Calculator calc;
int img=calc.sumImgComplex(c1,c2);
cout<<"The sum of the imaganiary part of the complex number is :"<<img<<endl;
int rel=calc.sumRealComplex(c1,c2);
cout<<"The sum of the real part of the complex number is :"<<rel<<endl;
return 0;
}