Hey everyone, I'm trying to implement a operator overload of several operators. I want to make sure my syntax is correct.

Here's my old Class implementation:

#include <iostream>

using namespace std;

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 = dy = yr = 0;}
    Date(int m, int d, int y): mn(m), dy(d), yr(y)
    {}

    //access functions
    int get_month() const
    {return mn;}
    int get_day() const
    {return dy;}
    int get_year() const
    {return yr;}

    bool equalto(Date d) const;
    bool greaterthan(Date d) const;
    bool lessthan(Date d) const;

    //modifier functions
    void set_month(int m)
    {mn = m;}
    void set_day(int d)
    {dy = d;}
    void set_year(int y)
    {yr = y;}
    void set_Date(int m, int d, int y)
    {mn = m; dy = d; yr = y;}

    //input/output functions
    void read();
    void write() const;
};

//Date class member functions
bool Date::equalto(Date d) const
{
    return (mn == d.mn) && (dy == d.dy) && (yr == d.yr);
}

bool Date::lessthan(Date d) const
{
    if ((yr < d.yr) ||
        ((yr == d.yr) && (mn < d.mn)) ||
        ((yr == d.yr) && (mn == d.mn) && (dy < d.dy)))
        return true;

    return false;
}

bool Date::greaterthan(Date d) const
{
    if ((yr > d.yr) ||
        ((yr == d.yr) && (mn > d.mn)) ||
        ((yr == d.yr) && (mn == d.mn) && (dy > d.dy)))
        return true;

    return false;
}

void Date::read()
{
    char skip_char;

    cin >> mn >> skip_char >> dy >> skip_char >> yr;
}

void Date::write() const
{
    if (mn < 10)
        cout << '0';
    cout << mn << '/';
    if (dy < 10)
        cout << '0';
    cout << dy << '/'
         << yr;
}

Now I tried to overload the greaterthan(), lessthan() and equalto()

Then I tried to overload the read() function with extraction >> thats where I'm stuck.

I can't compile any of it.

#include <iostream>

using namespace std;

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 = dy = yr = 0;}
    Date(int m, int d, int y): mn(m), dy(d), yr(y)
    {}

    //access functions
    int get_month() const
    {return mn;}
    int get_day() const
    {return dy;}
    int get_year() const
    {return yr;}

   // bool equalto(Date d) const;
	bool operator== (Date d) const;

    //bool greaterthan(Date d) const;
	bool operator> (Date d) const;

    //bool lessthan(Date d) const;
	bool operator< (Date d) const;

    //modifier functions
    void set_month(int m)
    {mn = m;}
    void set_day(int d)
    {dy = d;}
    void set_year(int y)
    {yr = y;}
    void set_Date(int m, int d, int y)
    {mn = m; dy = d; yr = y;}

    //input/output functions
    
	//void read();
	friend istream &operator>>(istream &str, Date&d)
	{
		char skip_char;

		str >> mn >> skip_char >> dy >> skip_char >> yr;
	}

    void write() const;
};

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

bool Date::operator<(Date d) const
{
    if ((yr < d.yr) ||
        ((yr == d.yr) && (mn < d.mn)) ||
        ((yr == d.yr) && (mn == d.mn) && (dy < d.dy)))
        return true;

    return false;
}

bool Date::operator>(Date d) const
{
    if ((yr > d.yr) ||
        ((yr == d.yr) && (mn > d.mn)) ||
        ((yr == d.yr) && (mn == d.mn) && (dy > d.dy)))
        return true;

    return false;
}

/*
void Date::read()
{
    char skip_char;

    cin >> mn >> skip_char >> dy >> skip_char >> yr;
}
*/
void Date::write() const
{
    if (mn < 10)
        cout << '0';
    cout << mn << '/';
    if (dy < 10)
        cout << '0';
    cout << dy << '/'
         << yr;
}

Thanks

Recommended Answers

All 4 Replies

Is there a particular problem? For you operator >, you can just do :
return !(a < b );

//input/output functions
    
	//void read();
	friend istream &operator>>(istream &str, Date&d)
	{
		char skip_char;

		str >> mn >> skip_char >> dy >> skip_char >> yr;
	}

The compiler might be asking you to access 'mn' , 'dy' , 'yr' using the Date reference.
something like

str >> d.mn ...

Is there a particular problem? For you operator >, you can just do :
return !(a < b );

I'm not sure because I don't have a main() to test with. Thats why I'm wondering if the syntax is correct.

I'm not sure because I don't have a main() to test with.

You could just make a few objects that are equal and not equal to test out your operators
e.g.

Date today(12,14,2009);
Date tomorrow(12,15,2009);
Date copyofdate(12,14,2009);
cout <<"today < tomorrow "<< (today < tomorrow)<<endl;
cout << "today == tomorrow "<<(today == tomorrow)<<endl;
cout <<"copyofdate == today "<<(copyofdate == today)<<endl;

(I didn't compile the above so there may be an error or two)

EDIT: Make sure you either put your main at the end of your existing file -- if you put it in its own file, you need to split off your class declaration into a .h file and #include "yourheader.h"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.