-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSession10F.cpp
More file actions
95 lines (72 loc) · 1.78 KB
/
Session10F.cpp
File metadata and controls
95 lines (72 loc) · 1.78 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include<string>
using namespace std;
class ComplexNum{
int real;
int imag;
public:
ComplexNum(){
real = 0;
imag = 0;
}
void setComlexNumber(int real, int imag){
this->real = real;
this->imag = imag;
}
void showComplexNumber(){
cout<<"Real Part: "<<real<<" Imag Part: "<<imag<<"\n";
}
// Objects as Inputs
/*static void addComplexNumbers(ComplexNum c1, ComplexNum c2){
ComplexNum c3;
c3.real = c1.real + c2.real;
c3.imag = c1.imag + c2.imag;
cout<<"<<<<<<<<<<<<<<\n";
c3.showComplexNumber();
cout<<"<<<<<<<<<<<<<<\n";
}*/
/*void addComplexNumbers(ComplexNum c1, ComplexNum c2){
ComplexNum c3;
c3.real = c1.real + c2.real;
c3.imag = c1.imag + c2.imag;
cout<<"<<<<<<<<<<<<<<\n";
c3.showComplexNumber();
cout<<"<<<<<<<<<<<<<<\n";
}*/
// Function which takes objects as inputs
// Returns Object
ComplexNum addComplexNumbers(ComplexNum c1, ComplexNum c2){
ComplexNum c3;
c3.real = c1.real + c2.real;
c3.imag = c1.imag + c2.imag;
return c3;
}
// Passing Object as Reference
// Operator Overloading
ComplexNum operator+(ComplexNum &cNum){
ComplexNum c3;
c3.real = real + cNum.real;
c3.imag = imag + cNum.imag;
return c3;
}
};
int main(int argc, char const *argv[]){
ComplexNum cNum1;
ComplexNum cNum2;
cNum1.setComlexNumber(10, 20);
cNum2.setComlexNumber(30, 40);
cNum1.showComplexNumber();
cNum2.showComplexNumber();
//ComplexNum::addComplexNumbers(cNum1, cNum2);
//cNum1.addComplexNumbers(cNum1, cNum2);
ComplexNum cNum3;
cNum3 = cNum3.addComplexNumbers(cNum1, cNum2);
cNum3.showComplexNumber();
ComplexNum cNum4;
cNum4 = cNum1 + cNum2;// cNum1 + cNum2 -> cNum1.operator+(cNum2);
cNum4.showComplexNumber();
ComplexNum cNum5;
cNum5 = cNum1.operator+(cNum2);
cNum5.showComplexNumber();
return 0;
}