# conversion functions

• 01-18-2003
WildBill
conversion functions
Hello...Im having a problem figuring out how to convert a long datatype to an object...as well converting a user-defined type to a long datatype.....here is what i wrote so far...any help would be appreciated. Thanks!

Code:

class Time
{
private:
int hrs;                      //1 to 23
int mins;                      //0 to 59
int secs;                            //0 to 59
public:                          //no-arg constructor
Time() : hrs(0), mins(0), secs(0)
{  }
.....

Time(long allSeconds)
{
allSeconds = Time(secs);

}
operator long()
{
return secs;
}
};
void main(void_
{
......

// Statement to test the conversion functions
long  allSeconds = 77777;
Time t6(allSeconds);
cout << '\n' << allSeconds << " converts to ";
t6.display();
allSeconds = long(t5);
cout << '\n'; t5.display();
cout <<" converts to " << allSeconds;
}

• 01-18-2003
ammar
You can merge the hours, minutes and seconds into one long integer, like doing this: hours*10000 + minutes*100 + seconds, and make the function operator long() return this value...
And the reverse process is easy, just take that value as an argument and break it back...
Isn't that what you wanted?
• 01-18-2003
WildBill
No, thats not what I want... I need it to go to that function and inside the function to do some kind of conversion and send it back
• 01-18-2003
Sebastiani
1) main() returns an integer, not void.
2) undeclared function "display()".
3)

Time(long allSeconds)
{
allSeconds = Time(secs);
}

This doesn't set the other members to zero as the "no arg" destructor does....

4) allSeconds = long(t5);
...should be simply:
allSeconds = t5;

5) Don't think you conversion will work with cout, it won't. You have to define a friend function to do that. And it won't work for va_arg functions either, like printf, sprintf, etc.