-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperator_overloading.cpp
99 lines (90 loc) · 1.52 KB
/
operator_overloading.cpp
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
96
97
98
99
#include<iostream>
using namespace std;
/*class Complex{
private:
int real,imag;
public:
Complex()
{
real=0;imag=0;
}
Complex(int r,int i)
{
real =r;
imag =i;
}
void print()
{
cout<<real<<" + "<<imag<<"i"<<endl;
}
// operator overloading
Complex operator +(Complex c)
{
Complex temp;
temp.real = real+c.real;
temp.imag = imag+c.imag;
return temp;
}
};
int main()
{
Complex c1(5,4);
Complex c2(2,3);
Complex c3;
//Complex c3(1,5);
// Complex c4;
c3=c1+c2; // c1.add(c2);
// int x=5;
// int y=4;
// int z =x+y;
c3.print();
//c4 =c1+c2+c3;
//c4.print();
return 0;
}*/
class Weight{
private:
int kg;
public:
Weight()
{
kg=0;
}
Weight(int x)
{
kg=x;
}
void print()
{
cout<<"Weight: "<<kg<<endl;
}
void operator ++()//pre
{
++kg;
}
void operator ++(int) //post
{
kg++;
}
void operator --()//pre
{
--kg;
}
void operator --(int) //post
{
kg--;
}
};
int main()
{
Weight obj(1);
++obj;
obj.print();
obj++;
obj.print();
obj--;
obj.print();
--obj;
obj.print();
return 0;
}