日期类输入运算符重载 :日期类Date,重载输入运算符">>"

这个问题毫无头绪,日期类还能定义,但是运算符的重载一窍不通

函数接口定义:

在这里描述函数接口。例如:
istream &operator>>(istream &in,Date &date);

裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如:
#include <iostream>
using namespace std;
class Date
{
    private:
    int year,month,day;
    public:
    Date(int y = 0,int m = 0,int d = 0)
    {
        year = y;
        month = m;
        day = d;
    }
    int getYear()const
    {
        return year;
    }
    int getMonth()const
    {
        return month;
    }
    int getDay()const
    {
        return day;
    }
    void setYear(int y)
    {
        year = y;
    }
    void setMonth(int m)
    {
        month = m;
    }
    void setDay(int d)
    {
        day = d;
    }
};

/* 请在这里填写答案 */

ostream &operator<<(ostream &out,const Date &date)
{
    cout<<date.getYear()<<'-';
    if(date.getMonth() < 10)
    {
        cout<<'0';
    }
    cout<<date.getMonth()<<'-';
    if(date.getDay() < 10)
    {
        cout<<'0';
    }
    cout<<date.getDay();
}
int main()
{
    Date d1;
    cin>>d1;
    cout<<d1;
    return 0;
}


输入样例:
在这里给出一组输入。例如:

2020 12 1

输出样例:
在这里给出相应的输出。例如:

2020-12-01

c++重载加法。
仅供参考。
就是将参数 const Box& b 计算成新的Box. 没有难度啊。

#include <iostream>
using namespace std;
 
class Box
{
   public:
 
      double getVolume(void)
      {
         return length * breadth * height;
      }
      void setLength( double len )
      {
          length = len;
      }
 
      void setBreadth( double bre )
      {
          breadth = bre;
      }
 
      void setHeight( double hei )
      {
          height = hei;
      }
      // 重载 + 运算符,用于把两个 Box 对象相加
      Box operator+(const Box& b)
      {
         Box box;
         box.length = this->length + b.length;
         box.breadth = this->breadth + b.breadth;
         box.height = this->height + b.height;
         return box;
      }
   private:
      double length;      // 长度
      double breadth;     // 宽度
      double height;      // 高度
};
// 程序的主函数
int main( )
{
   Box Box1;                // 声明 Box1,类型为 Box
   Box Box2;                // 声明 Box2,类型为 Box
   Box Box3;                // 声明 Box3,类型为 Box
   double volume = 0.0;     // 把体积存储在该变量中
 
   // Box1 详述
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);
 
   // Box2 详述
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);
 
   // Box1 的体积
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;
 
   // Box2 的体积
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;
 
   // 把两个对象相加,得到 Box3
   Box3 = Box1 + Box2;
 
   // Box3 的体积
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;
 
   return 0;
}

你写>>
不就是重载输入 方法吗
写几个cin>>
return Date 不就行了。

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632