#include "Float.h"
int main()
{
Float f1(2.4), f2(3.5);
cout << "f1 : " << f1 << endl;
cout << "f2 : " << f2 << endl;
cout << "f1+f2 : " << f1+f2 << endl;
cout << "f1*f2 : " << f1*f2 << endl;
cout << "f1^3.2: " << (f1^3.2) << endl; //为什么此处"(f1^3.2)"一定要加括号??
cout << "f1+=f2: " << (f1+=f2) << endl;
return 0;
}
// Float.h
#ifndef FLOAT_H
#define FLOAT_H
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
class Float
{
private:
float F;
public:
Float(float x) : F(x) {}
Float(): F(0) {}
Float operator + (const Float& F2) const
{return Float(F + F2.F); }
Float operator * (const Float& F2) const
{return Float(F * F2.F); }
Float operator ^ (const float& f) const
{return Float(pow(F,f));}
operator float() {return F;}
Float& operator += (const Float& F2)
{
F += F2.F;
return *this;
}
};
#endif