Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operator Overloading

Reply
Thread Tools

Operator Overloading

 
 
acheron05
Guest
Posts: n/a
 
      06-02-2006
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[i];
}

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<=2; // 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;

}

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-02-2006
acheron05 wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 2 02-21-2009 04:26 AM
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments