Problem #1: Point out the errors(s) if any, in the following program:
Labels:
C/C++ Language
1 // Problem related to Operator
2 // overloading
3 #include <iostream.h>
4
5 class myclass
6 {
7 int a;
8
9 public:
10 myclass(int);
11 void show();
12
13 myclass operator ++();
14 myclass operator --();
15 };
16
17 myclass::myclass(int x)
18 {
19 a=x;
20 }
21
22 void myclass::show()
23 {
24 cout<<a<<endl;
25 }
26
27 myclass myclass::operator ++()
28 {
29 a++;
30
31 return *this;
32 }
33
34 myclass myclass::operator --()
35 {
36 a--;
37
38 return *this;
39 }
40
41 // main
42 void main()
43 {
44 myclass ob(10);
45
46 ob.show();
47
48 ob++;
49 ob.show();
50 }
Problem #2: Point out the errors(s) if any, in the following program:
1 // Problem related to Operator
2 // overloading
3 #include <iostream.h>
4
5 class myclass
6 {
7 int a;
8 int b;
9
10 public:
11 myclass(int, int);
12 void show();
13
14 myclass operator=(myclass);
15 };
16
17 myclass myclass::operator=(myclass ob)
18 {
19 a=ob.a;
20 b=ob.b;
21 };
22
23 myclass::myclass(int x,int y)
24 {
25 a=x;
26 b=y;
27 }
28
29 void myclass::show()
30 {
31 cout<<a<<endl<<b<<endl;
32 }
33
34 // main
35 void main()
36 {
37 myclass ob(10,11);
38 myclass ob2(20,21);
39 myclass ob3(30,41);
40
41 int x,y,z;
42
43 x=y=z=10;
44 cout<<z<<endl;
45
46 ob=ob2=ob3;
47 ob.show();
48 }
Problem #3: Point out the errors(s) if any, in the following program:
1 #include <iostream.h>
2
3 // class
4 class myclass
5 {
6 int a;
7
8 public:
9 myclass(int);
10 void show();
11
12 void operator ++();
13 void operator --();
14 };
15
16 myclass::myclass(int x)
17 {
18 a=x;
19 }
20
21 void myclass::show()
22 {
23 cout<<a<<endl;
24 }
25
26 void myclass::operator ++()
27 {
28 a++;
29 }
30
31 void myclass::operator --()
32 {
33 a--;
34 }
35
36 // main
37 void main()
38 {
39 myclass ob(10);
40 myclass ob2(100);
41
42 ob.show();
43 ob2.show();
44
45 ob2=++ob;
46 ob.show();
47 ob2.show();
48 }
Responses
0 Respones to "Problems Related to Operator Overloading"
Post a Comment