Variable class name
is there a way to make name of a declared class variable? Like:
Say you have a class named Cat:
Can i say: Cat name;
where name = frisky or somthing the user inputs?
or do i have to give the class a definent name
What are classes most used for? I would think games would be a major place where classes are used but where else?
you can NOT create a variable with a name from a users input. This would also not help, because your code could not reference the variable before it is named, making the created entity useless.
You can however put the user's entered string into the variable and search against it or use it as with any other piece of data ... simply add "name" to the class and use it.
Classes are used everywhere. They allow the creation of powerful tools that can be reused; the ability to lump functions and data together into a package that works and can be reused in many programs is very powerful for any set of tasks. The ability to inherit to build large tools from existing small ones is very powerful as well, and the additional support for making classes more generic in C++ provides amazing power (consider the built in vector type which can hold anything in an array like structure and perform many handy functions on it).
I wasnt suggesting to create the variable after i named the class. But dont i have to define an object before i can pass variables to it. So i want to make the object name that i am defining variable.
So basically i am asking the user to input a name and then i want the program to define an object from the class with the name they input. But i dont think i can just do
because it will think that name is the name of the object i am defining and not the string the user inputed. Is there a way around this?
you cannot do what you are wanting to do in c++. You cannot even make a reference or a pointer that will do the trick.
if you could, imagine...
cin >> name; //user types "bob"
bob.field = value; //how did you know he would type bob? what if it was sue?
there would be no way to use the object by name in your code, because it would not exist at compile time only at run time.
the reverse is very frustrating ... you cannot get the name of a variable if you want to print it (in particular, enums would be far more powerful if you could do that!)
how then would i go about creating objects from classes on the fly?
So, say i have a game and i want to make a object from my class Cat. What could i do to create the object on the fly if i cant use variables as the name.
You're confusing two concepts here: creating objects on the fly and naming objects. You can create objects dynamically by using new. You don't have to read the name of the object from cin. You can also use an array of objects. In that case, each object is referred to by an index, not by its name.
Last Post: 04-19-2007, 07:44 PM
By velkropie in forum Java
Last Post: 03-24-2007, 12:09 PM
Last Post: 04-26-2006, 05:20 AM
By evac-q8r in forum Java
Last Post: 01-16-2006, 05:04 PM
By Shailesh C.Rathod in forum .NET
Last Post: 03-13-2002, 08:53 PM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center