patterncppMinor
Implementing a Yearmonth class
Viewed 0 times
implementingclassyearmonth
Problem
Recently I wrote a program that was required to handle year and month data and so I wrote this class to encapsulate that handling. What I needed was a way to initialize the
ymtest.cpp
Yearmonth.h
Yearmonth.cpp
```
#include
#include "Yearmonth.h"
namespace YM {
Yearmonth::Yearmonth()
{
time_t tt;
time(&tt);
tm *t = localtime(&tt);
myyear = t->tm_year + 1900;
mymonth = t->tm_mon;
}
Yearmonth::Yearmonth(unsigned ayear, unsigned amonth)
: myyear(ayear), mymonth(amonth-1)
{
myyear += mymonth/12;
mymonth %= 12;
}
unsigned Yearmonth::year() const
{
return myyear;
}
unsigned Yearmonth::month() const
{
return mymonth+1;
}
Yearmonth &Yearmonth::operator+=(const unsigned mon)
{
mymonth += mon;
myyear += mymonth/12;
mymonth %= 12;
return *this;
}
std::ostream& operator<<(std::ostream &out, const Yearmont
Yearmonth object based on the current local time, and allow a simple method of calculating future Yearmonth values based on a duration in months. This sample code illustrates how I use it:ymtest.cpp
#include
#include "Yearmonth.h"
int main()
{
YM::Yearmonth ym; // today
std::cout << ym << '\n';
ym += 2; // 2 months from now
std::cout << ym << '\n';
ym += 14; // test year increment
std::cout << ym << '\n';
}Yearmonth.h
#ifndef YEARMONTH_H
#define YEARMONTH_H
#include
namespace YM {
class Yearmonth
{
public:
// construct with today's year and month
Yearmonth();
// construct with year, month (1=Jan, 12=Dec)
Yearmonth(unsigned ayear, unsigned amonth);
// increment by given number of months
Yearmonth &operator+=(const unsigned mon);
// return year
unsigned year() const;
// return month (1=Jan, 12=Dec)
unsigned month() const;
// prints to ostream. E.g. 2014 Dec ==> "201412"
friend std::ostream& operator<<(std::ostream &out, const Yearmonth &ym);
private:
unsigned myyear;
unsigned mymonth;
};
}
#endif //YEARMONTH_HYearmonth.cpp
```
#include
#include "Yearmonth.h"
namespace YM {
Yearmonth::Yearmonth()
{
time_t tt;
time(&tt);
tm *t = localtime(&tt);
myyear = t->tm_year + 1900;
mymonth = t->tm_mon;
}
Yearmonth::Yearmonth(unsigned ayear, unsigned amonth)
: myyear(ayear), mymonth(amonth-1)
{
myyear += mymonth/12;
mymonth %= 12;
}
unsigned Yearmonth::year() const
{
return myyear;
}
unsigned Yearmonth::month() const
{
return mymonth+1;
}
Yearmonth &Yearmonth::operator+=(const unsigned mon)
{
mymonth += mon;
myyear += mymonth/12;
mymonth %= 12;
return *this;
}
std::ostream& operator<<(std::ostream &out, const Yearmont
Solution
I only have a few small remarks to make:
-
In your header file, don't include `
-
In your header file, don't include `
: include instead which contains the forward declarations for every type in .
-
Also, you only use std::ostream in your source file, so you could simply include there.
-
Several times, you compute mymonth / 12 and mymonth % 12. If your class is designed to be used intensively, you could consider using std::div(mymonth, 12) which will compute both values at once and may therefore be slightly faster (if you really need it).
-
You may want to prefix time_t, time, tm and localtime with std::. Them coming from the C standard library doesn't prevent you to use std::.
-
Using a const unsigned parameter seems pretty useless. I don't have strong opinions on the const` on value parameters, but you could safely drop it since it adds little value.Context
StackExchange Code Review Q#87664, answer score: 4
Revisions (0)
No revisions yet.