Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

declaring an object with a user defined name

Posted on 2000-04-11
7
Medium Priority
?
246 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:datavirus
  • 3
  • 2
  • 2
7 Comments
 
LVL 5

Expert Comment

by:pitonyak
ID: 2706462

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
}

0
 

Author Comment

by:datavirus
ID: 2706563
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.
0
 

Author Comment

by:datavirus
ID: 2706575
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Accepted Solution

by:
AndrewRodionov earned 200 total points
ID: 2706652
Hi datavirus,

You can declare your class Restaurant that contains the instance name, e.g.

class Restaurant {
private:
  char szName[32];
public:
  Restaurant();
  Restaurant( char* pszName );
  ...
  const char* GetName() const;
};

When the user enters restaurant name you instantiate your class, e.g.

char szName[32];
cout << endl << "Enter a restaurant name: ";
cin >> szName;
Restaurant rstnSample( szName );

So you have the named class instance, i.e. the named object, and can check its name calling rstnSample.GetName().

Hope that helps.
Andrew.
0
 

Author Comment

by:datavirus
ID: 2707871
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?....
0
 
LVL 5

Expert Comment

by:pitonyak
ID: 2709980

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
0
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2710501
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question