Changing an object's property through console
Working on a console application and I am trying to figure out a way that allows users to enter an object property along with a value. For example
HTML Code:
class Box{
public:
int height;
int width;
int length;
};
int main(){
Box ab;
string Name,value
cin>>Name>>value;
map<string,string> propMap
propMap[name] = value;
}
For example if a user inputs "height" for Name and "13" as value I would like to find a way to change ab's height to 13. I want to make this work so that one can add another class and get the same functionality.
I tried looking online and I think maps are a way to do this and I tried doing that but I don't know how to proceed after/what code to write that would allow me to change it.
Note: I can't use if else statements or hardcoding to match the input to member as I want to make this extensible.
Re: Changing an object's property through console
Why do you want to do this? What are you really trying to achieve?
Re: Changing an object's property through console
Just a way so that given a class, its member variables can be edited no matter what new class a user enters.
Re: Changing an object's property through console
Quote:
Originally Posted by
sunnykumar4434
Just a way so that given a class, its member variables can be edited no matter what new class a user enters.
We know that.
I'll try to reask the question:
At a higher-level, what are you trying to achieve? What real-world problem (forget about the language that you're using) are you trying to solve? Whatever it is, there are more than likely better paradigms and design patterns that can be used. In C++, there is no standard way for you to write a class at compile time and expect the runtime environment to know what the member variable names are.
Regards,
Paul McKenzie
Re: Changing an object's property through console
Quote:
Originally Posted by
Paul McKenzie
We know that.
I'll try to reask the question:
At a higher-level, what are you trying to achieve? What real-world problem (forget about the language that you're using) are you trying to solve? Whatever it is, there are more than likely better paradigms and design patterns that can be used. In C++, there is no standard way for you to write a class at compile time and expect the runtime environment to know what the member variable names are.
Regards,
Paul McKenzie
It's a part for an assignment for my class and one of the questions is to achieve such a functionality.
Re: Changing an object's property through console
Quote:
Originally Posted by sunnykumar4434
Just a way so that given a class, its member variables can be edited no matter what new class a user enters.
I suspect that instead of "class", you mean "object", i.e., you want to write code such that given an object of the Box class, the member variables of the object can be changed depending on the user input. You can store these objects in a map, and thus the user chooses the object to change based on the key. Is this what you are thinking of?
Re: Changing an object's property through console
Quote:
Originally Posted by
laserlight
I suspect that instead of "class", you mean "object", i.e., you want to write code such that given an object of the Box class, the member variables of the object can be changed depending on the user input. You can store these objects in a map, and thus the user chooses the object to change based on the key. Is this what you are thinking of?
Yes, how would I go on doing that?
Re: Changing an object's property through console
Oh, in that case, you have little choice other than to use "if else statements or hardcoding" (e.g., equivalently, a switch), unless you can change the class definition. If you can change the class definition, then I would go for a std::map<std::string, int> member (or std::unordered_map), seeing that the current members are all ints.