vgghhhgllljh 2021-11-30 19:49 采纳率: 50%
浏览 658
已结题

日期类输入运算符重载 :日期类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

  • 写回答

2条回答 默认 最新

  • CSDN专家-Time 2021-12-01 10:23
    关注

    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 不就行了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 请提交代码 6月28日
  • 系统已结题 5月21日
  • 已采纳回答 5月13日
  • 修改了问题 11月30日
  • 展开全部

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装