-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion 2 box class.cpp
More file actions
57 lines (43 loc) · 1.13 KB
/
question 2 box class.cpp
File metadata and controls
57 lines (43 loc) · 1.13 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
51
52
53
54
55
56
57
#include<iostream>
#include<string>
using namespace std;
class BOX {
private:
double length;
double width;
double height;
public:
BOX(double l, double w, double h)
: length(l), width(w), height(h) {}
friend void printDimensions(const BOX& box);
friend class BoxVolumeCalculator;
void displayVolume();
};
void printDimensions(const BOX& box) {
cout<<"\n";
cout << "Length: " << box.length << endl;
cout << "Width: " << box.width << endl;
cout << "Height: " << box.height << endl;
cout<<"\n";
}
class BoxVolumeCalculator {
public:
double calculateVolume(const BOX& box) {
return box.length * box.width * box.height;
}
};
void BOX::displayVolume()
{
BoxVolumeCalculator calc;
double volume = calc.calculateVolume(*this); // this refers to the current object
cout << "Volume of the box: " << volume << endl;
}
int main() {
BOX box(9.0, 6.6, 5.9);
printDimensions(box);
box.displayVolume();
BOX box2(3.0, 4.0, 5.0);
printDimensions(box2);
box2.displayVolume();
return 0;
}