不要有多用途的复合表达式

例如: d = (a = b + c) + r ; 该表达式既求 a 值又求 d 值。

应该拆分为两个独立的语句: a = b + c; d = a + r;

 

#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
//定义日期类
class Date    
{
    //定义友元重载输入运算符函数
    friend istream& operator >> (istream& input,Date& dt ); 
    //定义友元重载输出运算符函数
    friend ostream& operator<< (ostream& output,Date& dt ); 
    int mo, da, yr;
public:
    Date(void){  //无参数构造函数
        yr = 0;
        mo = 0; 
        da = 0; 
    }
    Date( int y, int m, int d )   //带参数构造函数
    {
        yr = y;
        mo = m; 
        da = d; 
    }
};
//定义">>"运算符重载函数
istream& operator >> ( istream& input, Date& dt )
{
    cout<<"Year:";
    input>>dt.yr;
    cout<<"Month:";
    input>>dt.mo;
    cout<<"Day:";
    input>>dt.da;
    return input;
}

//定义"<<"运算符重载函数
ostream& operator<< ( ostream& output, Date& dt )
{
   output<< dt.yr << '/' << dt.mo << '/' << dt.da<<endl;
   return output;
}

//在main()函数中测试Date类的插入(<<)和提取(>>)运算符

int main(int argc, char** argv) {
      //声明对象
    Date dt1(2002,5,1),dt2;

    //显示dt1对象
    cout<<dt1;

    //对dt2对象进行输入和输出
    cin>>dt2;
    cout<<dt2;
    return 0;
}

 

转载于:https://www.cnblogs.com/borter/p/9413460.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值