-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator_overloading.cpp
More file actions
178 lines (145 loc) · 3.92 KB
/
operator_overloading.cpp
File metadata and controls
178 lines (145 loc) · 3.92 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//C++ operator overloading
//prefix,postfix - increment and decrement oeprators
struct X
{
// prefix increment
X& operator++()
{
// actual increment takes place here
return *this; // return new value by reference
}
// postfix increment
X operator++(int)
{
X old = *this; // copy old value
operator++(); // prefix increment
return old; // return old value
}
// prefix decrement
X& operator--()
{
// actual decrement takes place here
return *this; // return new value by reference
}
// postfix decrement
X operator--(int)
{
X old = *this; // copy old value
operator--(); // prefix decrement
return old; // return old value
}
};
//operator []
class MyContainer {
private:
int* data; // Pointer to store integers
size_t size; // Size of the container
public:
// Constructor
MyContainer(size_t size) : size(size), data(new int[size]) {}
// Destructor
~MyContainer() { delete[] data; }
// Overload [] operator
int& operator[](size_t index) {
return data[index];
}
};
//operator -
#include <iostream>
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex(): real(0), imag(0){ }
void input()
{
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
// Operator overloading
Complex operator - (Complex c2)
{
Complex temp;
temp.real = real - c2.real;
temp.imag = imag - c2.imag;
return temp;
}
void output()
{
if(imag < 0)
cout << "Output Complex number: "<< real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag << "i";
}
};
int main()
{
Complex c1, c2, result;
cout<<"Enter first complex number:\n";
c1.input();
cout<<"Enter second complex number:\n";
c2.input();
// In case of operator overloading of binary operators in C++ programming,
// the object on right hand side of operator is always assumed as argument by compiler.
result = c1 - c2;
result.output();
return 0;
}
//operator +
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float img;
public:
// constructor to initialize real and img to 0
Complex() : real(0), img(0) {}
Complex(float real, float img) : real(real), img(img){}
// overload the + operator
friend Complex operator + (const Complex& obj1, const Complex& obj2) {
Complex temp;
temp.real = obj1.real + obj2.real;
temp.img = obj1.img + obj2.img;
return temp;
}
void display() {
if (img < 0)
cout << "Output Complex number: " << real << img << "i";
else
cout << "Output Complex number: " << real << "+" << img << "i";
}
};
int main() {
Complex c1(1.0f, 2.0f);
Complex c2(1.0f, 3.0f);
// calls the overloaded + operator
Complex result = c1 + c2;
result.display();
return 0;
}
//operator ()
#include <iostream>
class MyFunctionObject {
private:
int count; // Stores the number of times the object has been called
public:
// Constructor initializes count to 0
MyFunctionObject() : count(0) {}
// Overload () operator
void operator()(int value) {
++count;
std::cout << "Called " << count << " times." << std::endl;
}
};
int main() {
MyFunctionObject myFuncObj;
// Directly invoking the overloaded () operator
myFuncObj(5);
myFuncObj(10);
return 0;
}