|
1 | 1 | #include<iostream> |
2 | 2 | using namespace std; |
3 | 3 |
|
4 | | -class CN |
5 | | -{ |
| 4 | +class complexnum{ |
6 | 5 | private: |
7 | | - int realpart; ///accessible within class |
| 6 | + |
| 7 | + int realpart; |
8 | 8 | int imgpart; |
| 9 | + |
9 | 10 | public: |
10 | | - void Inputrealpart(int a){ |
11 | | - realpart=a; |
12 | | - } |
13 | 11 |
|
14 | | - void Inputimgpart(int a){ |
15 | | - imgpart=a; |
| 12 | + void acceptrealpart(int a){ |
| 13 | + realpart=a; |
| 14 | + } |
| 15 | + |
| 16 | + void acceptimgpart(int a){ |
| 17 | + imgpart=a; |
16 | 18 | } |
17 | 19 |
|
18 | | - void PrintingCN(){ |
19 | | - cout<<realpart; |
20 | | - if (imgpart>=0) cout<<"+"; |
21 | | - cout<<imgpart<<"i"<<endl; |
| 20 | + void Printcomplexnum(){ |
| 21 | + cout<<realpart; |
| 22 | + if (imgpart>=0) cout<<"+"<<imgpart<<"i"<<endl; |
| 23 | + else cout<<imgpart<<"i"<<endl; |
22 | 24 | } |
23 | 25 |
|
24 | | - void Add(CN cn)///parameter is cn |
25 | | - { |
26 | | - realpart=realpart+cn.realpart; ///realpart of left side and argument is right side |
27 | | - imgpart=imgpart+cn.imgpart; |
| 26 | + void Add(complexnum cn){ ///calling : cn1.Add(cn2) , cn1 is getting updated and argument is cn2 referred as cn |
| 27 | + realpart=realpart+cn.realpart; ///cn2 is as it is , cn1 is changed |
| 28 | + imgpart=imgpart+cn.imgpart; ///cn1 is not mentioned as the function is being called on LHS and arg. is RHS |
28 | 29 | } |
29 | 30 |
|
30 | | - CN operator+(CN input)///argument is output |
31 | | - { |
32 | | - CN output; |
33 | | - output.realpart=realpart+input.realpart; /// left hand side cn1(realpart)(input) |
34 | | - output.imgpart=imgpart+input.imgpart; |
35 | | - return output; |
| 31 | + complexnum operator+(complexnum cn){ |
| 32 | + complexnum cn3; |
| 33 | + cn3.realpart=realpart+cn.realpart; |
| 34 | + cn3.imgpart=imgpart+cn.imgpart; |
| 35 | + return cn3; |
36 | 36 | } |
37 | 37 | }; |
38 | 38 |
|
39 | 39 | int main() |
40 | 40 | { |
41 | | - |
42 | | -CN cn1,cn2,cn; |
43 | | - |
44 | | -cn1.Inputrealpart(10); |
45 | | -cn1.Inputimgpart(20); |
46 | | - |
47 | | -cn2.Inputrealpart(-5); |
48 | | -cn2.Inputimgpart(-80); |
49 | | - |
50 | | -cn1.PrintingCN(); |
51 | | -cn2.PrintingCN(); |
52 | | - |
53 | | -cn1.Add(cn2); |
54 | | -cn1.PrintingCN(); /// cn1 is updated |
55 | | -cn2.PrintingCN(); ///unchanged because value added in cn1 |
56 | | - |
57 | | - |
58 | | -cout <<endl; |
59 | | - |
60 | | -cn=cn1+cn2; |
61 | | -cn1.PrintingCN(); |
62 | | -cn2.PrintingCN(); |
63 | | -cn.PrintingCN(); |
64 | | - |
65 | | - |
| 41 | + complexnum cn1,cn2,cn3; |
| 42 | + cn1.acceptrealpart(10); |
| 43 | + cn1.acceptimgpart(20); |
| 44 | + cn2.acceptrealpart(5); |
| 45 | + cn2.acceptimgpart(15); |
| 46 | + cn1.Printcomplexnum(); |
| 47 | + cn2.Printcomplexnum(); |
| 48 | + |
| 49 | + cn1.Add(cn2); |
| 50 | + cout<<"updated cn1:"<<endl; |
| 51 | + cn1.Printcomplexnum(); |
| 52 | + cout<<"As it is cn2:"<<endl; |
| 53 | + cn2.Printcomplexnum(); |
| 54 | + |
| 55 | + cout<<"updated cn1+cn2 by + operator :"<<endl; |
| 56 | + cn3=cn1+cn2; |
| 57 | + cn3.Printcomplexnum(); |
66 | 58 | } |
0 commit comments