Thread: Assignment operator

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    10

    Assignment operator

    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;
    }

  2. #2
    Unregistered
    Guest
    You dont actually need it here because you don't hold any pointers inside your class, a binary copy will do just fine, but you could just copy the data from the object being passed by reference to your operator. And it is defined like this:

    Code:
    class Calender {
    ...
        // Inside the class
        Calender& operator = (const Calender& rhs);
    ...
    private:
        int year;
        int theCalender[12][35];
    };
    ...
    //Outside the class you need the "ClassName::" like any member function
    Calender& Calender :: operator = (const Calender& rhs) 
    {
        // Here all you have to do is copy the fields from rhs
    
        return *this; // Returns a reference to this object
    }
    Hope this helps.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    10

    What would that be?

    How would that assignment operator go?

    Would I have to do, say, make anything equal to .rhs??
    I'm unsure of what the total code would be. How would I copy it?


    Thanks@!

  4. #4
    Unregistered
    Guest
    Code:
    Calender& Calender :: operator = (const Calender& rhs) 
    {
        // You just copy field by field
        year = rhs.year;
    
        // For the array use memcpy
        memcpy( theCalender, rhs.theCalender, sizeof(theCalender) );
    
        return *this; // Returns a reference to this object
    }
    And now you can use it like this:

    Code:
    #include ....
    ...
    int main() {
        Calender c1, c2;
        // Do some stuff
        ...
        // Use it like this
        c2 = c1; // This just copies c1.year and c1.theCalender into c2.year and c2.theCalender
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. friendly assignment operator
    By Aidman in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2007, 01:09 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM