📄 englconv.cpp
字号:
// englconv.cpp
// conversions: Distance to meters, meters to Distance
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance //English Distance class
{
private:
const float MTF; //meters to feet
int feet;
float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0), MTF(3.280833F)
{ } //constructor (one arg)
Distance(float meters) : MTF(3.280833F)
{ //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
} //constructor (two args)
Distance(int ft, float in) : feet(ft),
inches(in), MTF(3.280833F)
{ }
void getdist() //get length from user
{
cout << "\nEnter feet: "; cin >> feet;
cout << "Enter inches: "; cin >> inches;
}
void showdist() const //display distance
{ cout << feet << "\'-" << inches << '\"'; }
operator float() const //conversion operator
{ //converts Distance to meters
float fracfeet = inches/12; //convert the inches
fracfeet += static_cast<float>(feet); //add the feet
return fracfeet/MTF; //convert to meters
}
};
////////////////////////////////////////////////////////////////
int main()
{
float mtrs;
Distance dist1 = 2.35F; //uses 1-arg constructor to
//convert meters to Distance
cout << "\ndist1 = "; dist1.showdist();
mtrs = static_cast<float>(dist1); //uses conversion operator
//for Distance to meters
cout << "\ndist1 = " << mtrs << " meters\n";
Distance dist2(5, 10.25); //uses 2-arg constructor
mtrs = dist2; //also uses conversion op
cout << "\ndist2 = " << mtrs << " meters\n";
// dist2 = mtrs; //error, = won't convert
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -