-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq53.cpp
56 lines (49 loc) · 1.29 KB
/
q53.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
/*
* WAP to show overloading of prefix and postfix increment operators.
*/
#include <iostream>
using namespace std;
class Box {
int length, breadth, height;
public:
Box() : length{0}, breadth{0}, height{0} { }
Box(int length, int breadth, int height) :
length{length}, breadth{breadth}, height{height} { }
Box(Box& obj) :
length{obj.length}, breadth{obj.breadth}, height{obj.height} { }
void display();
Box& operator ++ (); // prefix operator overloading
Box operator ++ (int); // postfix operator overloating
};
void Box::display() {
cout << "Length = " << length << '\n';
cout << "Breadth = " << breadth << '\n';
cout << "Height = " << height << '\n';
}
// prefix operator overloading
Box& Box::operator ++ () {
length += 1;
breadth += 1;
height += 1;
return *this;
}
// postfix operator overloading
Box Box::operator ++ (int) {
Box result(*this);
length += 1;
breadth += 1;
height += 1;
return result;
}
int main() {
Box b(10, 10, 10);
cout << "Box before incrementing: \n";
b.display();
b++;
cout << "\nBox after postfix increment: \n";
b.display();
++b;
cout << "\nBox after prefix increment: \n";
b.display();
return 0;
}