Thread: conversion functions

  1. #1
    WildBill
    Guest

    Smile 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;
    }

  2. #2
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    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?
    none...

  3. #3
    WildBill
    Guest
    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

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM