-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq54.cpp
55 lines (47 loc) · 1.27 KB
/
q54.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
/*
* WAP to demonstrate unary operator overloading using friend function.
*/
#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();
friend Box operator ++ (Box& obj); // prefix unary overloading
friend Box operator ++ (Box& obj, int); // postfix unary overloading
};
void Box::display() {
cout << "Length = " << length << '\n';
cout << "Breadth = " << breadth << '\n';
cout << "Height = " << height << '\n';
}
Box operator ++(Box& obj) {
obj.length += 1;
obj.breadth += 1;
obj.height += 1;
return obj;
}
Box operator ++(Box& obj, int) {
Box result(obj);
obj.length += 1;
obj.breadth += 1;
obj.height += 1;
return result;
}
int main() {
Box b(10, 10, 10);
cout << "Box before changes: \n";
b.display();
b++;
cout << "\nBox after postfix increment: \n";
b.display();
++b;
cout << "\nBox after prefix increment: \n";
b.display();
return 0;
}