将结构转换为类
converting structure to class
我正在尝试将结构转换为类,但是我不确定类标头文件中的确切发生的事情。我做了一些更改,有人可以告诉我我还应该改变什么?谢谢你!
结构:
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>
。
相关文章:
- JSON转换为nlohmann JSON-lib中的结构数组
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 从成员指针到整个结构/类的强制转换
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 将字符数组转换为时间结构
- 使用带有链表的堆栈数据结构将中缀转换为后缀
- i2d_X509_REQ_INFO无法正确转换req_info结构
- 在C++中将结构转换和存储为二进制
- 将字符数组转换为结构时出现问题. 结构的字符数组变量溢出
- 转换包含向量 C++ 的语法和结构
- 正在转换结构数据的字节序
- 如何大致将 cpp 代码结构转换为 python
- 将结构 std::memcpy 转换为具有足够容量的 std::vector 是未定义的行为<char>吗?
- C++ 向下转换结构
- 尝试在C++中转换结构时出现奇怪的错误消息
- 向向下转换结构添加属性值会导致分段错误
- C++基于子类型动态强制转换结构
- 从字节缓冲区强制转换结构
- c++中的类型转换结构
- c++的类型转换结构指针指向类型值