时间类是一种用于表示和操作时间的自定义数据类型。它可以用于存储和处理小时、分钟和秒钟等时间单位。
下面是一个简单的时间类的实现示例:
#include <iostream>
class Time {
private:
int hours; // 小时
int minutes; // 分钟
int seconds; // 秒钟
public:
// 默认构造函数
Time() {
hours = 0;
minutes = 0;
seconds = 0;
}
// 带参构造函数
Time(int h, int m, int s) {
hours = h;
minutes = m;
seconds = s;
}
// 获取小时
int getHours() const {
return hours;
}
// 获取分钟
int getMinutes() const {
return minutes;
}
// 获取秒钟
int getSeconds() const {
return seconds;
}
// 设置时间
void setTime(int h, int m, int s) {
hours = h;
minutes = m;
seconds = s;
}
// 重载输出运算符
friend std::ostream& operator<<(std::ostream& os, const Time& t) {
os << t.hours << ":" << t.minutes << ":" << t.seconds;
return os;
}
};
int main() {
Time t1(10, 30, 45);
Time t2;
std::cout << "t1: " << t1 << std::endl;
std::cout << "t2: " << t2 << std::endl;
t2.setTime(12, 15, 30);
std::cout << "t2 (after setting time): " << t2 << std::endl;
return 0;
}
在上面的示例中,Time类封装了三个私有成员变量hours、minutes和seconds,分别表示时间的小时、分钟和秒钟。它提供了默认构造函数和带参构造函数来初始化时间对象。
Time类还提供了一些成员函数,如getHours()、getMinutes()和getSeconds()用于获取时间的小时、分钟和秒钟,以及setTime()用于设置时间。
此外,Time类还重载了输出运算符<<,以便能够直接输出时间对象的内容。
在main函数中,我们创建了两个Time对象t1和t2,并使用setTime()函数设置t2的时间。最后,我们输出了这些时间对象的内容。
6 实例——MyTime的实现
//------mytime.h
#ifndef MYTIME_H
#define MYTIME_H
#include <iostream>
using namespace std;
class Time
{
//----------私有成员,类中的成员默认是私有的
private:
int hours;
int mintues;
//----------共有成员
public:
Time(); //默认构造函数
Time(int h, int m = 0); //显式构造函数
Time(const Time &); //拷贝构造函数
~Time(); //析构函数
void AddMin(int m);
void AddHour(int h);
void reset(int h = 0, int m = 0);
//------展示函数show()
void Time::show() const
{
cout << "hours:" << hours << " " << "mintues:" << mintues << " ";
}
Time operator+(const Time &t) const; //运算符重载
Time operator-(const Time &t) const;
Time operator*(double n) const;
friend Time operator*(double n, const Time &t) //友元;
{
return t*n; //在这里又调用了重载运算符 operator*(double n) const;
} //内联形式的定义;
friend ostream & operator<<(ostream &os, const Time &t); //一个双目运算符在重载时,如果是以友元的形式声明的,那么他有两个形参;如果是类的成员函数,那么他只有一个形参;
};
//-------时间重置,内联函数
inline void Time::reset(int h, int m)
{
hours = h;
mintues = m;
}
#endif
//--mytime.cpp
#include <iostream>
#include "mytime.h"
using namespace std;
//-------默认构造函数
Time::Time()
{
hours = mintues = 0;
cout << "调用默认构造函数" << endl;
}
//------显式的构造函数
Time::Time(int h, int m) :hours(h), mintues(m)
{
cout << "调用显式构造函数" << endl;
}
//------拷贝构造函数
Time::Time(const Time &t)
{
hours = t.hours;
mintues = t.mintues;
cout << "调用拷贝构造函数" << endl;
}
//------析构函数
Time::~Time()
{
cout << "调用了析构函数" << endl;
}
//-------小时相加
void Time::AddHour(int h)
{
hours += h;
}
//------分钟相加
void Time::AddMin(int m)
{
mintues += m;
hours += mintues / 60;
mintues %= 60;
}
//------重载+号
Time Time::operator+(const Time &t) const
{
Time sum;
sum.mintues = mintues + t.mintues;
sum.hours = hours + t.hours + sum.mintues / 60;
sum.mintues = sum.mintues % 60;
return sum;
}
//------重载-号
Time Time::operator-(const Time &t) const
{
Time diff;
int time1 = hours * 60 + mintues;
int time2 = t.hours * 60 + t.mintues;
diff.hours = (time1 - time2) / 60;
diff.mintues = (time1 - time2) % 60;
return diff;
}
//-------重载乘号
Time Time::operator*(double n) const
{
Time result;
long totalMintues = n*hours * 60 + n*mintues;
result.hours = totalMintues / 60;
result.mintues = totalMintues % 60;
return result;
}
//-------友元输出操作符
ostream & operator<<(ostream &os, const Time &t) //友元在类外定义的时候,不需要添加friend;
{
os << "hours:" << t.hours << " " << "mintues:" << t.mintues << " ";
return os;
}
//-----------------------
//main.cpp
//不用先生
//------------------------
#include <iostream>
#include "mytime.h"
using namespace std;
int main()
{
{
Time eat_breakfast(0, 45);
Time eat_lunch(1, 0);
Time eat_dinner(1, 30);
Time swiming(0, 45); //非const对象,既可以调用const成员函数,也可以调用非const成员。
const Time study(8, 5); //const对象只能调用const成员函数。
// study_cut_swim;
Time study_cut_swim = study - swiming; //调用运算符重载后的Time类的减号;
Time Eat_time_day = eat_breakfast + eat_dinner + eat_lunch; //调用了重载以后的加法;
cout << "学习比游泳多花" << study_cut_swim << endl; //调用友元输出运算符<<
cout << "每周吃饭所花费的时间为" << (7 * Eat_time_day) << endl; //调用了友元乘法以及输出运算符;
}
system("pause");
return 0;
}