Classes in C++

This is a discussion on Classes in C++ within the C++ Programming forums, part of the General Programming Boards category; I have a project that is getting hard for me to do. I've been told I've done the hardest part, ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    23

    Classes in C++

    I have a project that is getting hard for me to do. I've been told I've done the hardest part, but I'm just confused. We are making a program that adds 2 times together. The times are entered by the user. Here is what the assignment is and the code I've done so far.

    ************************************************** **************
    DESCRIPTION

    1.1
    Write a program that uses a class.

    1.2
    Your project will contain files CTime.h, CTime.cpp and TimeApp.cpp.

    1.2.1 (Class declaration)

    * Place the class declaration and inline functions in a seperate header file, CTime.h.

    * Class declaration:

    declare CTime as a class
    public:

    declare a defualt constructor
    define an inline destructor with an empty code body
    declare getHours as a member function
    declare getMinutes as a member function
    declare getSeconds as a member function
    declare setHours, setMinutes, setSeconds as a member function
    declare add as a member function with CTime return and a CTime parameter

    private:

    declare hours, minutes and seconds as an integer

    make the constructor, destructor, get- and set- functions inline.

    1.2.2 (Implementation CTime.cpp)

    * #include file CTime.h

    * place the noninline member function add in a separate file, CTime.cpp

    -convert the time values to total seconds before adding them
    example:
    CTime tm;
    long t1Seconds;
    t1Seconds = (long)tm.hours*3600 + tm.minutes*60 + tm.seconds;
    -add the total seconds together:
    t3Seconds = t1Seconds + t2Seconds;
    -divide them back out to make the new time hours, minutes and seconds.
    - store them in a class CTime variable before returning it.

    1.2.3 (member function add)

    Module add

    Return: CTime

    Paramaters: define t2 as a CTime

    Data:
    define t1Seconds, t2Seconds and t3Seconds as long integers
    Processing:

    convert hours, minutes and seconds to a long and assign to
    t1Seconds

    convert t2.hours, t2.minutes and t2.seconds toa long and assign to t2Seconds

    SET t3Seconds to t1Seconds + t2Seconds

    derive values from t3Seconds to assign to temp.hours, temp.minutes and temp.seconds

    return temp

    END add

    1.2.4 (Application file TimApp.cpp)

    this is where the prgoram asks the user for 2 times entered in this format:

    12:59:59

    the program adds the 2 times and displays the sum


    Here's the code I've done:


    CTime.h file:

    Code:
    /******************************
    * FILE: CTime.h
    * PROGRAMMER: (left blank for now, this is the internet after all)
    * DATE: 01/14/09
    *
    * Declaration of class CTime.
    *******************************/
    #ifndef CTime_H
    #define CTime_Hclass CTime
        
    #include <iostream.h>
    
    
        class   CTime 
        {
        public:
               CTime();
               ~CTime()  {}      
               int getHours();
               int getMinutes();
               int getSeconds();
               int add();      
               void setHours();
               void setMinutes();
               void setSeconds();
               
               
        private:
                int hours;
                int minutes;
                int seconds;
        };
        
        inline int CTime::getHours()
        {      return hours;         }
        inline int CTime::getMinutes()
        {      return minutes;        }
        inline int CTime::getSeconds()
        {      return seconds;        }
        inline int CTime::add(CTime)
        {      return CTime    }
        inline void CTime::setHours(int hr)
        {      hours = hr;              }
        inline void CTime::setMinutes(int min)
        {      minutes = min;             }
        inline void CTime::setSeconds(int sec)
        {      seconds = sec;             }
        
        #endif
    and here's CTime.cpp:

    Code:
    /******************************
    * FILE: CTime.cpp
    * PROGRAMMER: 
    * DATE: 01-14-09
    *
    * Implementation of class CTime.
    *******************************/
    #include "CTime.h"
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int t1Seconds;
        int t2Seconds;
        int t3Seconds;
        
        int CTime::time()
        {
            t1Seconds = (long)time.hours*3600 + time.minutes*60 + tm.seconds;
        
        
                     
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    Now, I really don't know what I'm doing, most of this work is from the notes we were given. I haven't started the application file, but I figure that's easy once the other 2 files are completed. I really just don't know where to go from here, any help would be appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,456
    I noticed that one of the requirements is:
    declare add as a member function with CTime return and a CTime parameter
    However, you declared add as a member function with an int return type and no parameters. Assuming that it is supposed to add the CTime provided as the argument to the current CTime object and return a new CTime object, it should be declared along the lines of:
    Code:
    class CTime 
    {
    public:
        // ...
    
        CTime add(const CTime& other) const;
    
        // ...
    };
    Notice that I declared add to be a const member function. Likewise, getHours, getMinutes and getSeconds should be const member functions since they do not change the observable state of the CTime object.

    Also, I note that you correctly included <iostream> in CTime.cpp, but incorrectly included <iostream.h> in CTime.h. Furthermore, your header inclusion guard in CTime.h is incorrectly written. The macro name in the #ifndef directive should match the one in the #define directive, e.g.,
    Code:
    #ifndef CTIME_H
    #define CTIME_H
    Quote Originally Posted by Kool4School
    I haven't started the application file, but I figure that's easy once the other 2 files are completed. I really just don't know where to go from here, any help would be appreciated.
    Note that according to your requirements, the global main function should be defined in TimeApp.cpp, not CTime.cpp. Why not begin writing the code that should be placed in TimeApp.cpp?
    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
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Also, you probably shouldn't be trying to implement functions within other functions (main).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    CTime add(const CTime& other) const;


    what do you mean by const and other? I'm not really a good programmer, just trying to understand this better.
    Last edited by Kool4School; 01-16-2009 at 11:11 AM. Reason: grammatical error

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    "other" is the name of the argument.
    I also question how much you know. You seem to be missing a lot of basic knowledge. Are you sure you should be where you are? Do you have a book and good grasp of the syntax? I don't really think so.
    As much as we'd love to help you out, it would mostly serve to confuse if you do not understand the basic principles and syntax.

    And the "const" keyword says that the argument may not be modified. If the function tries to do so, it will generate a compile error.
    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.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    The reason I hardly know what I'm doing is because my C programming class was all textbook work over the internet. Every assignment I turned in either barely worked or didn't at all. The teacher gave points for doing the assignment, but never commented on our work. This left me with very little from the class. Now I'm in a C++ class and I'm floundering.

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Well. Your best bet is to forget what you "learned" anyway, and learn C++ now. It's a fun language. You can find a whole host of beginner tutorials on the web, and books at the store. However, be careful if you get a book from the library. It might teach an old version of the language.
    Last edited by CodeMonkey; 01-16-2009 at 11:38 AM. Reason: wrong form of "your," if you must know
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    That's a good idea. I just hope I can figure this out, I can't afford to fail. God I miss Visual Basic, the intellisense held your hand the hold time.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    IntelliSense does pretty much hold your hand (especially with some 3rd party software) all the time.
    Anyway, as much as I would like to see you continue, I do not relish the idea that someone who basically understands nothing of the language to graduate.
    Is there a specific reason you must grade? Because I'd say you should scrap the class and go to a real one with real teaching.
    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.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    Well, this class actually is residential. My first step into C was online and the teacher was horrible. Now I'm expected to know what I'm doing and I don't. I might be able to back out of this class since I just started it, but I don't think I could start up in another C class this quarter.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    I've programmed in Java a little, if I can get past C++ I should be ok. (answering Elysia)

    If I pass C++ I go to Java.
    Last edited by Kool4School; 01-16-2009 at 11:53 AM. Reason: missing information

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    But that's the thing - you hardly know what you are doing!
    Are you going to half-know several languages?
    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.

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    What I'm thinking is I'll learn more from Java because I know some of it already.


    Maybe I should just change my major...
    Last edited by Kool4School; 01-16-2009 at 12:27 PM. Reason: added a thought

  14. #14
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    You don't need to consider a major to learn C++. If learning C++ is just one line on a long list of annoying things to do, then certainly don't do it.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    The thing is I'm going into video game design, I was hoping to get into the design aspect and not the drudging through code. I wanted to visually make a game look good. Now I've got all these programming classes...

    If I change my major I should only have to change it slightly, right?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

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