c++自定义输出格式

按照要求格式输出常用方法有两种

问题:按如下格式输出时间
YY:MM:SS


第一种方法:重载流操作符<< ,代码如下
  • Time.h头文件
//Time.h头文件
#ifndef TIME_H_INCLUDED
#define TIME_H_INCLUDED

using namespace std;//必须要加
class Time
{
private:
    int second;//秒
    int minute;//分
    int hour;//时
public:
    Time();
    Time(int h,int m,int s);
    friend Time & operator++(Time &time);//返回值是一个对象,加个引用比较好
    friend ostream & operator<<(ostream & out,Time & time);//这里把重载的<<方法声明为友元函数是因为该方法需要访问Time类的私有属性
};

/*而且需要写成传入自身参数形式,因为这是友元函数重载一元运算符,不是成员函数方式重载的,
成员函数这里就不用了传入参数,本身有this指针

如果是二元运算符用友元函数重载二院运算符的话,则需要写成传入两个参数的形式
成员函数重载二院运算符时就只需要传入一个参数就行,另一个是本身的this指针

*/




#endif // TIME_H_INCLUDED
  • Time.cpp具体实现
//Time.cpp具体实现类
#include <iostream>
#include <string>
#include "Time.h"


//必须使用c++2011版本编译器才能使用string的相关操作
using namespace std;


Time::Time()
    {
        this->second = 0;
        this->minute = 0;
        this->hour = 0;
    }

Time::Time(int h,int m,int s)//需要加Time::
{
    second = s;
    minute = m;
    hour = h;
}

Time & operator++(Time &time)//因为他不是成员函数,所以不要使用Time::(类名加作用域解析操作符)限定符。另外不要再定义中使用friend关键字
{
    time.second += 1;//秒
    time.minute = time.minute + time.second/60;//分
    time.hour = time.hour + time.minute/60;//时
    if(time.second==60)
    {
        time.second = 0;//重置秒数
    }
    if(time.minute==60)
    {
        time.minute = 0;//重置分数
    }
    if(time.hour==24)//重置时
    {
        time.hour = 0;
    }


    return time;

}

//重载流操作符<< 实现自定义格式输出,另一种tostring方法见自己CSDN博客
ostream & operator<<(ostream & out,Time & time)
{
    out << time.hour << ":" << time.minute << ":" << time.second;
    return out;
}
  • main.cpp主函数调用
//main.cpp主函数调用
#include <iostream>
#include "Time.h"
using namespace std;

int main()
{
    Time t1;
    Time t2(22,59,59);
    cout << ++t2 << endl;
    return 0;
}

运行结果:
在这里插入图片描述


第二种方法:利用c++2011新标准(2011年以后的都支持如果是codeblocks需要选择编译器版本见 https://blog.csdn.net/lereno/article/details/90266505)直接把要求格式tostring,再直接输出,代码如下:
  • Time.h头文件
//Time.h头文件
#ifndef TIME_H_INCLUDED
#define TIME_H_INCLUDED

using namespace std;//必须要加
class Time
{
private:
    int second;//秒
    int minute;//分
    int hour;//时
public:
    Time();
    Time(int h,int m,int s);
    friend string operator++(Time &time);//返回值是一个对象,加个引用比较好,
};

/*而且需要写成传入自身参数形式,因为这是友元函数重载一元运算符,不是成员函数方式重载的,
成员函数这里就不用了传入参数,本身有this指针

如果是二元运算符用友元函数重载二院运算符的话,则需要写成传入两个参数的形式
成员函数重载二院运算符时就只需要传入一个参数就行,另一个是本身的this指针

*/




#endif // TIME_H_INCLUDED
  • Time.cpp具体实现类
//Time.cpp具体实现类
#include <iostream>
#include <string>
#include "Time.h"


//必须使用c++2011版本编译器才能使用string的相关操作
using namespace std;


Time::Time()
    {
        this->second = 0;
        this->minute = 0;
        this->hour = 0;
    }

Time::Time(int h,int m,int s)//需要加Time::
{
    second = s;
    minute = m;
    hour = h;
}

string operator++(Time &time)//因为他不是成员函数,所以不要使用Time::(类名加作用域解析操作符)限定符。另外不要再定义中使用friend关键字
{
    time.second += 1;//秒
    time.minute = time.minute + time.second/60;//分
    time.hour = time.hour + time.minute/60;//时
    if(time.second==60)
    {
        time.second = 0;//重置秒数
    }
    if(time.minute==60)
    {
        time.minute = 0;//重置分数
    }
    if(time.hour==24)//重置时
    {
        time.hour = 0;
    }
    string str = std::to_string(time.hour)  + ":" + std::to_string(time.minute) + ":" + std::to_string(time.second);

    return str;//也可以直接返回一个Time对象就是

}
  • main.cpp主函数调用
//main.cpp主函数调用
#include <iostream>
#include "Time.h"
using namespace std;

int main()
{
    Time t1;
    Time t2(22,59,59);
    cout << ++t2 << endl;
    return 0;
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yhblog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值