按照要求格式输出常用方法有两种
问题:按如下格式输出时间
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;
}
运行结果: