-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfriendfun.cpp
More file actions
41 lines (32 loc) · 884 Bytes
/
friendfun.cpp
File metadata and controls
41 lines (32 loc) · 884 Bytes
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
#include <iostream>
using namespace std;
class Box {
int length, width, height;
public:
Box() {
cout << "Enter length, width, and height of the box: ";
cin >> length >> width >> height;
}
// Declare a friend function
friend void showVolume(Box b);
~Box() {
cout << "Box object destroyed." << endl;
}
};
// Friend function defined outside the class
inline void showVolume(Box b) {
int volume = b.length * b.width * b.height;
cout << "Volume of the box is: " << volume << endl;
}
int main() {
int n;
cout << "Enter number of boxes: ";
cin >> n;
Box* boxes = new Box[n]; // Allocate array of Box objects dynamically
for (int i = 0; i < n; i++) {
cout << "Box " << i + 1 << ": ";
showVolume(boxes[i]); // Call friend function
}
delete[] boxes; // Free memory
return 0;
}