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