-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator overloading.cpp
More file actions
52 lines (39 loc) · 958 Bytes
/
operator overloading.cpp
File metadata and controls
52 lines (39 loc) · 958 Bytes
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
#include <iostream>
using namespace std;
class Count {
private:
int value;
public
:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
Count operator ++ () {
Count temp;
// Here, value is the value attribute of the calling object
++value;
temp.value = value;
return temp;
}
// Overload ++ when used as postfix
Count operator ++ (int) {
Count temp;
// Here, value is the value attribute of the calling object
value++;
temp.value = value;
return temp;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1, result;
// Call the "Count operator ++ ()" function
result = ++count1;
result.display();
// Call the "Count operator ++ (int)" function
result = count1++;
result.display();
return 0;
}