Month Class : Overloading prefix and post fix?
May 2, 2013 at 4:46pm UTC
I hope this is the right place for this. I just made this account because I am at my wits end with this code. I've been fixing it around and around for 3 days and I have it almost fully done? Usually I would be indifferent and ask fellow classmates for help, but it does not seem the other classmates are interested in attending the final class for extra assistance.
The objective is to build a month class that hold data on the number of the month and the name of the month. Using constructors and overloads, set it up to where you can input either the month or the name and it will output the results for both the month number and name.
Here is the code I have so far:
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
#include<iostream>
#include<string>
using namespace std;
class Month
{
private : string name;
int monthNumber;
public :
Month()
{
monthNumber = 1;
name = "January" ;
}
Month(string nm)
{
name = nm;
setNumber(name);
}
Month(int number)
{
if (number >= 1 && number <=12)
{
monthNumber=number;
}
setName(monthNumber);
}
void setName(int number)
{
if ( number==1)
name="January" ;
else if (number==2)
name="February" ;
else if (number==3)
name="March" ;
else if (number==4)
name="April" ;
else if (number==5)
name="May" ;
else if (number==6)
name="June" ;
else if (number==7)
name="July" ;
else if (number==8)
name="August" ;
else if (number==9)
name="September" ;
else if (number==10)
name="October" ;
else if (number==11)
name="November" ;
else if (number==12)
name="December" ;
else
cout<<"Invalid number!!" <<endl;
}
void setNumber(string nm)
{
if ( nm=="January" )
monthNumber=1;
else if (nm=="February" )
monthNumber=2;
else if (nm=="March" )
monthNumber=3;
else if (nm=="April" )
monthNumber=4;
else if (nm=="May" )
monthNumber=5;
else if (nm=="June" )
monthNumber=6;
else if (nm=="July" )
monthNumber=7;
else if (nm=="August" )
monthNumber=8;
else if (nm=="September" )
monthNumber=9;
else if (nm=="October" )
monthNumber=10;
else if (nm=="November" )
monthNumber=11;
else if (nm=="December" )
monthNumber=12;
else
cout<<"Invalid String!!" <<endl;
}
int getNumber()
{
return monthNumber;
}
string getName()
{
return name;
}
Month Month::operator ++();
Month Month::operator --();
friend ostream &operator << (ostream &, const Month &);
friend istream &operator >> (istream &,Month &);
};
Month Month::operator ++()
{
monthNumber++;
if (monthNumber==13)
{
monthNumber=1;
name="January" ;
}
setName(monthNumber);
return *this ;
}
Month Month::operator --()
{
monthNumber--;
if (monthNumber==0)
{
monthNumber=12;
name="December" ;
}
setName(monthNumber);
return *this ;
}
ostream &operator <<(ostream &strm, const Month &obj)
{
strm << "Month Name: " << obj.name << "\nNumber:" <<obj.monthNumber;
return strm;
}
istream &operator >> (istream &strm, Month &obj)
{
cout<<"Enter in month number: " ;
strm >> obj.monthNumber;
cout<<"Enter in month name: " ;
strm >> obj.name;
return strm;
}
int main()
{
Month m1;
cout<<"Input month name or number for output. \n" ;
cin>>m1;
++m1;
cout<<m1;
m1--;
cout<<m1;
}
It is almost fully compiled if I go by the error list. The only problems I see to be having are with the prefix and postfix overloads.
Any small hints or tips would be greatly appreciated because I am not finding any help in the book or online...
May 2, 2013 at 5:07pm UTC
prefix overload:
Class::operator --()
postfix overload:
Class::operator --(int ) //Note dummy value
May 2, 2013 at 5:17pm UTC
I have an idea how to make setName much more efficient.
Make an array of strings (c-stye strings) and use monthNumber-1 (since arrays start from zero) as index to assign value to variable name.
p.s. iostream includes string, so you don't have to include string.
May 2, 2013 at 5:27pm UTC
iostream includes string, so you don't have to include string.
<iostream>
can include <string> but is not required to do so. In gcc iostream includes only part of <string> library leaving out some important parts.
Do not rely on including headers by another headers.
May 2, 2013 at 5:51pm UTC
Ah right MiiNiPaa! I worked with your advice and it ran nice and smooth! Many thanks!
zoran404, I will give that a try! It will certainly cut back on some of the cluttered mess!
May 2, 2013 at 5:56pm UTC
I forgot to mention the check if number is in range 1-12 for your error: cout<<"Invalid number!!"<<endl;
Topic archived. No new replies allowed.