Solved

declaring an object with a user defined name

Posted on 2000-04-11
7
217 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
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.

 
LVL 2

Accepted Solution

by:
AndrewRodionov earned 50 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

829 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