LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-23-2004, 11:00 AM   #1
ToothlessRebel
Member
 
Registered: Jun 2004
Location: FL, USA
Distribution: Slackware 9.1
Posts: 190

Rep: Reputation: 30
Unique class names (C++)


I want to create an instance of a class with name "X", where "X" is defined by the user.

So that, if they want to input information for a new division of a company, they cold input "Marketing" and the program would initialize an instance of the Division class with the name "Marketing". Possible?
 
Old 12-23-2004, 11:11 AM   #2
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
Nope, not possible. That is dynamic run-type type-name binding. C++ is a low-level language and every thing is statically configured at runtime, for efficientcy reasons.

To answer this question you really need to think about what you mean when you say that a class has a name. Do you mean that you want to instaniate an object of the Division class which contains an identifying string "Marketing" enbedded in it?
 
Old 12-23-2004, 12:48 PM   #3
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Not only not possible, not desirable (that I can see). If you are relying on the user to name the class you are creating, then you probably have not spent enough time analyzing your project from an OO perspective.

Also keep in mind that class names are for programmers; the classes should be invisible to the user. It really seems as though bm17's suggestion is more likely, that you have a Division that will be labeled as "Marketing" via user input when the object is instantiated. Is that right? Or why, exactly, would you want a user to name your class?
 
Old 12-23-2004, 12:51 PM   #4
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Sorry, I take that back upon rereading your post. Clearly you want an instance of Division instantiated with the name "Marketing", for example. That can easily be done with a constructor that takes the division name as one of its arguments. Piece of cake.
 
Old 12-23-2004, 09:16 PM   #5
ToothlessRebel
Member
 
Registered: Jun 2004
Location: FL, USA
Distribution: Slackware 9.1
Posts: 190

Original Poster
Rep: Reputation: 30
OK, I have taken a class in C++ and it was believe or not the first one offered at HCC. With that, we did as a class project a POS/INventory software for an imaginary bookstore. Each book was an object of the Book class. Now, being beginers our project had us initialize an array of twenty of these Books, and that was all inventory could do. What I am trying to do is something similar...

Imagine a watch store, with a similar inventory controling/monitoring software system. Each watch is stored in a class object. When I want to add a new watch, I want to create a new object of that class. So if I added Seiko to my inventory, I could create an object of the Watch class named Seiko, with all the member functions of the watch class. So I could use like.... seiko.getRetail();

Is THIS possible is what I was asking.

-TR

PS
;

Like this....

Code:
class Watch
{
private:
float retailPrice;
public:
Watch()
{
XXXXXXX
}
}

int main()
{
string name;
...
Ask and input name of watch to variable name.
...
Watch name()
...
}
If the user inputs seiko into the name variable, will it create an object of the Watch class with the name seiko? I've never seen it done, so can the constructor ask for input or can it only initialize variables?

Last edited by ToothlessRebel; 12-23-2004 at 09:29 PM.
 
Old 12-24-2004, 12:07 AM   #6
bm17
Member
 
Registered: Sep 2004
Location: Santa Cruz, CA, USA
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
No, it can not be done. Variable names, in the sense that you are refering to, exist only for the compiler to refer to while producing code. They do not exist outside of that. At best you can produce a global symbol at compile time. You can not produce new symbols at runtime.
 
Old 12-25-2004, 01:16 AM   #7
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
I really think you need to ask yourself why you want to do this. If a watch needs to be identified by brand name, then that should be a member variable within the class. For example:

Code:
class Watch
{
  private:
    string brandName;
    float retailPrice;
  public:
    Watch( string name, float price )
    {
      brandName = name;
      retailPrice = price;
    }
}

int main()
{
  string name;
  float price;
  // Get name and price from user
  aWatch = Watch( name, price );
}
As others have mentioned, variable names in a program are only for the programmer. In C++, when your program is compiled, a variable name is converted into a memory address where the variable is stored; the name given in the source code no longer exists.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
copying files and give new unique names to each file by using xargs command gnim66 Programming 6 06-22-2005 09:29 PM
create unique user names sabapathys Linux - Software 1 05-12-2005 06:50 AM
create unique user names sabapathys Linux - Newbie 1 05-12-2005 05:09 AM
BlackBox.class & VerifierBug.class virus ??? dalek Linux - Security 4 02-29-2004 09:55 AM
Inheriting class members (Qt C++, QApplication class) jtshaw Programming 2 01-15-2004 12:52 PM


All times are GMT -5. The time now is 08:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration