Thread: arrays as class names

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1

    Question arrays as class object names

    I need to create a program that allows a user to create up to 10 class objects (rationals), so a for loop seems logical... BUT I need to give each rational a name so I can call the constructor. Can I use an array here in the loop like this: rational name[i] (x, y)?
    And if so, do I make the array a char array, or do I need to declare an array of class objects, and then construct each element with the parameters input by the user?

    The next problem is that the objects each need to be stored and accessible to the user to perform member functions. Can I call a member function using an array for the calling object and for the argument (eg. name[i].add (rational name[i+2]) - where the function is rational rational::add(rational b))???

    I'm completely stumped and feeling really foolish...

    Any advice much appreciated!
    Last edited by Katle; 11-25-2002 at 09:58 AM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is a map with string keys and function pointer values. Call a function that will create a specific object based on the string name the user inputs.

    Kuphryn

  3. #3
    Satya
    Guest

    Maybe useful

    Here is what I would do

    Rational* name[10];

    for(i=1 to 10)
    {
    name[i] = new Rational(x,y);
    }

    I am guessing your x,y will have to come from a switch statement, since the values are going to be different between two objects.

    To access functions, you could do

    Rational* pObject = name[5] ; //for example
    pObject->functionName();

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. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Replies: 8
    Last Post: 07-24-2006, 08:14 AM
  4. Replies: 1
    Last Post: 04-20-2003, 05:02 PM
  5. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM