Changing an object's property through console
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Changing an object's property through console

  1. #1
    Join Date
    May 2013
    Posts
    4

    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.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,544

    Re: Changing an object's property through console

    Why do you want to do this? What are you really trying to achieve?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    May 2013
    Posts
    4

    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.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Changing an object's property through console

    Quote Originally Posted by sunnykumar4434 View Post
    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
    Last edited by Paul McKenzie; May 27th, 2013 at 10:34 PM.

  5. #5
    Join Date
    May 2013
    Posts
    4

    Re: Changing an object's property through console

    Quote Originally Posted by Paul McKenzie View Post
    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,315

    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?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    May 2013
    Posts
    4

    Re: Changing an object's property through console

    Quote Originally Posted by laserlight View Post
    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?

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,315

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center