Thread: Classes Mess

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    Classes Mess

    Ok I have read up on classes and I modelled this one from an example out of my book I got from the library. Now I understand how it works in the book and I have got other classes to work, but I'm having a bit of trouble with this one.

    This is the part from my Classes.H
    Code:
    class User
    {
        public:
    
        User (string name);
        ~User();
    
        char GetName() { return GetName;}       // I want this function to get the players name from input
    
        void setName(string userName);          // This is a ... Cant remember the terminology.
        void setAge(string userAge);            // Same here.
    
        private:
    
        const string& userName;     
        const string& userAge;
    
    };
    and how would I got about implementing this into my main file.

    Would I do something like

    string player;

    getName(player);

  2. #2
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Aliaks, there are a number of problems you will have with this class. The biggest is that you are storing refenences to strings. This is a problem becuase you are passing in a temporary copy to name in your constructor. When the constructor completes, the copy is destroyed and the reference is invalid. The class members should be strings, not references to strings. You should pass in references in your constructor so a temporary copy does not have to be made during the call of the constructor. Even so, you should store copies of strings, not references.

    If userName and userAge are declared const, it means they will not change, so there is no point in having a setName or a setAge method.

    You should initialize age in the constructor.
    You don't need a destructor because the class is not allocating any resources that are not released automatically.

    Try this:
    In classes.h
    Code:
    #include <string>
    using namespace std;
    class User
    {
        public:
    
        User (const string name&, const string& age);
    
        string GetName();
        string GetAge();
    
        void setName(const string&  userName);
        void setAge(const string& userAge);
    
        private:
        string userName;     
        string userAge;
    
    };
    in classes.cpp
    Code:
    #include <iostream>
    #include "classes.h"
    User::User(const string& name, const string& age) : userName(name), userAge(age) { }
    
    string User::GetName() { return userName; }
    string User::GetAge() { return userAge;}
    
    void User::setName(const string& name) { userName = name;  }
    void User::setAge(const string& age) { userAge = age; }
    
    int main(void)
    {
    
    	string name = "MyName";
    	string age = "99";
    
    	User u(name, age);
    
    
    	cout << "name is " << u.GetName() << endl;
    	cout << "age is " << u.GetAge() << endl;
    }

    Does that help you?

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Quote Originally Posted by Zlatko View Post

    in classes.cpp
    Code:
    int main(void)
    {
    
    	string name = "MyName";
    	string age = "99";
    
    	User u(name, age);
    
    
    	cout << "name is " << u.GetName() << endl;
    	cout << "age is " << u.GetAge() << endl;
    }
    Thanks very much for that information, I am obviously not very strong with the concepts of C++ yet but that is indeed helpful.

    With the part of the code above can I change that to a cin function to take the user input instead? and will that store it in the class?

  4. #4
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Well, I don't know all the capabilities of cin, but something simple like this should work
    Code:
    	string name;
    	string age;
    
    	cout << "Name:";
    	cin >> name;
    	cout << "Age:";
    	cin >> age;
    
    	User u(name, age);

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Thanks very Much

    That is very very helpful.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not forget the setters either (the terminology you were missing).

    This is possible too:
    Code:
    	string name;
    	int age;
    	User u;
    
    	cout << "Name:";
    	cin >> name;
    	cout << "Age:";
    	cin >> age;
    
    	u.setAge(age);
    	u.setName(name);
    For an extra effect (and somewhat of an optimization), you could change the class definition to this:
    Code:
    #include <string>
    using namespace std;
    class User
    {
        public:
    
        User (const string name&, int age);
    
        const string& GetName();
        int GetAge();
    
        void setName(const string&  userName);
        void setAge(int userAge);
    
        private:
        string userName;     
        int userAge;
    
    };
    Now why did I do this?
    Because your class stores a name, you can safely return a reference to it instead of copying it. Because it's a getter, it must also make sure that the name will never be changed from the outside, thus it must return a const reference.

    Also note how I changed the type of the age from string to int. We want a numerical age, after all, not a string age.
    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
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Ahh I seeeee Thanks so much for your help.

    One stupid question though, in terms of coding, how do you go about optimisation?

  8. #8
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Generally, as Elysia and I demonstrated, pass around and return references instead of making copies of objects. Any function declaration should use the reference modifier ('&') for any objects unless there is a reason for making copies. To ensure that your coding doesn't accidentally modify the object being referred to, you can specify const for the parameter.
    For example
    void foo(const string& s);
    The same goes for return values. You can specify
    const string& getString();
    Then getString can only be assigned to a const reference.
    const string myString& = myObject.getString()

    When storing objects in containers, you can allocate the objects on the heap (using new), and store the pointers. Otherwise the object is copied multiple times before getting into the container. This becomes more important as objects get bigger.

    Objects can become deceptively large when using inheritance. The class you develop might be small, but it might inherit from a huge one that you have no idea about.

    The other place to optimize is your algorithms. That's a whole other topic.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    Code:
    #include <iostream>
    #include "classes.h"
    User::User(const string& name, const string& age) : userName(name), userAge(age) { }
    
    string User::GetName() { return userName; }
    string User::GetAge() { return userAge;}
    
    void User::setName(const string& name) { userName = name;  }
    void User::setAge(const string& age) { userAge = age; }
    I tried this from the above post and got an entire page of errors, is
    Code:
    int main (void)
    have to be used to use it , if so what does void do-?

    also, the errors contained something along the lines of getline being wrong or something..

    How can that be wrong, isnt that a standard function ??

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Quote Originally Posted by Zlatko View Post
    Generally, as Elysia and I demonstrated, pass around and return references instead of making copies of objects. Any function declaration should use the reference modifier ('&') for any objects unless there is a reason for making copies. To ensure that your coding doesn't accidentally modify the object being referred to, you can specify const for the parameter.
    For example
    void foo(const string& s);
    The same goes for return values. You can specify
    const string& getString();
    Then getString can only be assigned to a const reference.
    const string myString& = myObject.getString()

    When storing objects in containers, you can allocate the objects on the heap (using new), and store the pointers. Otherwise the object is copied multiple times before getting into the container. This becomes more important as objects get bigger.

    Objects can become deceptively large when using inheritance. The class you develop might be small, but it might inherit from a huge one that you have no idea about.

    The other place to optimize is your algorithms. That's a whole other topic.
    I see, I honestly reply with a huge about to what you said as I am quite new to cpp and that will take me a little while to digest But thanks kindly for the help

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The void does nothing and can be removed.
    As for the errors, do post a list of them next time.
    We can only as much as guess otherwise.
    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.

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, 09: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, 05:38 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM