Solved

declaring an object with a user defined name

Posted on 2000-04-11
7
204 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 66
Find Visual Studio Tools 2 92
is twain_32.dll cmpatible with windows 10 ? 10 123
Why isn't object file created? 6 39
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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

932 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

10 Experts available now in Live!

Get 1:1 Help Now