-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBox It.cpp
107 lines (92 loc) · 2.54 KB
/
Box It.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
100
101
102
103
104
105
106
107
#include <iostream>
using namespace std;
class Box
{
int length;
int breadth;
int height;
public:
// Default constructor
Box() : length(1), breadth(1), height(1) {}
// Parameterized constructor
Box(int l, int b, int h) : length(l), breadth(b), height(h) {}
// Copy constructor
Box(const Box& B) : length(B.length), breadth(B.breadth), height(B.height) {}
// Getter functions
int getLength() const
{
return length;
}
int getBreadth() const
{
return breadth;
}
int getHeight() const
{
return height;
}
// Function to calculate volume
long long CalculateVolume() const
{
return (long long) length * breadth * height;
}
// Overloading less than operator for comparison
bool operator<(const Box& B) const
{
if (length < B.length)
{
return true;
}
else if (length == B.length && breadth < B.breadth)
{
return true;
}
else if (length == B.length && breadth == B.breadth && height < B.height)
{
return true;
}
else
{
return false;
}
}
// Overloading output stream operator to print Box object
friend ostream& operator<<(ostream& output, const Box& B)
{
output << B.length << " " << B.breadth << " " << B.height;
return output;
}
};
int main()
{
// Creating Box objects
Box box1; // default constructor
Box box2(3, 4, 5); // parameterized constructor
Box box3(box2); // copy constructor
// Printing dimensions of the boxes
cout << "Box 1: " << box1 << endl<< "Box 2: " << box2 << endl<< "Box 3: " << box3 << endl;
//cout << "Box 2: " << box2 << endl;
//cout << "Box 3: " << box3 << endl;
// Comparing boxes using the < operator
if (box1 < box2)
{
cout << "Box 1 is smaller than Box 2" << endl;
}
else
{
cout << "Box 1 is greater than or equal to Box 2" << endl;
}
if (box2 < box3)
{
cout << "Box 2 is smaller than Box 3" << endl;
}
else
{
cout << "Box 2 is greater than or equal to Box 3" << endl;
}
// Calculating volume of boxes
cout << "Volume of Box 1: " << box1.CalculateVolume() << endl<< "Volume of Box 2: " << box2.CalculateVolume() << endl<< "Volume of Box 3: " << box3.CalculateVolume() << endl;
//cout << "Volume of Box 2: " << box2.CalculateVolume() << endl;
//cout << "Volume of Box 3: " << box3.CalculateVolume() << endl;
return 0;
}