-
Notifications
You must be signed in to change notification settings - Fork 0
/
73.cpp
73 lines (65 loc) · 2.01 KB
/
73.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
// conversions: Distance to meters, meters to Distance
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance
//English Distance class
{
private:
static const float MTF = 3.280833F; //meters to feet
int feet;
float inches;
public:
//constructor (no args)
Distance() : feet(0), inches(0.0)
{ }
//constructor (one arg) basic to Class type float to Distance
Distance(float meters)
{
//convert meters to Distance
float fltfeet = MTF * meters; //convert to float feet
feet = int(fltfeet); //feet is integer part
inches = 12*(fltfeet-feet); //inches is what's left
cout << "\nOne arg\n";
cout << "Basic to Class type Conversion" << endl;
}
//constructor (two args)
Distance(int ft, float in) : feet(ft),inches(in)
{
cout << "\nTwo args\n";
}
void getdist() //get length from user
{
cout << "\nEnter feet: "; cin >> feet;
cout << "Enter inches: "; cin >> inches;
}
void showdist() const //display distance
{
cout << feet << "\':" << inches << '\"';
}
//dist2 = 2.35F; dist2.operator=(2.35)
void operator=(float meters)
{
float fltfeet = MTF * meters; //convert to float feet
feet = int(fltfeet); //feet is integer part
inches = 12*(fltfeet-feet); //inches is what's left
cout << "\nOverload =\n";
}
};
int main()
{
float mtrs;
//Basic type to Class Conversion
Distance dist1(2.35F); //uses 1-arg constructor to convert meters to Distance
cout << "\ndist1 = "; dist1.showdist();
cout << "\n=========================" << endl;
cout << "Press enter to continue";
cin.get();
Distance dist2;
dist2 = 2.35F; // uses overloaded =
cout << "\ndist2 = " ; dist2.showdist();
cout << "\n=========================" << endl;
cout << "Press enter to continue";
cin.get();
return 0;
}