Thread: This works, could it be better designed?

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    This works, could it be better designed?

    Hi guys.

    I have finished one of the pointer exercises in my book and it works perfectly and I get the correct outputs, but could I have written it anymore efficiently? I have to use a struct with functions as shown below. I had to pass the object to the function which I did also. I know using a class would of been better but I was told to use a struct.

    Here is my code:

    Code:
    #include <iostream>
    
    // struct definition
    struct Time
    {
       int m_Hour;
       int m_Minute;
       int m_Second;
    
       // struct functions
       void setTime( Time *t );
       void getTime( Time *t );
    };
    
    // function to input the tume by user
    void Time::setTime ( Time *t )
    {
       std::cout << "Enter the hour: ";
       std::cin >> m_Hour;
       std::cout << "\nEnter the minutes: ";
       std::cin >> m_Minute;
       std::cout << "\nEnter the seconds: ";
       std::cin >> m_Second;
    }
    
    // function to display the time
    void Time::getTime( Time *t )
    {
       std::cout << "Time is: " << m_Hour << " :: " << m_Minute << " :: "
                 << m_Second << std::endl;
    }
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main(int argc, char *argv[])
    {
       Time *t = new Time;
    
       t->setTime( t );
       t->getTime( t );
    
       delete t;
       t = NULL;
    
       std::cin.get(); // freeze console output window
       std::cin.ignore();
    
       return 0; // return value from int main
    }
    Double Helix STL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Firstly, setTime() and getTime() need not be member functions. Alternatively, they can be member functions, but then they do not take any arguments but instead work on the *this object.

    Next, you do not actually need to use new/delete:
    Code:
    int main(int argc, char *argv[])
    {
       Time t;
    
       setTime( &t ); // or t.setTime(); if it is a member function
       getTime( &t ); // or t.getTime(); if it is a member function
    
       std::cin.get(); // freeze console output window
       std::cin.ignore();
    
       return 0; // return value from int main
    }
    Of course, the only use of pointers here is to simulate pass by reference, but since C++ has pass by reference, you could just use that instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thank you laserlight. All taken onboard
    Double Helix STL

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think you also forgot const correctness - those functions could take a const pointer since they never modify it!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think you also forgot const correctness - those functions could take a const pointer since they never modify it!
    I did forget about const correctness, but I would be more concerned about Time not being const in getTime(), and getTime() not being const if it was a member function. The const pointer thing would be moot if C++ references were used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If references were used, then const references would have to be used...
    getTime should probably be const too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If references were used, then const references would have to be used...
    For getTime() as a free function, but setTime() as a free function would still need to take by reference only. I suspect that making them member functions is not a good idea since they are more than simple setters/getters in that they actually perform I/O.

    I think that the const pointer thing is not important since changing the pointer does not affect the caller. Changing what is pointed to does affect the caller, but this is guarded against by making Time const (or a const reference). As a side effect of using references, one automatically cannot change the reference (i.e., a reference is like a const pointer), so the implementation is also benefits.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anybody show me why this works??
    By tzuch in forum C Programming
    Replies: 2
    Last Post: 03-29-2008, 09:03 AM
  2. explanation of how this works
    By c++.prog.newbie in forum Linux Programming
    Replies: 3
    Last Post: 09-27-2004, 05:59 PM
  3. Works in MSVC++ 6.0 but not in Dev-C++
    By smitsky in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2004, 08:07 PM
  4. fprintf works in one function but not in the other.
    By smegly in forum C Programming
    Replies: 11
    Last Post: 05-25-2004, 03:30 PM