#include <iostream>

using namespace std;


class NumDays
{
private:
	double hours;
	double days;


public:
	NumDays(double h=0)
	{
	    hours=h;

	}

how can I assign values to both hours and days, but only takes in the number of hours as its argument? I don't know how to assign the days without changing NumDays(double h=0)

void setHours(double h)
	{
              //code
	}

how can I set function that assign values to both hours and days, but only takes in the number of hours as its argument?

double getHours() const
	{
		return hours;
	}

	void setDays(double d)
	{
                 //code
	}

how can I set function that assign values to both hours and days, but only takes in the number of hours as its argument?

double getDays() const
	{
		return days;
	}

	NumDays operator +(NumDays &);
	NumDays operator ++();
	NumDays operator (int);



};


int main()
{
	NumDays dayOne(17), dayTwo(27),dayThree, dayFour, dayFive;

	cout<< "Day One: "<< dayOne.getDays() << endl;
	cout<< "Day Two: "<< dayTwo.getDays() << endl;

	dayThree =dayOne +dayTwo;

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

	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;

	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;
	cout<< endl;

	system("pause");
	return 0;

}

Edited 6 Years Ago by knellgust: n/a

You technically don't need a days variable at all. In your getDays method, just return current hours divided by the number of hours in a day. If not, use your brain and do simple math:

setHours(double h) {
    hours = h;
    days = h / 24.0f;
}

Given the above, you should be able to write the setDays method on your own. There are ~24 hours in a day if you didn't know. Brave new world.

Edited 6 Years Ago by xikkub: n/a

The class's purpose is to store a value that represents a number of work hours and convert it to a number of days. For example, 8 hours would be converted to 1days, 12 hours would be converted to 1.5days, and 18 hours would be converted to 2.25 days.

in the public, NumDays operator (int); should be NumDays operator ++(int);.

Did my answer provide a solution for you? Simply change 24 hours to 8 hours if you're changing your hours-per-day. Get rid of your days variable. It's not needed. Create a method getDays that returns: hours / 8.0f.

void setHours(double h)
	{
		hours=h;
		days=h/8.0f;
void setDays(double d)
	{
		
		days=d;
		hours=d*8.0f;
				

	}

The outcome should be done like picture.
I did this way. How to assign values in the NumDays construcuctor?

Edited 6 Years Ago by knellgust: n/a

Attachments ScreenHunter_01_Dec._06_22_.11_.jpg 22.81 KB

The NumDays constructor is no different than any other method. What I told you is correct. I can't help you if you don't explain the problem.

1. Your problem isn't with your constructor, it's with your overloaded operators. I can't even get it to compile.
2. Overloading Operators

NumDays(double h=0)    // I must use this head
	{
		hours=h;       //you can change       
		days=h/8.0;    //you can change 
	}
	void setHours(double h)     // Must use it
	{
		hours=h;               //you can change 
		days=h/8.0;            //you can change 
	}
        void setDays(double d)    /Must use it
	{
		days=d;            //you can change 
		hours=d*8.0;       //you can change 
	}

Then how can I avoid overloaded operators ?

I'm not telling you to get rid of the overloaded operators. Simply define them such that they perform the correct computations. Overloaded operators are explained in moderate detail on the URL I provided. Good luck.

This article has been dead for over six months. Start a new discussion instead.