Solved

declaring an object with a user defined name

Posted on 2000-04-11
7
201 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now