zoukankan      html  css  js  c++  java
  • 不要有多用途的复合表达式

    例如: 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;
    }
  • 相关阅读:
    [BZOJ4755][JSOI2016]扭动的回文串(manacher+Hash)
    十二省联考2019部分题解
    [BZOJ2959]长跑(LCT+并查集)
    [BZOJ4541][HNOI2016]矿区(平面图转对偶图)
    笛卡尔树
    [CF896C]Willem, Chtholly and Seniorious(珂朵莉树)
    [BZOJ4349]最小树形图
    [BZOJ1858][SCOI2010]序列操作(线段树)
    [PA2014]Parking
    [PA2014]Budowa
  • 原文地址:https://www.cnblogs.com/borter/p/9413460.html
Copyright © 2011-2022 走看看