Hey guys!
I posted a msg b4 about having to include an assignment operator in my code for a program that outputs a calender. My tutor has already declared it for a hint for us as:
Calender& operator= (const Calender& rhs);
The thing is, we don't actually need to incorporate it into our code. Our tutor just wants us to define it, but I'm unsure of how to do that.
mOST of my code is as follows (by the way, I know some of it is not right!!
//assign2.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
class Calender
{
public:
Calender(); //constructor requests user to input a 4 digit year
Calender& operator= (const Calender& rhs); //Assignment operator
void CreateCalender(); //Fills 'theCalender' array with all the dates of the year
void Display(); //Displays the calender to the screen
int FirstDayOfMonth(int mm); //Calulates which day
// of the week the first day of month occurs and returns an int // between 0-6
bool IsLeapYear(); //Returns true if the year is a leap yr, or else returns false
private:
int year; //Holds the 4 digit year
int theCalender[12][35]; //holds all the dates for the year
};
//************************************************** *************************************8
//constructor
//************************************************** **************************************
Calender::Calender()
{
cout << " Please enter a year" << endl;
cin >> year;
if (year < 1582 || year > 4902)
cout << "Year is before or after Gregorian caldender was accepted. Please enter a year between 1582 and 4902" << endl;
}
//Assignment operator
Calender& operator= (const Calender& rhs);
{
WHAT DO I WRITE HERE?!
}
//************************************************** **********************************8
//Fills 'theCalender' array with all the dates of the year
//************************************************** **********************************
void Calender::CreateCalender()
{
int i, j, mm, date, temp, loopCount;
mm = 1;
date = 1;
//initialising the days
int theCalender[12][35];
for (int i=0; i<12; i++)
{
for (int j=0; j<35; j++)
theCalender[i][j] = 0;
}
while (mm <= 12)
{
date = 1;
if (mm ==1) //if jan
for (int j =0; j <31; j++)
{
theCalender[0][j] = date;
date++;
}
date = 1;
if (mm == 2) //if feb
{
if (IsLeapYear())
for (j =0; j <29; j++)
{
theCalender[1][j]=date;
date++;
}
else
for (j =0; j <28; j++)
{
theCalender[1][j]=date;
date++;
}
}
date=1;
if (mm ==3) //if march
for (j =0; j <31; j++)
{
theCalender[2][j]=date;
date++;
}
date = 1;
if (mm ==4) //if april
for (j =0; j <30; j++)
{
theCalender[3][j]=date;
date++;
}
date = 1;
if (mm ==5) //if may
for (j =0; j <31; j++)
{
theCalender[4][j] =date;
date++;
}
date = 1;
if (mm ==6) //if june
for ( j =0; j <30; j++)
{
theCalender[5][j] = date;
date++;
}
date=1;
if (mm ==7) //if july
for ( j =0; j <31; j++)
{
theCalender[6][j]=date;
date++;
}
date=1;
if (mm ==8) //if august
for ( j =0; j <31; j++)
{
theCalender[7][j]=date;
date++;
}
date=1;
if (mm ==9) //if sep
for ( j =0; j <30; j++)
{
theCalender[8][j]=date;
date++;
}
date=1;
if (mm ==10) //if oct
for (j =0; j <31; j++)
{
theCalender[9][j]=date;
date++;
}
date=1;
if (mm ==11) //if nov
for ( j =0; j <30; j++)
{
theCalender[10][j]=date;
date++;
}
date=1;
if (mm ==12) //if dec
for (j =0; j <31; j++)
{
theCalender[11][j]=date;
date++;
}
date=1;
//counts the loops and shifts the no's down
loopCount = 0;
while (loopCount < FirstDayofMonth(mm)) {
temp = theCalender[i][34]; / /put the last value of the array into a temp position
for (j=34; j>0; j--) //must count backwards
{
theCalender[i][j] = theCalender[i][j-1]; //moves the value into the position next to it on it's right
}
theCalender[i][0] = temp;
loopCount ++;
}
mm ++;
} // end while loop
} //end createCalender() loop
//************************************************** **********************
//Displays the calender to the screen
//************************************************** **********************
void Calender:isplay()
{
int i, j, mm;
for (i=0; i<12; i++)
{
cout <<endl;
for (j=0, j<35; j++)
cout theCalender[i][j];
}
}
//************************************************** ****************************************8
//Calulates which day of the week the first day of month occurs and returns an int between 0-6
//************************************************** ***************************************8
int Calender::FirstDayOfMonth(int mm)
{
int yyyy = year;
int mm = month;
int adjustedMonth;
int yearPart, centuryPart, sum, result;
if (mm<3)
adjustedMonth = mm+10;
if (mm >=3)
adjustedMonth = mm-2;
yyyy = year;
if (adjustedMonth == 11)
yyyy = year-1;
if (adjustedMonth == 12)
yyyy = year-1;
yearPart = (yyyy%100);
centuryPart = (yyyy/100);
sum = (dd + floor(2.6 * adjustedMonth - 0.2) + yearPart + floor(yearPart / 4) + floor(centuryPart / 4) - 2 * centuryPart);
result = (sum % 7);
if (result <0)
result = result + 7;
return result;
}
//************************************************** ***************************************
//Returns true if the year is a leap yr, or else returns false
//************************************************** ****************************************
bool IsLeapYear()
{
int leap;
if (yearPart = 0)
if { yyyy%400 = 0) return true;
else
leap = yyyy%4; if (leap = 0)
return true; else return false;
return;
}