-
July 11th, 2010, 10:10 AM
#1
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.
-
July 11th, 2010, 10:16 AM
#2
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.
-
July 11th, 2010, 10:19 AM
#3
Re: Can I create an object with the user defined name?
Originally Posted by hayloiuy
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.
-
July 11th, 2010, 10:33 AM
#4
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.
-
July 11th, 2010, 10:36 AM
#5
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.
-
July 11th, 2010, 10:42 AM
#6
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.
-
July 11th, 2010, 10:46 AM
#7
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.
-
July 11th, 2010, 10:56 AM
#8
Re: Can I create an object with the user defined name?
Originally Posted by hayloiuy
what is std::map?
A container that maps keys to values
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.
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.)
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.
-
July 11th, 2010, 11:00 AM
#9
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|