Is this possible

This is a discussion on Is this possible within the C++ Programming forums, part of the General Programming Boards category; I want to make a program that will make a different number of classes based on user input. For example, ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Is this possible

    I want to make a program that will make a different number of classes based on user input. For example, prompt the user with how many cars he/she wants. Then with that information set up a class that will make itself for as many cars as the person has. Another words, is it possible to make a function to set up classes? Then set up a loop to reapeat it as many times as I want?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Use dynamic memory allocation (new & delete), either by using linked lists or allocating an array.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Set up classes? I believe you mean objects of classes. Classes themselves cannot be "created" at run-time, unless you count templated classes, which still aren't created at run-time.
    Code:
    std::vector<Car*> cars; //vector of pointers, to avoid constructor trouble
    
    int input;
    std::cin >> input;
    for(int i = 0; i < input;++i) //Create (input) cars
    {
         cars.push_back(new Car(constructor_parameters));
    }
    
    //Do something with all your (input) new cars
    
    while(cars.size() > 0) //Delete all the cars, free the memory
    {
         delete cars.back();
         cars.pop_back();
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

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