-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_20.cpp
More file actions
50 lines (40 loc) · 1.4 KB
/
Problem_20.cpp
File metadata and controls
50 lines (40 loc) · 1.4 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
#include <iostream>
using namespace std;
class Box {
private:
double length, width, height; // Private data members
public:
// Constructor to initialize dimensions
Box(double l, double w, double h) : length(l), width(w), height(h) {}
// Friend function declaration
friend double calculateVolume(const Box& box);
// Member function demonstrating the use of this pointer
void compare(Box& otherBox) {
if (this->getVolume() > otherBox.getVolume()) {
cout << "Current box is larger than the other box." << endl;
} else if (this->getVolume() < otherBox.getVolume()) {
cout << "Current box is smaller than the other box." << endl;
} else {
cout << "Both boxes are of the same size." << endl;
}
}
// Helper function to get volume
double getVolume() const {
return length * width * height;
}
};
// Friend function definition
double calculateVolume(const Box& box) {
return box.length * box.width * box.height; // Accessing private members
}
int main() {
// Create two Box objects
Box box1(3.5, 2.5, 1.5);
Box box2(4.0, 3.0, 2.0);
// Using friend function to calculate volume
cout << "Volume of box1: " << calculateVolume(box1) << endl;
cout << "Volume of box2: " << calculateVolume(box2) << endl;
// Comparing boxes using the this pointer
box1.compare(box2);
return 0;
}