1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include <iostream>
#include <cstdlib> // library for exit()
#include <cctype> // library for tolower()
using namespace std;
/*Define a class called Month that is an abstract data type for a month. Your class will
have one member variable of type int to represent a month (1 for January, 2 for February
, and so forth). Include all the following member functions: a constructor to set the month using the first three letters in the name of the month as three arguments, a constructor to
set the month using an integer as an argument (1 for January, 2 for February, and so forth),
a default constructor, an input function that reads the month as an integer, an input
function that reads the month as the first three letters in the name of the month, an
output function that outputs the month as an integer, an output function that outputs
the month as the first three letters in the name of the month, and a member function
that returns the next month as a value of type Month. The input and output functions
will each have one formal parameter for the stream. Embed your class definition in a test program.*/
class Month {
public:
Month (char first, char second, char third);
Month (int aMonth);
Month ();
void inputNumber (istream& fin);
void inputLetter (istream& fin);
void outputNumber (ostream& fout);
void outputLetter (ostream& fout);
Month nextMonth();
private:
int month;
};
int main() {
Month test1, test2;
cout << "Enter the month by number: ";
test1.inputNumber(cin);
cout << "The currunt month in number is: ";
test1.outputNumber(cout);
cout << "The currunt month in letter is: ";
test1.outputLetter(cout);
cout << "The next month after this month is: ";
test1.nextMonth(); // Can I get a return value of type Month here?????????????????????????
//test1.outputLetter(cout);
//cout << "Or in number is: ";
//test1.outputNumber(cout);
//cout << endl;
cout << "Enter the month in character (first 3 letter): ";
test2.inputLetter(cin);
cout << "The currunt month in number is: ";
test2.outputNumber(cout);
cout << "The currunt month in letter is: ";
test2.outputLetter(cout);
cout << "The next month after this month is: ";
test2.nextMonth();
test2.outputLetter(cout);
cout << "Or in number is: ";
test2.outputNumber(cout);
cout << endl;
return 0;
}
Month::Month (char first, char second, char third) {
if (first == 'j' && second == 'a' && third == 'n')
month = 1;
else if (first == 'f' && second == 'e' && third == 'b')
month = 2;
else if (first == 'm' && second == 'a' && third == 'r')
month = 3;
else if (first == 'a' && second == 'p' && third == 'r')
month = 4;
else if (first == 'm' && second == 'a' && third == 'y')
month = 5;
else if (first == 'j' && second == 'u' && third == 'n')
month = 6;
else if (first == 'j' && second == 'u' && third == 'l')
month = 7;
else if (first == 'a' && second == 'u' && third == 'g')
month = 8;
else if (first == 's' && second == 'e' && third == 't')
month = 9;
else if (first == 'o' && second == 'c' && third == 't')
month = 10;
else if (first == 'n' && second == 'o' && third == 'v')
month = 11;
else if (first == 'd' && second == 'e' && third == 'c')
month = 12;
else {
cout << "Invalid month!\n";
exit(1);
}
}
Month::Month (int aMonth) {
if (aMonth > 12 || aMonth < 1) {
cout << "Invalid month!\n";
exit(1);
}
else
month = aMonth;
}
Month::Month () {
}
void Month::inputNumber(istream& fin) {
fin >> month;
if (month > 12 || month < 1) {
cout << "Invalid month!\n";
exit(1);
}
}
void Month::inputLetter(istream& fin) {
char first, second, third;
fin >> first >> second >> third;
first = tolower(first);
second = tolower(second);
third = tolower(third);
if (first == 'j' && second == 'a' && third == 'n')
month = 1;
else if (first == 'f' && second == 'e' && third == 'b')
month = 2;
else if (first == 'm' && second == 'a' && third == 'r')
month = 3;
else if (first == 'a' && second == 'p' && third == 'r')
month = 4;
else if (first == 'm' && second == 'a' && third == 'y')
month = 5;
else if (first == 'j' && second == 'u' && third == 'n')
month = 6;
else if (first == 'j' && second == 'u' && third == 'l')
month = 7;
else if (first == 'a' && second == 'u' && third == 'g')
month = 8;
else if (first == 's' && second == 'e' && third == 't')
month = 9;
else if (first == 'o' && second == 'c' && third == 't')
month = 10;
else if (first == 'n' && second == 'o' && third == 'v')
month = 11;
else if (first == 'd' && second == 'e' && third == 'c')
month = 12;
else {
cout << "Invalid month!\n";
exit(1);
}
}
Month Month::nextMonth() {
if (month < 12)
month++;
else
month = 1;
return month;
}
void Month::outputNumber(ostream& fout) {
fout << month << endl;
}
void Month::outputLetter(ostream& fout) {
if (month == 1)
fout << "Jan";
else if (month == 2)
fout << "Feb";
else if (month == 3)
fout << "Mar";
else if (month == 4)
fout << "Apr";
else if (month == 5)
fout << "May";
else if (month == 6)
fout << "Jun";
else if (month == 7)
fout << "Jul";
else if (month == 8)
fout << "Aug";
else if (month == 9)
fout << "Sep";
else if (month == 10)
fout << "Oct";
else if (month == 11)
fout << "Nov";
else if (month == 12)
fout << "Dec";
fout << endl;
}
/*
Sample Dialouge:
Enter the month by number: 8
The currunt month in number is: 8
The currunt month in letter is: Aug
The next month after this month is: Sep
Or in number is: 9
Enter the month in character (first 3 letter): oct
The currunt month in number is: 10
The currunt month in letter is: Oct
The next month after this month is: Nov
Or in number is: 11 */
|