0

I am new to overloading operators. I am trying to overload a bool operator. I am currently using the bool operator as an access function into the Date class. Any suggestions how I would go about converting the bool EqualTo function to overload the operator? Thank you!

class Date {
private:
    int mn;        //month component of a date
    int dy;        //day component of a date
    int yr;        //year comonent of a date

public:
    //constructors
    Date() : mn(0), dy(0), yr(0)
    {}
    Date(int m, int d, int y) : mn(m), dy(d), yr(y)
    {}

    //access functions

    int getDay() const
    {
        return dy;
    }
    int getMonth() const
    {
        return mn;
    }
    int getYear() const
    {
        return yr;
    }

    bool EqualTo(Date d) const;

};

bool Date::EqualTo(Date d) const
{
    return (mn == d.mn) && (dy == d.dy) && (yr == d.yr);
}
7
  • Change the declaration to operator bool() const. But you don't want that. You want to create friend operator==(const Date& lhs, const Date& rhs). Commented May 6, 2020 at 17:18
  • 2
    Looks more like you are trying to overload operator== to compare two objects? Commented May 6, 2020 at 17:19
  • 1
    Why are you trying to overload operator bool()? You seem to be comparing 2 Dates, so you should probably overload operator==. Commented May 6, 2020 at 17:19
  • I should have mentioned that it will be an array of Dates. Commented May 6, 2020 at 17:21
  • I'm not sure what that has to do with checking if 2 dates are equal? Commented May 6, 2020 at 17:22

1 Answer 1

5

The implementation of your EqualTo function suggests that you should be overloading operator== to test if 2 Date objects are equal. All you have to do is rename EqualTo to operator==. And you should take the arguments by const&.

bool Date::operator==(Date const &d) const
{
    return (mn == d.mn) && (dy == d.dy) && (yr == d.yr);
}

Inside the class Date, the declaration would look like:

bool operator==(Date const &d) const;

Another way to do this is to make the operator a friend of the class:

friend bool operator==(Date const &a, Date const &b) const
{
    return (a.mn == b.mn) && (a.dy == b.dy) && (a.yr == b.yr);
}

Note that in this case, this is not a member function. In this case, you can define it inside the class (where you need the friend keyword).

If you define a friend function outside the class, you still need to declare it as a friend within the class. However, the definition can no longer have the friend keyword.

I would also recommend naming your variables a bit more clearly, such as month, day, and year.

Sign up to request clarification or add additional context in comments.

6 Comments

Great! How would I declare in Date?
Added more explanation.
I implemented the first example into my code. Now I am getting an error - "Out-of-line definition of 'operator==' does not match any declaration in 'Date'"
@cigen the declaration is usually marked as friend ;) Its worth mentioning that these should be public member functions, and that const can be placed on either side ;)
@Waqar Yes, it's in the answer. Is it not clear enough? Edited a bit.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.