forked from vectormars/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInheritance_008.cpp
103 lines (103 loc) · 2.89 KB
/
Inheritance_008.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
// multiple inheritance with English Distances
#include <iostream>
#include <string>
using namespace std;
////////////////////////////////////////////////////////////////
class Type //type of lumber
{
private:
string dimensions;
string grade;
public: //no-arg constructor
Type() : dimensions("N/A"), grade("N/A")
{ }
//2-arg constructor
Type(string di, string gr) : dimensions(di), grade(gr)
{ }
void gettype() //get type from user
{
cout << " Enter nominal dimensions (2x4 etc.): ";
cin >> dimensions;
cout << " Enter grade (rough, const, etc.): ";
cin >> grade;
}
void showtype() const //display type
{
cout << "\n Dimensions: " << dimensions;
cout << "\n Grade: " << grade;
}
};
////////////////////////////////////////////////////////////////
class Distance //English Distance class
{
private:
int feet;
float inches;
public: //no-arg constructor
Distance() : feet(0), inches(0.0)
{ } //constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
void getdist() //get length from user
{
cout << " Enter feet: ";
cin >> feet;
cout << " Enter inches: ";
cin >> inches;
}
void showdist() const //display distance
{
cout << feet << "\'-" << inches << '\"';
}
};
////////////////////////////////////////////////////////////////
class Lumber : public Type, public Distance
{
private:
int quantity; //number of pieces
double price; //price of each piece
public: //constructor (no args)
Lumber() : Type(), Distance(), quantity(0), price(0.0)
{ }
//constructor (6 args)
Lumber( string di, string gr, //args for Type
int ft, float in, //args for Distance
int qu, float prc ) : //args for our data
Type(di, gr), //call Type ctor
Distance(ft, in), //call Distance ctor
quantity(qu), price(prc) //initialize our data
{ }
void getlumber()
{
Type::gettype();
Distance::getdist();
cout << " Enter quantity: ";
cin >> quantity;
cout << " Enter price per piece: ";
cin >> price;
}
void showlumber() const
{
Type::showtype();
cout << "\n Length: ";
Distance::showdist();
cout << "\n Price for " << quantity
<< " pieces: $" << price * quantity;
}
};
////////////////////////////////////////////////////////////////
int main()
{
Lumber siding; //constructor (no args)
cout << "\nSiding data:\n";
siding.getlumber(); //get siding from user
//constructor (6 args)
Lumber studs( "2x4", "const", 8, 0.0, 200, 4.45F );
//display lumber data
cout << "\nSiding";
siding.showlumber();
cout << "\nStuds";
studs.showlumber();
cout << endl;
return 0;
}