First off, let me say hi! I'm learning C++ more or less on my own, via tutorials on the internet. My main problem is that I understand the exact concept of what I'm trying to accomplish, and that it should be perfectly simple, but I don't know the syntax...
Anyway, I'm trying to learn how to create a new instance of a class (an object) that is named according to user input.
Since I first started this exercise in a coffee shop, I thought I'd try to create a class called "coffeeshop".
1 2 3 4 5 6 7 8
|
class coffeeshop
{
public:
int no_of_customers;
int no_of_employees;
coffeeshop(int, int);
~coffeeshop();
};
|
Then I created my prototype:
1 2 3 4 5
|
coffeeshop::coffeeshop(int x, int y)
{
no_of_customers = x;
no_of_employees = y;
};
|
Easy enough. Now I can make new coffeeshop objects like so:
|
coffeeshop Starbucks(5,2);
|
Piece of cake. I'll have you know, by the way, it took me a while to get my head around the concept of prototypes and constructors. But I managed that leap on my own. :)
But now, I want the user to be able to input the coffeeshop he's sitting in.
1 2
|
string current_coffeeshop;
cin >> current_coffeeshop;
|
And I have NO CLUE how to create a new coffeeshop object using their input.
I tried:
|
coffeeshop current_coffeeshop(0,0);
|
But of course that didn't fly. It's obviously trying to create a new coffeeshop named "current_coffeeshop" and that's not going to work, since that name is already assigned to a string variable.
So how do I get the
contents of my string into the name of a coffeeshop object?
I have a sinking suspicion it involves pointers somehow... I just got that realization there when I was emphasizing the word "contents." I'm also suspicious that it might involve the << or >> operators. I'm still unclear on exactly how they work and what they really do.
Can somebody help me out here? I'm at my wit's end!