Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: 周政耀
- //ID Number: B10515009
- //Problem: Design Month Class
- #include <iostream>
- #include <string>
- using namespace std;
- string MONTH_TABLE[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
- class Month{
- private:
- int month_;
- public:
- Month();
- Month(int);
- Month(char, char, char);
- void inputInt();
- void inputFirstThreeLetters();
- void outputInt();
- void outputFirstThreeLetters();
- Month nextMonth();
- };
- Month::Month(){
- //default
- this->month_ = 1;
- }
- Month::Month(int month){
- // initializing and use number to represent a month
- this->month_ = month;
- }
- Month::Month(char firstLetter, char secondLetter, char thirdLetter){
- // initializing and use the first three letter to represent a month
- string month = "";
- month += firstLetter;
- month += secondLetter;
- month += thirdLetter;
- for(int i=0;i<12;++i){
- if(month == MONTH_TABLE[i]){
- this->month_ = i+1;
- break;
- }
- }
- }
- void Month::inputInt(){
- // reads the month as an integer
- int month;
- cin >> month;
- this->month_ = month;
- }
- void Month::inputFirstThreeLetters(){
- // reads the month as the first three letters in the name of the month
- char tempLetter;
- string month = "";
- for(int i=0;i<3;++i){
- cin >> tempLetter;
- month += tempLetter;
- }
- for(int i=0;i<12;++i){
- if(month == MONTH_TABLE[i]){
- this->month_ = i+1;
- break;
- }
- }
- }
- void Month::outputInt(){
- // output the month as an integer
- cout << this->month_;
- }
- void Month::outputFirstThreeLetters(){
- // outputs the month as the first three letters in the name of the month
- cout << MONTH_TABLE[this->month_-1];
- }
- Month Month::nextMonth(){
- //returns the next month as a value of type Month
- return Month(this->month_+1!=13?this->month_+1:1);
- }
- //input-main1.cpp
- int main(void)
- {
- Month month1, month2(2), month3('M', 'a', 'r'), month4, month5, month6;
- month4 = month3.nextMonth();
- month5.inputInt();
- month6.inputFirstThreeLetters();
- cout << "Month1 = ";
- month1.outputInt();
- cout << ' ';
- month1.outputFirstThreeLetters();
- cout << endl;
- cout << "Month2 = ";
- month2.outputInt();
- cout << ' ';
- month2.outputFirstThreeLetters();
- cout << endl;
- cout << "Month3 = ";
- month3.outputInt();
- cout << ' ';
- month3.outputFirstThreeLetters();
- cout << endl;
- cout << "Month4 = ";
- month4.outputInt();
- cout << ' ';
- month4.outputFirstThreeLetters();
- cout << endl;
- cout << "Month5 = ";
- month5.outputInt();
- cout << ' ';
- month5.outputFirstThreeLetters();
- cout << endl;
- cout << "Month6 = ";
- month6.outputInt();
- cout << ' ';
- month6.outputFirstThreeLetters();
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement