This works, could it be better designed?

This is a discussion on This works, could it be better designed? within the C++ Programming forums, part of the General Programming Boards category; Hi guys. I have finished one of the pointer exercises in my book and it works perfectly and I get ...

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

    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
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,479
    Thank you laserlight. All taken onboard
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    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
    22,157
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    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
    22,157
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10:03 AM
  2. explanation of how this works
    By c++.prog.newbie in forum Linux Programming
    Replies: 3
    Last Post: 09-27-2004, 06: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, 09: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, 04:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21