CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2010
    Posts
    26

    Can I create an object with the user defined name?

    As the title suggest i want to create an object with the name that was entered by the user into the console.

    This code looks stupid but you will get the idea what i want to do (although it doesn't work):

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    string name;
    
    string *p;
    
    p=&name;
    
    cin>>name;
    
    cout<<*p<<endl;
    
    string [name];
    
    system("pause");
    return 0;
    }
    If possible i dont want to use array because i am doing a pet clinic management system. Asking user how many pets they wanna admit into the clinic is unrealistic. The above code is not the code for the program but a "thought experiment" program about the problem i mentioned.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Can I create an object with the user defined name?

    Instead of trying to create a variable whose name is the string entered by the user, what you can do is to associate that string with the other information, e.g., create a Pet class with a name member variable of string type, or perhaps create a std::map of pet names to pet objects.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Can I create an object with the user defined name?

    Quote Originally Posted by hayloiuy View Post
    As the title suggest i want to create an object with the name that was entered by the user into the console.
    It's still confusing as to what you want. The title of this thread or the fake code you posted doesn't clear this up.
    Code:
    #include <string>
    #include <iostream>
    
    class SomeObject
    {
        std::string name;
    
        public:
            SomeObject( std::string& theName) : name( theName ) { }
            std::string getName() const { return name; }
    };
    
    int main()
    {
       std::string name;
       cin >> name;
       SomeObject obj( name );
       cout << obj.getName();
    }
    This code creates an object with a name and outputs the name. Is this what you mean? Or do you mean create an object from a name, where the name determines what object to create?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 11th, 2010 at 10:39 AM.

  4. #4
    Join Date
    Apr 2010
    Posts
    26

    Re: Can I create an object with the user defined name?

    What i meant was..

    In the console:
    enter the name of your pet:John

    In the code:
    ....bla bla
    ....bla bla
    ....bla bla

    pet John; //pet is the class and John is the name of the animal.
    //when the user enter the name of the pet a new object instance is created with the
    //name of the pet as the class' name.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Can I create an object with the user defined name?

    Ah, then I guessed correctly. My suggestions apply, and in fact Paul McKenzie's example is an example of my first suggestion.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Apr 2010
    Posts
    26

    Re: Can I create an object with the user defined name?

    Ok... sorry i am still a beginner but what is std::map? And why Paul McKenzie's code does not have "using namespace std" and is he using constructor initializer list? Thanks for your time.

  7. #7
    Join Date
    Apr 2010
    Posts
    26

    Re: Can I create an object with the user defined name?

    Oh yeah i just figured vector is also plausible right? I can encapsulate the program by hiding the number of pets i have. I wont be replying for a while. I have class tomorrow morning. I need to sleep now. Thank you in advance to anyone who replied my question.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Can I create an object with the user defined name?

    Quote Originally Posted by hayloiuy
    what is std::map?
    A container that maps keys to values

    Quote Originally Posted by hayloiuy
    And why Paul McKenzie's code does not have "using namespace std"
    Because those names from the std namespace are fully qualified.

    Quote Originally Posted by hayloiuy
    is he using constructor initializer list?
    Yes. (Though I note that the constructor parameter is a non-const reference when it should be a const reference.)

    Quote Originally Posted by hayloiuy
    Oh yeah i just figured vector is also plausible right?
    Probably, but it depends on what exactly you are trying to do.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Can I create an object with the user defined name?

    Quote Originally Posted by hayloiuy View Post
    What i meant was..

    In the console:
    enter the name of your pet:John

    In the code:
    ....bla bla
    ....bla bla
    ....bla bla

    pet John; //pet is the class and John is the name of the animal.
    //when the user enter the name of the pet a new object instance is created with the
    //name of the pet as the class' name.
    Let's say you did that. How would the rest of the code know how to refer to John? A map or a database is what you want.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured