Operator Overloading

A

acheron05

Hi,

I've been writing a program for another school assignment but I am
having trouble working out how to overload the < and << operators. The
program is designed to read data from a file, create Date objects,
place them in a vector, sort them and then output the results. The <
operator needs to be overloaded to allow the container filled with the
Date objects to be sorted and the insertion operator << needs to be
overloaded to output the sorted elements to cout. What am I doing
wrong?

Here is what I have done so far. I've got the vector and fstream
working properly I believe.

Thanks for any help.... I'm really stuck.....


#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

// Use this enumeration to symbolically represent the months of the
// year. Note that JAN is initialized to 1 because by default,
// enumerations start at 0.
enum months { JAN=1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV,
DEC };

// Date class from Assignment 1. This needs to be modified for this
assignment
class Date
{

public:

// This is the only constructor we allow
Date(int y, int m, int d) : year(y), month(m), day(d) { cout<<y<<"
"<<m<<" "<<d<<endl; }

// Destructor does nothing, but I've put it here anyway
~Date() {}

// Check that the year, month, and day corresponds to a valid date
bool isValid(void);

// Check for a leap year
bool isLeapYear(void);

friend bool operator < (const Date& d1, const Date& d2);

friend ostream& operator << (ostream& os, const Date& d);

private:
int year, month, day;
};

// Use this to display who you are
void displayInfo(void);

int main(int argc, char* argv[])
{

// Display who you are
displayInfo();

// Your code here

char endLine[10];

int y;
int m;
int d;

string filename = "/a4-input.txt";

vector<Date> DateItems;

ifstream fin(filename.c_str());

while(!fin.eof())
{
fin >> y;
fin >> m;
fin >> d;
fin.getline(endLine, 10);

Date DateRecord(y, m, d);

DateItems.push_back(DateRecord);
}

fin.close();

sort(DateItems.begin(), DateItems.end());

for(unsigned int i = 0; i < DateItems.size(); i++)
{
cout<<DateItems;
}

return 0;
}

//------------------------------------------------------------
// supply your information in the function below .
void displayInfo()
{
cout << "----------------------------------------" << endl;
cout << "Assignment 4 Semester 1 2006" << endl;
cout << " Submitted by: Duck, Donald, 000000000" << endl;
cout << "----------------------------------------" << endl;
}

bool operator < (const Date& d1, const Date& d2)
{
if(d1 < d2)
{
return true;
}

else
{
return false;
}
}

ostream& operator << (ostream& os, const Date& d)
{

return os;
}

// Is the date a leap year? You do not need to modify this function.
bool Date::isLeapYear(void)
{
return (year%400==0) || (year%4==0 && year%100!=0);
}

// Is the date valid? You do not need to modify this.
bool Date::isValid(void)
{
// Test in the special case of February
if (month==FEB)
{

// Is this a leap year?
if ( isLeapYear() )
return (day>=1 && day<=29); // Feb has 29 days

else
return (day>=1 && day<=28); // Otherwise, 28 days

}

// Test if the case is a month with 31 days
else if (month==JAN || month==MAR || month==MAY || month==JUL ||
month==AUG || month==OCT || month==DEC)
{
return (day>=1 and day<=31);
}

// Test in the case of a 30 day month
else if (month==APR || month==JUN || month==SEP || month==NOV)
return (day>=1 and day<=30);


// Otherwise this is an invalid month
else
return false;

}
 
K

Kai-Uwe Bux

acheron05 said:
Hi,

I've been writing a program for another school assignment but I am
having trouble working out how to overload the < and << operators. The
program is designed to read data from a file, create Date objects,
place them in a vector, sort them and then output the results. The <
operator needs to be overloaded to allow the container filled with the
Date objects to be sorted and the insertion operator << needs to be
overloaded to output the sorted elements to cout. What am I doing
wrong?
[snip]
class Date
{

public: [snio]
friend bool operator < (const Date& d1, const Date& d2); [snip]
private:
int year, month, day;
}; [snip]

bool operator < (const Date& d1, const Date& d2)
{
if(d1 < d2)

Here, this operator recursively calls itself. That is not what you want. You
want to use year, month, and day to form an opinion about which date comes
first.
{
return true;
}

else
{
return false;
}
}


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top