Link to home
Start Free TrialLog in
Avatar of Helix
Helix

asked on

Operator Overloading

Hey everyone, im trying to do some operator overloading but cannot seem to get it working, can you guys please help me (sorry im new to operator overloading)

date.h

#ifndef DATE_H
#define DATE_H

class Date {

public:
      etc
        etc
       etc
      Date operator== (Date& somedate);

      
private:
      int day;
      int month;
      int year;
      
};

#endif

date.cpp



Date::operator== (Date& somedate);
{      
      if (somedate.day && somedate.month && somedate.year == Date.day && Date.month && Date.year);
return false;
else;
return true;
}

Avatar of Harisha M G
Harisha M G
Flag of India image

Hi Helix,
    if(somedate.day == Date.day &&  somedate.month == Date.month && somedate.year == Date.year)
Bye
---
Harish
Also.. there is a mistake in return values..
return true if they are equal

Date::operator== (Date& somedate);
{    
    if(sd.day == Date.day &&  sd.month == Date.month && sd.year == Date.year)
        return true;
    else
        return false;
}


Avatar of Helix
Helix

ASKER

Hmm still getting errors like:

C:\Documents and Settings\David\My Documents\C++\Exercise 5\1\date.cpp(65) : error C2556: 'int __thiscall Date::operator ==(class Date &)' : overloaded function differs only by return type from 'class Date __thiscall Date::operator ==(class Date &)'
        c:\documents and settings\david\my documents\c++\exercise 5\1\date.h(17) : see declaration of '=='
C:\Documents and Settings\David\My Documents\C++\Exercise 5\1\date.cpp(65) : error C2371: '==' : redefinition; different basic types
        c:\documents and settings\david\my documents\c++\exercise 5\1\date.h(17) : see declaration of '=='
C:\Documents and Settings\David\My Documents\C++\Exercise 5\1\date.cpp(66) : error C2447: missing function header (old-style formal list?)
Error executing cl.exe.

date.obj - 3 error(s), 0 warning(s)
Please send the whole code... so that it becomes easy to debug
remove the ";" from the line

Date::operator== (Date& somedate);
Avatar of Helix

ASKER

Date.h:

#ifndef DATE_H
#define DATE_H

class Date {

public:
      Date(int, int, int);
      void setDate( int d, int m, int y);
      bool isLeapYear();
      void printDate() const;
      void setDay( int d );
      void setMonth( int m );
      void setYear( int y);
      int getDay() const;
      int getMonth() const;
      int getYear() const;
      Date operator== (Date& somedate);

      
private:
      int day;
      int month;
      int year;
      
};

#endif




Date.cpp:

#include <iostream>
#include <iomanip>
using std::cout;
using std::setfill;
using std::setw;

#include "date.h"

Date::Date(int d, int m, int y)
{
      day = d;
      month = m;
      year = y;
}

void Date::setDate( int d, int m, int y){

      day = d;
      month = m;
      year = y;
      
}

bool Date::isLeapYear()
{
      return ( (year % 4) ? false : true);
}

void Date::printDate() const
{
      cout << setfill('0') << setw(2) << day << ", "<< setw(2) << month << ", " << setw(4) <<year;
      
}

void Date::setDay( int d )
{
      day = d;
}

void Date::setMonth( int m )
{
      month = m;
}

void Date::setYear( int y )
{
      year = y;
}

int Date::getDay() const
{
      return day;
}

int Date::getMonth() const
{
      return month;
}

int Date::getYear() const
{
      return year;
}

Date::operator== (Date& somedate);
{      
      if (somedate.day && somedate.month && somedate.year == temp.day && temp.month && temp.year);
return false;
else;
return true;
}

      

Yes.. remove ;

Date::operator== (Date& somedate)
{    
    if(sd.day == Date.day &&  sd.month == Date.month && sd.year == Date.year)
        return true;
    else
        return false;
}
Sorry..

Date::operator== (Date& sd)
{    
    if(sd.day == Date.day &&  sd.month == Date.month && sd.year == Date.year)
        return true;
    else
        return false;
}
Avatar of Helix

ASKER

Tried but no go :(
Avatar of Helix

ASKER

C:\Documents and Settings\David\My Documents\C++\Exercise 5\1\date.cpp(65) : error C2143: syntax error : missing ';' before '=='
C:\Documents and Settings\David\My Documents\C++\Exercise 5\1\date.cpp(65) : error C2350: 'Date::Date::Date' is not a static member
C:\Documents and Settings\David\My Documents\C++\Exercise 5\1\date.cpp(65) : fatal error C1004: unexpected end of file found
You have to compare each variable independently...
Also, you should return true if they are all equal
Your prototype is wrong...

operator == (Date& somedate);

is enough.. it is returning boolean ( or int)
Avatar of Helix

ASKER

its returning boolean, ive changed it but its throwing up more errors. Sorry, brian is just not working today..
IHO that should be

#ifndef Date_H
#define Date_H

class Date {

public:
     Date(int, int, int);
    void setDate( int d, int m, int y);
    bool isLeapYear();
    void printDate() const;
     void setDay( int d );
    void setMonth( int m );
    void setYear( int y);
    int getDay() const;
    int getMonth() const;
    int getYear() const;
    bool operator== (const Date& someDate);

   
private:
    int day;
    int month;
    int year;
   
};

#endif




Date.cpp:

#include <iostream>
#include <iomanip>
using std::cout;
using std::setfill;
using std::setw;

#include "Date.h"

Date::Date(int d, int m, int y)
{
    day = d;
    month = m;
    year = y;
}

void Date::setDate( int d, int m, int y){

    day = d;
    month = m;
    year = y;
   
}

bool Date::isLeapYear()
{
    return ( (year % 4) ? false : true);
}

void Date::printDate() const
{
    cout << setfill('0') << setw(2) << day << ", "<< setw(2) << month << ", " << setw(4) <<year;
   
}

void Date::setDay( int d )
{
    day = d;
}

void Date::setMonth( int m )
{
    month = m;
}

void Date::setYear( int y )
{
    year = y;
}

int Date::getDay() const
{
    return day;
}

int Date::getMonth() const
{
    return month;
}

int Date::getYear() const
{
    return year;
}

bool Date::operator== (const Date& someDate);
{    
     if (someDate.day && someDate.month && someDate.year == temp.day && temp.month && temp.year);
return false;
else;
return true;
}

which compiles fine.
What is temp? Where is it defined?
bool Date::operator== (const Date& someDate);

How does this compile jkr? Which compiler?
Avatar of Helix

ASKER

indeed, that doesn't compile in ms visual studio 6...
>>How does this compile jkr? Which compiler?

Sorry, there indeed was something wrong - he operator should be

bool Date::operator== (const Date& someDate)
{    
     if (someDate.day && someDate.month && someDate.year == day && month && year)
return false;
else;
return true;
}
ASKER CERTIFIED SOLUTION
Avatar of Harisha M G
Harisha M G
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
jkr>  if (someDate.day && someDate.month && someDate.year == day && month && year)
return false;

Is this logically correct ?

What happened to the Genius today? :)
Avatar of Helix

ASKER

Champion, cheers, got it working

David
Thanks :)
Here, i added a main and tested it...  this compiles and runs for MS Visual Studio 2003


Main.cpp

#include <iostream>
#include <iomanip>

using namespace std;

#include "Date.h"

void main(void)
{
      DateObject dateone(10, 10, 2004);
      DateObject datetwo(10,10,2004);
      
      if(dateone == datetwo)
            cout << "Yeah" << endl;
      else
            cout << "Na" << endl;

      datetwo.setDate(10,20,2003);

      if(dateone == datetwo)
            cout << "Oops" << endl;
      else
            cout << "Yeah" << endl;

      system("pause");
}

DateObject::DateObject(int d, int m, int y)
{
    day = d;
    month = m;
    year = y;
}

void DateObject::setDate( int d, int m, int y){

    day = d;
    month = m;
    year = y;
   
}

bool DateObject::isLeapYear()
{
    return ( (year % 4) ? false : true);
}

void DateObject::printDate() const
{
    cout << setfill('0') << setw(2) << day << ", "<< setw(2) << month << ", " << setw(4) <<year;
   
}

void DateObject::setDay( int d )
{
    day = d;
}

void DateObject::setMonth( int m )
{
    month = m;
}

void DateObject::setYear( int y )
{
    year = y;
}

int DateObject::getDay() const
{
    return day;
}

int DateObject::getMonth() const
{
    return month;
}

int DateObject::getYear() const
{
    return year;
}

bool DateObject::operator== (const DateObject& someDate)
{    
      //cout << this->day << " " << this->month << " " << this->year << endl;
      //cout << someDate.getDay() << " " << someDate.getMonth() << " " << someDate.getYear() << endl;

    if (someDate.getDay() == this->day && someDate.getMonth() == this->month && someDate.year == this->year)
            return true;
      else
            return false;
}

Date.h

#ifndef Date_H
#define Date_H

class DateObject {

public:
    DateObject(int, int, int);
    void setDate( int d, int m, int y);
    bool isLeapYear();
    void printDate() const;
    void setDay( int d );
    void setMonth( int m );
    void setYear( int y);
    int getDay() const;
    int getMonth() const;
    int getYear() const;
    bool operator == (const DateObject& someDate);

   
private:
    int day;
    int month;
    int year;
   
};

#endif


You can rename the DateObject back to date if you like... just remember, if in your main you have a date pointer (say Date * dateone) then instance memory into that pointer, dereference before you compare (thats the mistake I was making when I started hehe)
Bah hehe, sorry it was still outstanding when I posted :)