0

I'm trying to convert structures to class, but I'm not completely sure of what exactly goes in a class header file. I've made some changes, can someone tell me what else I should change? Thank you!

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);
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

class:

#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
adin
  • 1
  • 1
  • In C++ struct is the same as class, only difference - by default in struct is public and in class - private – Anton Apr 29 '17 at 18:17
  • Does the *real* code *work* ? If so, this question probably doesn't belong here, and should be on [code review](https://codereview.stackexchange.com/) – WhozCraig Apr 29 '17 at 18:17
  • 1
    http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c. Read it first. – Cengiz Kandemir Apr 29 '17 at 18:18
  • 1
    You shuold rename init to Time (it's constructor), in some cases you need to add ~Time() (destructor) – Anton Apr 29 '17 at 18:18
  • @WhozCraig Read https://codereview.meta.stackexchange.com/questions/5777/a-guide-to-code-review-for-stack-overflow-users and see if you think this question really meets their criteria. – Barmar Apr 29 '17 at 19:01

1 Answers1

0

You should always use the constructor and destructor instead of 'init'- or 'close'-methods: Time(int hrs, int secs, int mins). Provide a default constructor too which initializes the members to some default values. Consider defining them as unisgned int, since they cannot become negative. Further, include <iosfwd> instead of <iostream> if you just want acces to the stream interfaces.

The Techel
  • 918
  • 8
  • 13