【发布时间】:2017-09-27 16:22:03
【问题描述】:
我正在尝试将结构转换为类,但我不完全确定类头文件中的具体内容。我做了一些改变,谁能告诉我还有什么需要改变的?谢谢!
结构:
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
【问题讨论】:
-
在 C++ 中,struct 与 class 相同,唯一不同的是 - 默认情况下,struct 中是 public,而 class 中是 private
-
real 代码是否工作?如果是这样,这个问题可能不属于这里,应该在code review
-
你应该将init重命名为Time(它的构造函数),在某些情况下你需要添加~Time()(析构函数)
-
@WhozCraig 阅读codereview.meta.stackexchange.com/questions/5777/…,看看你是否认为这个问题真的符合他们的标准。
标签: c++ class header structure