1

I have no idea why this code should work, but tell me what to do if i want to add two objects together. please. while you are trying to answer please be more noob specific

sorry for my bad english, I am an Indian, here is my code.

#include<iostream>

using namespace std;

class time
{
private:
    int sec;
    int mint;
    int hours;
public:
    int Inputsec;
    int Inputmint;
    int Inputhours;
time(int Inputsec, int Inputmint, int Inputhours):sec(Inputsec), mint(Inputmint), hours(Inputhours){};
time operator+(time Inputobj)
{
    time blah (sec+Inputsec,mint+Inputmint,hours+Inputhours);
    return blah;
}

void DisplayCurrentTime()
{
    cout << "The Current Time Is"<<endl<< hours<<" hours"<<endl<<mint<<"minutes"<<endl<<sec<<"seconds"<<endl;
}
};

int main()
{
time now(11,13,3);
time after(13,31,11);
time then(now+after);
then.DisplayCurrentTime();
}

code is working fine but it is giving me horrible output. Where is my mistake?

4
  • Being Indian is no excuse for an incomplete topic - copy/paste that 'horrible output' and we might actually understand some of it. Commented Jun 24, 2013 at 11:59
  • " horrible output"? How about telling us the actual and expected output? Commented Jun 24, 2013 at 12:00
  • Also note: time already exists in ctime header. Commented Jun 24, 2013 at 12:05
  • 'I am an Indian',how does that relate to being bad in English? Commented Jun 24, 2013 at 12:06

4 Answers 4

5

Your addition operator is using unitinitialized member Inputsec, Inputmint and Inputhours variables. It should look like this:

time operator+(time Inputobj)
{
    return time(sec+InputObj.sec, mint+InputObj.mint, hours+InputObj.hours);
}

or

time operator+(time Inputobj)
{
    InputObj.sec += sec;
    InputObj.mint += mint;
    InputObj.hours += hours;
    return InputObj;
}

Or, even better, implement time& operator+=(const time& rhs); and use it in a non-member addition operator:

time operator+(time lhs, const time& rhs)
{
  return lhs += rhs;
}

You have two sets of member variables representing the same thing. You do not need this duplication.

One final remark: there is something called std::time in header<ctime>. Having a class called time, and using namespace std is asking for trouble. You should avoid both if possible (avoiding the second is definitely possible).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you juan, that expalins everything :)
1

You should rewrite your operator+ at least as follows:

time operator+(time Inputobj)
{
    time blah time(sec+InputObj.sec, mint+InputObj.mint, hours+InputObj.hours);
    return blah;
}

also I think you should use the % operator for getting the correct time results:

time operator+(time Inputobj){
    int s = (sec+InputObj.sec) % 60;
    int m = (sec+InputObj.sec) / 60 + (mint+InputObj.mint) % 60;
    int h = (mint+InputObj.mint) / 60 + (hours+InputObj.hours) % 24;
    return time(s,m,h);
}

Comments

0

Your operator overloading function is using uninitialized variables. Initialize variables inputsec, inputmint, Inputhours in your constructor.

Moreover, try this:

time operator+ (time Inputobj)
{
   time blah (sec+Inputobj.sec, mint+Inputobj.mint, hours+Inputobj.hours);
   return blah;
}

Comments

0

Your error is your publics members with the same name as the parameter constructor, which are unitialized. Try this :

#include <iostream>
using namespace std;

class time
{
private:
    int sec;
    int mint;
    int hours;
public:
time(int Inputsec, int Inputmint, int Inputhours):sec(Inputsec), mint(Inputmint), hours(Inputhours)
{
};

time operator+(time Inputobj)
{
    time blah (sec+Inputobj.sec, mint+Inputobj.mint, hours+Inputobj.hours);
    return blah;
}

void DisplayCurrentTime()
{
    cout << "The Current Time Is"<<endl<< hours<<" hours"<<endl<<mint<<"minutes"<<endl<<sec<<"seconds"<<endl;
}
};

int main()
{
time now(11,13,3);
time after(13,31,11);
time then(now+after);
then.DisplayCurrentTime();
}

1 Comment

Setters/Getters should not be created just for the sake of it, especially for something like an overloaded + operator, as a class has access to private members of it's own type when they are used as input parameters, so you can directly call Inputobj.sec within the + operator function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.