1 Replies - 232 Views - Last Post: 04 December 2012 - 04:29 PM Rate Topic: -----

#1 Jbf1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 24-October 12

Overloading Operators is giving me trouble

Posted 04 December 2012 - 04:20 PM

My proffessors instructions are often quite confusing. So I think I have this written correctly, but the output only gives me this:

Month = 1, Day = 1
Month = 1, Day = 1
The dates are the same!

Could someone point me in the direction of the problem. Please don't just give me the fixed code. I want to understand the problem and the solution.
#include <iostream>
using namespace std;

class DayOfYear
{
public:
	DayOfYear(int theMonth, int theDay);
	DayOfYear();
    void setMonth(int newMonth);
    void setDay(int newDay);
    int getMonth();
    int getDay();
	friend istream& operator >>(istream& ins, DayOfYear date);
    friend ostream& operator <<(ostream& outs, const DayOfYear date);
	friend bool operator ==(DayOfYear date1, DayOfYear date2);
private:
    int month;
    int day;
};

int main()
{
	DayOfYear date_1, date_2;

	cout << "Enter the Day and Month separated by a space." << endl;
	cin >> date_1;

	cout << "Enter the Day and Month separated by a space." << endl;
	cin >> date_2;

	cout << "Date 1:" << endl;
	cout << date_1;

	cout << "Date 2:" << endl;
	cout << date_2;

	if(date_1 == date_2){
		cout << "The dates are the same!" << endl;
	}else{
		cout << "The dates are different!" << endl;
	}
	return 0;
}

bool operator ==(DayOfYear date1, DayOfYear date2)
{
	return ((date1.month == date2.month) &&
		(date1.day == date2.day));
}

DayOfYear::DayOfYear(int theMonth, int theDay)
{
	if ((theMonth >= 1) && (theMonth <= 12) && (theDay >= 1) && (theDay <= 31))
	{
		month = theMonth;
		day = theDay;
	}
	else
	{
		cout << "Error!  Invalid initial value for month and / or day." << endl;
		cout << "Setting both values to 1." << endl;
		day = 1;
		month = 1;
	}
}

DayOfYear::DayOfYear()
{
	month = 1;
	day = 1;
}

void DayOfYear::setMonth(int newMonth)
{
    if ((newMonth >= 1) && (newMonth <= 12))
    	month = newMonth;
    else
    	cout << "Error!  The month must be between 1 and 12." << endl;
}

void DayOfYear::setDay(int newDay)
{
    if ((newDay >= 1) && (newDay <= 31))
    	day = newDay;
    else
    	cout << "Error!  The month must be between 1 and 31." << endl;
}

int DayOfYear::getMonth()
{
    return month;
}

int DayOfYear::getDay()
{
    return day;
}

istream& operator >>(istream& ins, DayOfYear date)
{
	int tempMonth, tempDay;
	
    ins >> tempMonth;
	date.setMonth(tempMonth);

    ins >> tempDay;
	date.setDay(tempDay);

	return ins;
}

ostream& operator <<(ostream& outs, const DayOfYear date)
{
    outs << "month = " << date.month << ", day = " << date.day << endl;
	return outs;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Overloading Operators is giving me trouble

#2 Jbf1001  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 24-October 12

Re: Overloading Operators is giving me trouble

Posted 04 December 2012 - 04:29 PM

O.K. so I jumped the gun early on this one. HAHA. I had to change some of my 'DayOfYear' to 'DayOfYear&'. Pass by reference really makes a difference hahaha
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1