Link to home
Start Free TrialLog in
Avatar of datavirus
datavirus

asked on

declaring an object with a user defined name

how do I get the program (in c++) to declare an object of a class when the user enters the name of that object?

example:
the user wants to create an object of the class Restaurant and he enters the name Wendy's for it.
Avatar of pitonyak
pitonyak


I would assume that the user would not really know that they wanted to create a particular class..... I would assume that the user would simply say that they wanted to create a Restuarant and then you would create a restuarant and then fill in the values....


How you would do this would depend upon what you really mean to do.

Consider the following:

cout << "User, what do you want to create?" << endl;
cin >> item_type;

if (item_type == "restuarant") {
  item = new restuarant;
  item->ask_user_for_values();  // assume that this is a virtual function
} else if (item_type == "grocery") {
  item = new grocery;
  item->ask_user_for_values();  // assume that this is a virtual function
}


Another option might be:

cout << "User, what do you want to create?" << endl;
cin >> item_name;

item_type = figure_out_item_type(item_name);
if (item_type == "restuarant") {
  item = new restuarant;
  item->get_values();  // assume that this is a virtual function
} else if (item_type == "grocery") {
  item = new grocery;
  item->get_values();  // assume that this is a virtual function
}

Avatar of datavirus

ASKER

The only option the user has is to create a restaurant. The code I'm making is for editing a database of restaurants. So what the program would do is receive the name of the restaurant and declare it as a member of that class.
The only option the user has is to create a restaurant. The code I'm making is for editing a database of restaurants. So what the program would do is receive the name of the restaurant and declare it as a member of that class.
ASKER CERTIFIED SOLUTION
Avatar of Andrei Rodionov
Andrei Rodionov
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Andrew,
In this line of code:

Restaurant rstnSample( szName );

Is rstnSample(szName) a method of the class Restaurant that returns the name of the restaurant entered by the user?....

Note that I did not write that particular piece of code, but....

Here is his code again with comments...

// Declare an array of characters to hold the name
//
char szName[32];
//
// Tell the user that you desire the name.
// Note that this may not display because the
// stream has not been flushed. You can do this with
// cout << endl;
// or explicitly as
// cout.flush();
//
cout << endl << "Enter a restaurant name: ";
//
// Read the name
//
cin >> szName;
//
// Create a variable of type Restaurant with the name
// rstnSample. Note that he created a variable of type char*
// called szName above.
// When this variable, rstnSample, is created, it calls the constructor
// which takes a char* (I would recommend a const char*)
// He will probably then copy this into the internal data member of
// the variable rstnSample which is of type Restaurant.
//
Restaurant rstnSample( szName );


Enough for now...
Andy
Hi guys,

Andy, your comments for my code are detailed and accurate. Thanks. And I agree with a const char*...

datavirus, feel free to ask more if there are some difficulties.

Andrew.