C++ 将结构转换为类

C++ 将结构转换为类,c++,class,header,structure,C++,Class,Header,Structure,我试图将结构转换为类,但我不完全确定类头文件中到底有什么内容。我做了一些改变,有人能告诉我还有什么需要改变的吗?谢谢大家! 结构: 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);

我试图将结构转换为类,但我不完全确定类头文件中到底有什么内容。我做了一些改变,有人能告诉我还有什么需要改变的吗?谢谢大家!

结构:

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

您应该始终使用构造函数和析构函数,而不是“init”或“close”方法:
Time(int-hrs、int-secs、int-mins)
。还提供一个默认构造函数,将成员初始化为一些默认值。 考虑将它们定义为unISGNIDint,因为它们不能变为负值。
此外,如果您只想导入流接口,则包含<代码> <代码>,而不是<代码> >代码>。C++结构中的

与类相同,只有结构中的缺省值是公共的,而在类私有的情况下,真正的代码是工作的?如果是这样的话,这个问题可能不属于这里,应该在这里。首先阅读它。你需要将init重命名为Time(它的构造函数),在某些情况下,你需要添加~Time()(析构函数)@WhozCraig来阅读,看看你是否认为这个问题真的符合他们的标准。所以应该是:无效时间(int小时,int分钟,int秒);谢谢,还有什么我要换的吗?
#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