最近在学习C++操作符重载知识点,为了更加深入的理解,就写了一段小代码实现了操作符<,<=,>,>=,>>,<<的重载以及测试。下面是我写的代码:
class Money {
public:
friend bool operator < (const Money& amount1, const Money& amount2);
friend bool operator <= (const Money& amount1, const Money& amount2);
friend bool operator > (const Money& amount1, const Money& amount2);
friend bool operator >= (const Money& amount1, const Money& amount2);
Money(long dollars);
Money();
double get_value() const;
long get_all_cents() const;
void set_all_cents(long allcents);
Money percent(int percent_figure) const;
friend istream& operator >> (istream& ins, Money& amount);
friend ostream& operator << (ostream& outs, Money& amount);
private:
long all_cents;
};
/*
普通函数,将数字转化成int型
*/
int digit_to_int(char c);
void main()
{
Money amount, amount1, amount2;
bool lower, lagre, lower_equal, lagre_equal;
//声明一个输入流和一个输出流,将输入、输出流链接到文件并检查是否能够成功打开文件!
ifstream in_stream;
ofstream out_stream;
in_stream.open("infile.txt");
if(in_stream.fail())
{
cout<<"Input file opening failed!";
exit(1);
}
out_stream.open("outfile.txt");
if(out_stream.fail())
{
cout<<"Output file opening failed!\n";
exit(1);
}
//使用重载的输入流输入数据
in_stream >> amount >> amount1 >> amount2;
//测试重载操作符<、<=、>、>=
lower = amount1 < amount2;
lagre = amount1 > amount2;
lower_equal = amount1 <= amount2;
lagre_equal = amount1 >= amount2;
//测试重载操作符<<,将数据输出到文件
out_stream << amount << amount1 << amount2
<< "\ncopied from the file infile.txt"
<< "\n" << amount.percent(10) << amount1.percent(100) << amount2.percent(1);
//将数据输出到屏幕
cout << amount << amount1 << amount2
<< "\ncopied from the file infile.txt"
<< "\n" << amount.percent(10) << amount1.percent(100) << amount2.percent(1)
<< "\n" << lower << lagre << lower_equal << lagre_equal;
//关闭输入、输出流
in_stream.close();
out_stream.close();
}
/*
类Money的友元函数,重载操作符>>的函数定义;
函数有两个参数,第一个参数是输入流类型的引用,第二个参数是类Money类型的引用;
返回值输入流类型的引用。
*/
istream& operator >> (istream& ins, Money& amount)
{
char one_char,decimal_point,digit1,digit2;
long dollars,all_cents;
int cents;
bool negative;
ins >> one_char;
if(one_char == '-')
{
negative = true;
ins >> one_char;
}else{
negative = false;
}
ins >> dollars >> decimal_point >> digit1 >>digit2;
if(one_char != '$' || decimal_point != '.' || !isdigit(digit1) || !isdigit(digit2))
{
cout << "Error illegal form for Money input\n";
exit(1);
}
cents = digit_to_int(digit1)*10 + digit_to_int(digit2);
all_cents = dollars*100 + cents;
amount.set_all_cents(all_cents);
if(negative)
{
amount.set_all_cents(-all_cents);
}
return ins;
}
int digit_to_int(char c)
{
return (int(c) - int('0'));
}
/*
类Money的友元函数,重载操作符<<的函数定义;
函数有两个参数,第一个参数是输出流类型的引用,第二个参数是类Money类型的引用;
返回值输入流类型的引用。
*/
ostream& operator << (ostream& outs, Money& amount)
{
long positive_cents,dollars,cents;
positive_cents = labs(amount.get_all_cents());
dollars = positive_cents/100;
cents = positive_cents%100;
if(amount.get_all_cents() < 0)
outs << "-$" << dollars << '.';
else
outs << "$" << dollars << '.';
if(cents<10)
outs << '0';
outs << cents;
return outs;
}
Money::Money(long dollars) : all_cents(dollars*100)
{
}
Money::Money() : all_cents(0)
{
}
/*
类Money的成员函数定义,函数无参数,返回double类型的账户余额值。
*/
double Money:: get_value() const
{
return (all_cents*0.01);
}
/*
类Money的成员函数定义,函数无参数,返回long类型的账户余额的总美分值。
*/
long Money:: get_all_cents() const
{
return all_cents;
}
/*
类Money的成员函数定义,函数有一个参数,为long类型的账户余额的总美分值,函数实现成员变量的赋值。
*/
void Money:: set_all_cents(long allcents)
{
all_cents = allcents;
}
/*
类Money的成员函数定义,函数有一个参数,为int类型,返回一个类Money类型的数据;
函数实现对象中余额的百分比。
*/
Money Money:: percent(int percent_figure) const
{
Money temp;
temp.all_cents = all_cents * 0.01 * percent_figure;
return temp;
}
/*
类Money的友元函数,操作符<的函数定义,函数有两个常量参数,为类Money类型的引用,返回一个bool类型的值;
*/
bool operator < (const Money& amount1, const Money& amount2)
{
return (amount1.get_all_cents() < amount2.get_all_cents());
}
/*
类Money的友元函数,操作符<=的函数定义,函数有两个常量参数,为类Money类型的引用,返回一个bool类型的值;
*/
bool operator <= (const Money& amount1, const Money& amount2)
{
return (amount1.get_all_cents() < amount2.get_all_cents() || (amount1.get_all_cents() == amount2.get_all_cents()));
}
/*
类Money的友元函数,操作符>的函数定义,函数有两个常量参数,为类Money类型的引用,返回一个bool类型的值;
*/
bool operator > (const Money& amount1, const Money& amount2)
{
return (amount1.get_all_cents() > amount2.get_all_cents());
}
/*
类Money的友元函数,操作符>=的函数定义,函数有两个常量参数,为类Money类型的引用,返回一个bool类型的值;
*/
bool operator >= (const Money& amount1, const Money& amount2)
{
return (amount1.get_all_cents() > amount2.get_all_cents() || (amount1.get_all_cents() == amount2.get_all_cents()));
}
由于我用的事VC++ 6.0的编辑器,在实现操作符>>,<<的重载时,总是提示不能访问类的私有成员变量。但是我已经把操作符声明为类的友元函数,按照C++操作符重载的语法,是可以访问的,可能因为编辑器的原因,所以我写了两个类的成员函数,用来获取私有成员变量和赋值。