-
Notifications
You must be signed in to change notification settings - Fork 0
/
62.cpp
50 lines (39 loc) · 1.19 KB
/
62.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
#include <iostream>
#include <string>
using namespace std;
class Box
{
protected:
double length ;
double width ;
double height ;
public:
Box() { length = width = height = 1.0; }
Box(double lv, double wv, double hv) : length(lv), width(wv), height(hv) {}
// Function to show the volume of an object
void showVolume() const
{ cout << "Box usable volume is " << volume() << endl; }
// Function to calculate the volume of a Box object
double volume() const { return length*width*height; }
};
class ToughPack : public Box
{
public:
// Constructor
ToughPack(double lv, double wv, double hv) : Box (lv, wv, hv) {}
// Function to calculate volume of a ToughPack allowing 15% for packing
double volume() const { return 0.85*length*width*height; }
};
int main()
{
Box box(20.0, 30.0, 40.0);
// Define a box
ToughPack hardcase(20.0, 30.0, 40.0);
// Declare tough box - same size
box.showVolume();
// Display volume of base box
hardcase.showVolume(); //early binding
// Display volume of derived box
cout << "hardcase volume is " << hardcase.volume() << endl;
return 0;
}