6 Replies - 326 Views - Last Post: 07 December 2011 - 12:37 PM Rate Topic: -----

#1 Hejam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-April 11

Overloading the ++ Operator Trouble

Posted 07 December 2011 - 01:58 AM

#include <iostream>
using namespace std;

class numberDays
{
private:
double hours;
double days;

public:
//constructor that assigns values to both hours and days, but only
//takes in the number of hours as its argument
numberDays(double h = 0)
{ hours = h; 
  days = h/8.0; }

// set function that assigns values to both hours and days, but only
//takes in the number of hours as its argument
void setHours(double h)
{ hours = h; 
  days = h/8.0; }

double getHours() const
{ return hours; }

// set function that assigns values to both hours and days, but only
//takes in the number of days as its argument
void setDays(double d)
{ days = d; 
  hours = d * 8.0; }

double getDays() const
{ return days; }

numberDays operator + (numberDays &);
numberDays operator ++ (); // Prefix ++
numberDays operator ++ (int); // Postfix ++
};

numberDays numberDays::operator + (numberDays &right)
{
	numberDays temp;

	temp.hours = hours + right.hours;
	temp.days = days + right.days;
	return temp;
}

numberDays numberDays::operator ++ ()
{
	++hours;
	setDays();	         //I am trying to fix this operator ++
	return *this;
}
	
numberDays numberDays::operator ++ (int)
{
	numberDays temp(hours);
	
	hours++;                //I need to fix this one as well
	setDays();
	return temp;
}

int main()
{
numberDays dayOne(17), dayTwo(27), dayThree, dayFour, dayFive;
cout << "Day One: " << dayOne.getDays() << endl;
cout << "Day Two: " << dayTwo.getDays() << endl;

// Overloaded Add
dayThree = dayOne + dayTwo;

// Display Day three.
cout << "Day Three (in days): " << dayThree.getDays() << endl;
cout << "Day Three (in hours): " << dayThree.getHours() << endl;
cout << endl;

// Overloaded Postfix increment
dayFour = dayThree++;
cout << "Day Three (in days): " << dayThree.getDays() << endl;
cout << "Day Three (in hours): " << dayThree.getHours() << endl;
cout << "Day Four (in days): " << dayFour.getDays() << endl;
cout << "Day Four (in hours): " << dayFour.getHours() << endl;
cout << endl;

// Overloaded Prefix increment
dayFive = ++dayThree;
cout << "Day Three (in days): " << dayThree.getDays() << endl;
cout << "Day Three (in hours): " << dayThree.getHours() << endl;
cout << "Day Five (in days): " << dayFive.getDays() << endl;
cout << "Day Five (in hours): " << dayFive.getHours() << endl;

system("pause");
return 0;
}



I am having trouble writing the code for the two functions that overload the ++ operator which annotated with a comment. The rest of the code should all be correct and I wish not to mess with it. Please help me out guys. Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Overloading the ++ Operator Trouble

#2 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Overloading the ++ Operator Trouble

Posted 07 December 2011 - 03:06 AM

Look at your setDays() function:
void setDays(double d){..}

It expect one parameter of double value,but you are not passing anything:
numberDays numberDays::operator ++ ()
{
	++hours;
	setDays();<------	         //I am trying to fix this operator ++
	return *this;
}


Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,594
  • Joined: 16-October 07

Re: Overloading the ++ Operator Trouble

Posted 07 December 2011 - 06:48 AM

Don't store days, you're confusing yourself. Perhaps something like:
class numberDays {
private:
	double hours;
public:
	numberDays(double h = 0) : hours(h) {}
	void setHours(double h) { hours = h; }
	double getHours() const { return hours; }
	void setDays(double d){ hours = d * 8.0; }
	double getDays() const { return hours / 8.0; }

	numberDays operator + (numberDays &other) const { return numberDays(other.hours + hours); }
	numberDays operator ++ () { hours += 1.0; return *this; }
	numberDays operator ++ (int) { numberDays t = *this; hours += 1.0; return t; }
};



Judging from your test harness, it seems that the ++ is for a day, not an hour, in which case just use 8.0. Conversely, since you're using double, you could store days and calc hours.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 Hejam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-April 11

Re: Overloading the ++ Operator Trouble

Posted 07 December 2011 - 09:41 AM

I'm actually not allowed to change a lot of that stuff you just coded there, sorry. And when I put days in there, then it still continues to give me the same error which states "this declaration has no storage class or type specifier" on a lot of things like dayThree, dayOne, cout...
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,594
  • Joined: 16-October 07

Re: Overloading the ++ Operator Trouble

Posted 07 December 2011 - 09:50 AM

You're calling setDays with no parameter. What is supposed to happen? And why are you ++hours?

I suspect you want something like:
numberDays numberDays::operator ++ () {
	setHours(++hours);
	return *this;
}
// or
numberDays numberDays::operator ++ () {
	setDays(++days);
	return *this;
}



That code compiled. If it doesn't compile for you, post back what you have.
Was This Post Helpful? 0
  • +
  • -

#6 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Overloading the ++ Operator Trouble

Posted 07 December 2011 - 09:53 AM

View PostHejam, on 07 December 2011 - 09:41 AM, said:

I'm actually not allowed to change a lot of that stuff you just coded there, sorry.


A shame. What baavgai wrote is better. hours and days essentially keep the same information, this is usually considered not a good thing as you can occidentally put them out of sync.

View PostHejam, on 07 December 2011 - 09:41 AM, said:

And when I put days in there, then it still continues to give me the same error which states "this declaration has no storage class or type specifier" on a lot of things like dayThree, dayOne, cout...


First of all

numberDays numberDays::operator ++ ()
{
	++hours;
	setDays();	         //I am trying to fix this operator ++
	return *this;
}



From your test code I see that ++ hours advance numberDays by one day, so why do you increment hours by one ? And yes setDays should take a parameter.

Basically what you want is increment days by one and hours by 8. There are 3 ways to do it within your code.

Straight

days += 1
hours += 8



or

setDays(days+1)



or

setHours(hours+8)



Verify that all 3 statements do exactly the same thing.

This post has been edited by Karel-Lodewijk: 07 December 2011 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 Hejam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-April 11

Re: Overloading the ++ Operator Trouble

Posted 07 December 2011 - 12:37 PM

Well my code is suppose to recognize that 8 hours is equal to 1 day and be able to translate that with the number of hours meaning: 12 hours is equal to 1.5 days, 18 hours is equal to 2.25 days and so forth.
@baavgai
What did you put for the second ++ overload with (int) because I'm still getting the same errors and am unable to compile. Here is my current code:
#include <iostream>
using namespace std;

class numberDays
{
private:
double hours;
double days;

public:
//constructor that assigns values to both hours and days, but only
//takes in the number of hours as its argument
numberDays(double h = 0)
{ hours = h; 
  days = h/8.0; }

// set function that assigns values to both hours and days, but only
//takes in the number of hours as its argument
void setHours(double h)
{ hours = h; 
  days = h/8.0; }

double getHours() const
{ return hours; }

// set function that assigns values to both hours and days, but only
//takes in the number of days as its argument
void setDays(double d)
{ days = d; 
  hours = d * 8.0; }

double getDays() const
{ return days; }

numberDays operator + (numberDays &);
numberDays operator ++ (); // Prefix ++
numberDays operator ++ (int); // Postfix ++
};

numberDays numberDays::operator + (numberDays &right)
{
	numberDays temp;

	temp.hours = hours + right.hours;
	temp.days = days + right.days;
	return temp;
}

numberDays numberDays::operator ++ ()
{
	setHours(++hours);
	return *this;
}
	
numberDays numberDays::operator ++ (int)
{
	numberDays temp(hours);

	setHours(hours++);
	return temp;
}

int main()
{
numberDays dayOne(17), dayTwo(27), dayThree, dayFour, dayFive;
cout << "Day One: " << dayOne.getDays() << endl;
cout << "Day Two: " << dayTwo.getDays() << endl;

// Overloaded Add
dayThree = dayOne + dayTwo;

// Display Day three.
cout << "Day Three (in days): " << dayThree.getDays() << endl;
cout << "Day Three (in hours): " << dayThree.getHours() << endl;
cout << endl;

// Overloaded Postfix increment
dayFour = dayThree++;
cout << "Day Three (in days): " << dayThree.getDays() << endl;
cout << "Day Three (in hours): " << dayThree.getHours() << endl;
cout << "Day Four (in days): " << dayFour.getDays() << endl;
cout << "Day Four (in hours): " << dayFour.getHours() << endl;
cout << endl;

// Overloaded Prefix increment
dayFive = ++dayThree;
cout << "Day Three (in days): " << dayThree.getDays() << endl;
cout << "Day Three (in hours): " << dayThree.getHours() << endl;
cout << "Day Five (in days): " << dayFive.getDays() << endl;
cout << "Day Five (in hours): " << dayFive.getHours() << endl;

system("pause");
return 0;
}



@Karel
From the looks of what your saying seems to be just logic errors. Right now I am more concerned about it compiling because I usually can figure out the logic from what prints out. Thanks though.
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,594
  • Joined: 16-October 07

Re: Overloading the ++ Operator Trouble

Posted 07 December 2011 - 01:02 PM

Your other one should also be setHours(++hours); That temp variable is what's holding prior state. You don't really want to modify hours when it comes back; you'll throw the whole thing off.

I copied your code verbatim, compiled with every warning turned on I could think of, and it ran without any problems.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1