将结构转换为类

converting structure to class

本文关键字:转换 结构      更新时间:2023-10-16

我正在尝试将结构转换为类,但是我不确定类标头文件中的确切发生的事情。我做了一些更改,有人可以告诉我我还应该改变什么?谢谢你!

结构:

enter code here: 
#ifndef TIME_H
#define TIME_H
#include <iostream>
struct Time {
int hours, minutes, seconds;
};
void init(Time &t, int hours, int minutes, int seconds);
void normalize(Time &t);
Time operator +(const Time &t1, const Time &t2);
Time &operator +=(Time &t1, const Time &t2);
Time operator -(const Time &t1, const Time &t2);
Time &operator -=(Time &t1, const Time &t2);
bool operator ==(const Time &t1, const Time &t2);
std::ostream &operator <<(std::ostream &os, const Time &t);
std::istream &operator >>(std::istream &is, Time &t);
#endif

类:

#define TIME_H
#include <iostream>
class Time {

public:
void init(Time &t, int hours, int minutes, int seconds);
void normalize(Time &t);
Time operator +(const Time &t1, const Time &t2);
Time &operator +=(Time &t1,const Time &t2);
Time operator -(const Time &t1, const Time &t2);
Time &operator -=(Time &t1, const Time &t2);
bool operator ==(const Time &t1, const Time &t2);
std::ostream &operator <<(std::ostream &os, const Time &t);
std::istream &operator >>(std::istream &is, Time &t);
private: 
int hours, minutes, seconds;

};

#endif

您应该始终使用构造函数和驱动器,而不是'init'-或'Close'-hethods: Time(int hrs, int secs, int mins)。也提供默认构造函数,该构造函数将成员初始化为某些默认值。考虑将它们定义为未静止的INT,因为它们不能变成负面。此外,如果您只想进入流界面。

,请包括<iosfwd>而不是<iostream>