码迷,mamicode.com
首页 > 其他好文 > 详细

操作符重载

时间:2017-08-17 20:22:28      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:常量   style   返回值   class   stream   取值   pause   abs   for   

#include <iostream>
#include <cstdlib>   //labs
#include <cctype>
using namespace  std;
//表示美元金额的类a
class Money
{
public:
    friend Money operator +(const Money& amount1,const Money& amount2); //友元函数 返回值 Money ,const常量参数不可更改 关键字operator
                                                                        //操作符重载 +
    //返回两个对象之和
    friend bool  operator==(const Money& amount1, const Money& amount2);//操作符重载 ==
    //比较两个对象 相等为ture
    Money(long dollars,int cents);                                     //构造函数 初始化对象为美分形式
    Money(long dollars);                                               //构造重载 $dollars.00
    Money();                                                           //无参构造函数 $0.00
    double get_value() const;                                          //取值成员函数 const修饰该成员函数不更改调用它的对象
    void input (istream& ins) ;                                       //输入
    void output(ostream& outs) const;                                  //输出
private:
    long all_cents;
    };

//主函数
int main()
{
    Money cost(1,05),tax(0,15),exo,total;
    //exo的输入
    cout<<"Enter an amount of money: for exo: ";  
    exo.input(cin);
    cout<<endl;
    cout<<"exo= ";
    exo.output(cout);
    cout<<endl;

    total=cost+exo; 
    cout<<"cost = ";
    cost.output(cout);                             
    cout<<endl;
    /*cout<<"exo = ";
    exo.output(cout);
    cout<<endl;*/
    cout<<"total bill = ";
    total.output(cout);
    cout<<endl;
    if(cost == exo)
         cout<<"Move to another state.\n";
    else
         cout<<"Tings seem normal.\n";
    system("pause");
    return 0;
}
//操作符重载函数
Money operator +(const Money& amount1,const Money& amount2)  //const不能缺少 与类中函数声明保持一致
{
   Money temp;
   temp.all_cents=amount1.all_cents+amount2.all_cents;
   return temp;
}
bool operator ==(const Money& amount1,const Money& amount2)  //同上 cosnt不能少
{
  return(amount1.all_cents==amount2.all_cents);
}
//初始化函数
Money::Money(long dollars,int cents)    //构造函数是类的成员函数(前加类名::) 无返回类型
{
  if(dollars*cents<0)
    { 
      cout<<"Illegal values for dollars and cents.\n";
      exit(1);
    }
  all_cents=dollars*100+cents;          //负数都要为负哦
}

Money::Money(long dollars)
{
  all_cents=dollars*100;
}

Money::Money():all_cents(0)          //初始赋值区域 从冒号开始 没有分号!
{
    //all_cents是 long类型 
    //有意留空
}
//取值函数
double Money::get_value() const      //成员函数不更改调用对象的值 与类中函数声明保持一致
{
 return (all_cents*0.01);           //转换成美元形式 $2.50
}
//输入函数 

void Money::input(istream& ins) 
{
  char one_char,decimal_point,digit1,digit2; //one_char检查正负 存放$ decimal_ppoint存放. digit1,ditit2存放两位小数
  long dollars;                              //存放美元部分
  int  cents;                                //存放美分部分
  bool negative;
  ins>>one_char;
  if(one_char==-)                          //输入负数 将negative置真并读取$符号 输入非负 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=int(digit1-0)*10+int(digit2-0);
  all_cents=dollars*100+cents;
  if(negative)
      all_cents=-all_cents;
}

//输出函数
void Money::output(ostream& outs) const   //输出要以美元(小数)形式输出 $2.50
{
  long positive_cents,dollars,cents;      //用get_value函数改写可以吗???
  positive_cents=labs(all_cents);
  dollars=positive_cents/100;
  cents=positive_cents%100;
if(all_cents<0)                                                    //根据调用的对象判断all_cents的值
   outs<<"-$"<<dollars<<.;                            //根据正负输出不同的形式
else
   outs<<$<<dollars<<.;

if(cents<10)
  outs<<0;
outs<<cents;
}

/*
//改写输出函数 调用get_value
void Money::output(ostream& outs) const   //输出要以美元(小数)形式输出 $2.50
{
  bool positive;
  if(all_cents<0)
      positive=false;
  else
      positive=true;
if(positive)                                                    //根据调用的对象判断all_cents的值
   outs<<"$"<<get_value()<<‘.‘;                                //根据正负输出不同的形式
else
   outs<<"-$"<<abs(get_value())<<‘.‘;
}
*/

 

操作符重载

标签:常量   style   返回值   class   stream   取值   pause   abs   for   

原文地址:http://www.cnblogs.com/qdjzl/p/7383774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!