LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Unique class names (C++) (https://www.linuxquestions.org/questions/programming-9/unique-class-names-c-269579/)

ToothlessRebel 12-23-2004 10:00 AM

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?

bm17 12-23-2004 10:11 AM

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?

zaichik 12-23-2004 11:48 AM

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?

zaichik 12-23-2004 11:51 AM

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.

ToothlessRebel 12-23-2004 08:16 PM

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?

bm17 12-23-2004 11:07 PM

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.

wapcaplet 12-25-2004 12:16 AM

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.


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