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.
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
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.
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.
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.
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.
Re: Can I create an object with the user defined name?
Originally Posted by hayloiuy
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.
Bookmarks