?
Solved

Operator Overloading

Posted on 2005-04-16
24
Medium Priority
?
347 Views
Last Modified: 2013-12-14
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;
}

0
Comment
Question by:Helix
  • 12
  • 7
  • 2
  • +2
24 Comments
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13798956
Hi Helix,
    if(somedate.day == Date.day &&  somedate.month == Date.month && somedate.year == Date.year)
Bye
---
Harish
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13798962
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;
}


0
 

Author Comment

by:Helix
ID: 13798973
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)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:Harisha M G
ID: 13798999
Please send the whole code... so that it becomes easy to debug
0
 
LVL 1

Expert Comment

by:jonmclean2
ID: 13799002
remove the ";" from the line

Date::operator== (Date& somedate);
0
 

Author Comment

by:Helix
ID: 13799009
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;
}

      

0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13799020
Yes.. remove ;

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

Expert Comment

by:Harisha M G
ID: 13799023
Sorry..

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

Author Comment

by:Helix
ID: 13799027
Tried but no go :(
0
 

Author Comment

by:Helix
ID: 13799028
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
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13799029
You have to compare each variable independently...
Also, you should return true if they are all equal
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13799034
Your prototype is wrong...

operator == (Date& somedate);

is enough.. it is returning boolean ( or int)
0
 

Author Comment

by:Helix
ID: 13799040
its returning boolean, ive changed it but its throwing up more errors. Sorry, brian is just not working today..
0
 
LVL 86

Expert Comment

by:jkr
ID: 13799047
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.
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13799058
What is temp? Where is it defined?
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13799065
bool Date::operator== (const Date& someDate);

How does this compile jkr? Which compiler?
0
 

Author Comment

by:Helix
ID: 13799082
indeed, that doesn't compile in ms visual studio 6...
0
 
LVL 86

Expert Comment

by:jkr
ID: 13799098
>>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;
}
0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 1000 total points
ID: 13799108
Try this....

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

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

Expert Comment

by:Harisha M G
ID: 13799114
jkr>  if (someDate.day && someDate.month && someDate.year == day && month && year)
return false;

Is this logically correct ?

What happened to the Genius today? :)
0
 

Author Comment

by:Helix
ID: 13799127
Champion, cheers, got it working

David
0
 
LVL 37

Expert Comment

by:Harisha M G
ID: 13799133
Thanks :)
0
 

Expert Comment

by:battlewr001
ID: 13799152
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)
0
 

Expert Comment

by:battlewr001
ID: 13799155
Bah hehe, sorry it was still outstanding when I posted :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question