Skip to content

Commit 7a5cb7e

Browse files
Update OOPS(1).CPP
1 parent e6cb13a commit 7a5cb7e

File tree

1 file changed

+39
-47
lines changed

1 file changed

+39
-47
lines changed

OOPS(1).CPP

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,58 @@
11
#include<iostream>
22
using namespace std;
33

4-
class CN
5-
{
4+
class complexnum{
65
private:
7-
int realpart; ///accessible within class
6+
7+
int realpart;
88
int imgpart;
9+
910
public:
10-
void Inputrealpart(int a){
11-
realpart=a;
12-
}
1311

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;
1618
}
1719

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;
2224
}
2325

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
2829
}
2930

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;
3636
}
3737
};
3838

3939
int main()
4040
{
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();
6658
}

0 commit comments

Comments
 (0)