Classes Continued.

This is a discussion on Classes Continued. within the C++ Programming forums, part of the General Programming Boards category; I understand the basics, and I had a basic user class which worked and I asked how tro do onething ...

  1. #16
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    I understand the basics, and I had a basic user class which worked and I asked how tro do onething and i got some pretty complex replies which im still trying to wrap my head around.

    Basicly what I want to do is just have a user class which i can define the user and then..

    Take name input, take age input, display the name, store a score, etc.

  2. #17
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by Aliaks View Post
    I understand the basics
    I think otherwise. Create the user class, declare and define some functions in their related files. Define your main() function, and use the functions where they need to be used.

    Tell us what you have a problem with. "Don't fully understand how the top part works" isn't quite up to scratch. Help us to help you.
    MSDN <- Programmers Haven!

  3. #18
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Well I had something like this before..

    Code:
    class User {
    
    public:
            
            void setName(string Name);                                 // Accessors
            void setAge(int age);
    
           private:
    
            string Name; 
            int age;
            
    }
    I used my dog class as a example which was this

    Code:
    class Dog                                                          // Begins declaration of class
    {                                                                  //
        public:                                                        // Begins public section
            Dog();
            ~Dog();                                                    //
            void Woof() {cout << "Woof! \n\n";}                        // General functions and so fourth
            void Info()    {;}
        private:
    };
    Would these be correct?

  4. #19
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by Aliaks View Post
    Well I had something like this before..

    Code:
    class User {
    
    public:
            
            void setName(string Name);                                 // Accessors
            void setAge(int age);
    
           private:
    
            string Name; 
            int age;
            
    }
    I used my dog class as a example which was this

    Code:
    class Dog                                                          // Begins declaration of class
    {                                                                  //
        public:                                                        // Begins public section
            Dog();
            ~Dog();                                                    //
            void Woof() {cout << "Woof! \n\n";}                        // General functions and so fourth
            void Info()    {;}
        private:
    };
    Would these be correct?
    Correct? The only way a class can be correct is if it does what it's suppose to do, and only you, the creator, can decide that. We can tell you various things that aren't allowing the class to be correct however, such as:

    Code:
    class Dog                                                          // Begins declaration of class
    {                                                                  //
        public:                                                        // Begins public section
            Dog();
            ~Dog();                                                    //
            void Woof() {cout << "Woof! \n\n";}                        // General functions and so fourth
            void Info()    {;} /*I'm not sure that this is legal, I may be wrong however*/
        private:
    };
    MSDN <- Programmers Haven!

  5. #20
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    The void info line I will later put a cout into that.

    I want the class to take input such as name and age.

    With this input it can then display these when requested

    also, id like to set it so it can store a point value for a points system.

  6. #21
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Well the way I'm thinking of is to take input in main(), and store it into your variables using your setter functions. Then output it when needed using your getter functions.

    e.g.

    User.h
    Code:
    #include <iostream>
    #ifndef USER_H /*Inclusion guards*/
    #define USER_H
    
    class User
    {
    public:
        void vSetUserName(std::string); /*Function for setting the username, takes a string*/
        void vSetAge(int); /*Function for setting the age, takes an int*/
    
        std::string sGetUserName() const; /*Function for retrieving the username*/
        int iGetAge() const; /*Function for retrieving the age*/
    
    private:
        std::string m_sUsername;
        int m_iAge;
    };
    
    #endif /*USER_H*/
    The next step is to define the class member functions in a source file:

    User.cpp
    Code:
    #include "User.h"
    
    void User::vSetUserName(std::string sUsername)
    {
        m_sUsername = sUsername; /*Store the contents of sUsername in m_sUsername*/
    }
    
    void User::vSetAge(int iAge)
    {
        m_iAge = iAge; /*Store the value of iAge in m_iAge*/
    }
    
    std::string User::sGetUserName() const
    {
        return m_sUsername; /*Return the variable to the program*/
    }
    
    int User::iGetAge() const
    {
        return m_iAge; /*Return the variable to the program*/
    }
    The final step is to use the functions to do as you please:

    Main.cpp
    Code:
    #include "User.h"
    
    int main()
    {
        User NewUser; /*Create a User object*/
        std::string sTempUserName; /*Create a temporary variable for input*/
        int iTempAge; /*Create a temporary variable for input*/
    
        std::cout << "Enter Your Username: ";
        std::cin >> sTempUsername; /*Store the user input in the temporary variable*/
        std::cout << "\nAnd now your Age: ";
        std::cin >> iTempAge; /*Store the user input in the temporary variable*/
    
        NewUser.vSetUserName(sTempUserName); /*Set the username*/
        NewUser.vSetAge(iTempAge); /*Set the age*/
    
        std::cout << "\nYour Username is: " << NewUser.sGetUserName();
        std::cout << "\nYour Age is: " << NewUser.iGetAge();
        std::cin.get();
    
        return 0;
    }
    This should do what you just explained.

    NOTE - This was all off of the top of my head, so if you find bugs, point them out.
    MSDN <- Programmers Haven!

  7. #22
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Now, by looking at that code, that is exactly how I got to all that stuff I said about before, with mine being simple and then being blown away with what you just wrote

    It's very nicely laid out and complete so I will spend a while going through it and understanding it and then I will come back haha

    Many thanks

  8. #23
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Hmm

    So what is the point of "temporary" string? Is it removed afterwards? Or did you just state it as a temorary because of some other reason?

  9. #24
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    It's temporary because we declare it in main(), therefore it gets destroyed at the end of main. It's also temporary because we pass it into vSetUserName(std::string), and that's all we do with it.

    By all means, you can call it something else. This is just my programming style.
    Last edited by legit; 07-05-2009 at 07:40 AM.
    MSDN <- Programmers Haven!

  10. #25
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Ok I see, thats fair enough.

    I just was not sure, as I am new I wasn't sure if it was concept.

  11. #26
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    What does cin.get() do?

    I can kinda of guess, but how does it know what to return?
    Last edited by Aliaks; 07-05-2009 at 09:56 PM.

  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    legit: SourceForge.net: Do not remove parameter names - cpwiki
    Aliaks: It waits for the user to enter one character, it's used like sort of like a pause. Look here for more info, or better solutions: SourceForge.net: Pause console - cpwiki
    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. #28
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Thanks

  14. #29
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Thanks Elysia, interesting.
    MSDN <- Programmers Haven!

Page 2 of 2 FirstFirst 12
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. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 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