Created
January 10, 2013 06:30
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "date.h" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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