Creating a new object with a user-defined name

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!
The name given to an object is only relevant in the source code.
You can add a string object into your class:
1
2
3
4
5
6
7
8
9
10
11
12
class coffeeshop
{
      public:
             string name;
             int no_of_customers;
             int no_of_employees;
             coffeeshop(int, int);
             ~coffeeshop();
};

coffeeshop current_coffeshop(0,0);
cin >> current_coffeshop.name;

OK, I'm following. It's a lot easier to get the user to input a string for an existing string variable than for an actual new object.

But what would I do if I wanted to user to be able to input more than one coffeeshop? Maybe my program could ask them how many coffeeshops they want to enter, and then let the user type in the names (and number of employees/customers) for each one of them.
(After all, an object isn't much use if I'm only going to have ONE of it, if I'm understanding the benefits of object-oriented programming correctly.)

I know the whole "coffeeshop" thing is silly, but I think it illustrates the concepts to me well enough. :)
Topic archived. No new replies allowed.