📄 nofri.cpp
字号:
// nofri.cpp
// limitation to overloaded + operator
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance //English Distance class
{
private:
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0) //constructor (no args)
{ } //constructor (one arg)
Distance(float fltfeet) //convert float to Distance
{ //feet is integer part
feet = static_cast<int>(fltfeet);
inches = 12*(fltfeet-feet); //inches is what's left
}
Distance(int ft, float in) //constructor (two args)
{ feet = ft; inches = in; }
void showdist() //display distance
{ cout << feet << "\'-" << inches << '\"'; }
Distance operator + (Distance);
};
//--------------------------------------------------------------
//add this distance to d2
Distance Distance::operator + (Distance d2) //return the sum
{
int f = feet + d2.feet; //add the feet
float i = inches + d2.inches; //add the inches
if(i >= 12.0) //if total exceeds 12.0,
{ i -= 12.0; f++; } //less 12 inches, plus 1 foot
return Distance(f,i); //return new Distance with sum
}
////////////////////////////////////////////////////////////////
int main()
{
Distance d1 = 2.5; //constructor converts
Distance d2 = 1.25; //float feet to Distance
Distance d3;
cout << "\nd1 = "; d1.showdist();
cout << "\nd2 = "; d2.showdist();
d3 = d1 + 10.0; //distance + float: OK
cout << "\nd3 = "; d3.showdist();
// d3 = 10.0 + d1; //float + Distance: ERROR
// cout << "\nd3 = "; d3.showdist();
cout << endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -