Skip to content

Instantly share code, notes, and snippets.

@id-0x76adf1
Created January 10, 2013 06:30
Show Gist options
  • Select an option

  • Save id-0x76adf1/4499944 to your computer and use it in GitHub Desktop.

Select an option

Save id-0x76adf1/4499944 to your computer and use it in GitHub Desktop.
Write a class that has three unsigned members representing year, month, and day. Write a constructor that takes a string representing a date. This constructor should handle a variety of formats, such as January 1, 1900, 1/1/1900, Jan 1, 1900, and so on.
#include "date.h"
#ifndef DATE_H
#define DATE_H
#include <string>
class date
{
public:
explicit date(const std::string& s);
unsigned get_year() const
{
return year_;
}
unsigned get_month() const
{
return month_;
}
unsigned get_day() const
{
return day_;
}
private:
unsigned year_;
unsigned month_;
unsigned day_;
};
#endif // DATE_H
#include <iostream>
#include "date.h"
int main(int argc, char* argv[])
{
date d1("1/1/1990");
date d2("January 1, 1990");
date d3("Jan 1, 1990");
std::cout << d1.get_year() << " " << d1.get_month() << " " << d1.get_day() << std::endl;
std::cout << d2.get_year() << " " << d2.get_month() << " " << d2.get_day() << std::endl;
std::cout << d3.get_year() << " " << d3.get_month() << " " << d3.get_day() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment