Creating class object from user input?

This is a discussion on Creating class object from user input? within the C++ Programming forums, part of the General Programming Boards category; I recently started learning about classes and a question came to me. I know how to create a class object. ...

  1. #1
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280

    Question Creating class object from user input?

    I recently started learning about classes and a question came to me.

    I know how to create a class object. But how would you use user input to create a new object with the name they specified?

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    The way is understand it you want after the program is compiled
    the user to specify the name of a newly created object?

    Impossible

  3. #3
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Yes.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Probably the thing you want is to let the player create a new
    profile in your game and that it creates a new object from the
    player class with the name specified in the profile name right?
    NO CAN DO!

    Though what i do like to know is, if its possible to create an array
    of objects, like 16 objects from the class player for 16 players

  5. #5
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Thanks anyways. Figured it would be something neat to know.

  6. #6
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    It depends what you mean by "name".

    An object can be created at runtime by using "new".
    and the object could have a name, for example
    char name[10]; as a data member.

    But if you mean that you want the object to have a name like the way variables have names in a program, then the answer is no, but why would you want to anyway? Once a program is compiled the names are changed anyway and aren't included in the executable( for example Car car1; or int An_Integer; , after compilation these identifiers are all changed to information more useful to the computer )

  7. #7
    ggs
    ggs is offline
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    taking things literally, you could always write yourself a runtime object system to suit your needs.
    .sect signature

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But how would you use user input to create a new object with the name they specified?
    You can't do this directly, for example, this won't work in C++:
    Code:
    #include <iostream>
    #include <string>
    
    class test
    {
    public:
      test()
      {
        std::cout<<"A new test is born!"<<std::endl;
      }
    };
    
    int main()
    {
      std::string s;
    
      std::cout<<"Enter a name: "<<std::flush;
      std::getline ( std::cin, s );
    
      test s;
    }
    However, using a lookup table of names and matching objects you can create the type of functionality with only a little more effort:
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    class test
    {
    public:
      test ( std::string& name )
      {
        std::cout<<"A new "<< name <<" is born!"<<std::endl;
      }
    };
    
    int main()
    {
      std::string s;
      std::map<std::string, test*> lookup;
    
      std::cout<<"Enter a name: "<<std::flush;
      std::getline ( std::cin, s );
    
      lookup[s] = new test ( s );
      delete lookup[s];
    }
    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    You can use a linked list or vector or some other container without a fixed size. Then if the user wants to create a new object you can add it to the container, although you won't be able to name it... unless you make one of the private variables it's name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Input class
    By Elysia in forum C++ Programming
    Replies: 18
    Last Post: 10-20-2008, 08:45 AM
  3. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Replies: 4
    Last Post: 04-21-2004, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21