Thread: Template Problem

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    38

    Template Problem

    Okay here's my dillema. I'm creating a class template, called RentalSystem. This RentalSystem is supposed to accept either a Car class or a Motorcycle class, with each having 3 derived classes for more specific types ( i.e. economy, luxury, midsize, etc. ). Now the way I have it setup is that a function within RentalSystem parses through the command file, and recieves each command. If a vehicle is supposed to be added, the vehicle type is compared with that of the 3 derived classes and the vehicle pointer is set to the address of the derived class. The problem is, now that I'm being asked to use a template, the project requirements prohibit me from explicity making any reference to Car or Motorcycle classes inside the template, i.e. it all has to be <T>. So what I can't figure out is how I'm supposed to create this derived object within the RentalSystem template without explicity creating a Economy object, etc. Sorry if that was unclear, if so I can clarify.

    Thanks,
    Chris

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm not sure I understand, but perhaps a factory method that creates the proper derived type and returns a pointer to the base class would be appropriate. So a static method in Motorcycle that returns a Motorcycle* but inside the function creates an EconomyMotorcycle or LuxuryMotorcycle, etc. based on a function parameter. If that method had the same name and parameters for both Car and Motorcycle, you could just do something like:

    T* newObj = T::Factory(derivedType);

    Also, consider making the car type (economy, luxury, etc) a separate class that works with the vehicle classes instead of making separate derived classes from Car and Motorcycle. That might make better sense design-wise.
    Last edited by Daved; 05-06-2005 at 03:16 PM.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Based on an instructor's suggestion, I'm going to parse the command file in main as opposed to the RentalSystem class. So at this point, what I'm trying to do is simply read in the car type from the command file in main, and send a derived class pointer to a function that will add it to my vector of vehicle pointers. I.e. main would be

    Code:
    NewCar* Car = new NewCar;
    NewLuxury Luxury;
    while ( inStream !eof() )
    {
        if ( command == add_car )
       {
          inStream >> carType;
    
          if ( carType == Luxury )
          {
               NewCar == &Luxury;
          }
        }
    }
    Then I would send NewCar to a member function of RentalSystem called AddVehicle, which would take the object and place it into the private data member; a vector of <T*>. Would this work? I wasn't sure if there'd be any scope issues with it.

    BTW, do you use AIM Daved? Would be easier than posting back and forth. If you use it, my AIM is in my user profile.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Sorry, I don't use AIM. Posting here is better, since I am busy at work now and I will be leaving for the weekend soon. Many other intelligent people can and will help you if you keep posting. If you take it off the board you will get only one person's opinion.

    In the code you have, you are leaking the NewCar you create at the beginning. What you should do is use new to create a new NewLuxury once you know that the type in the file is Luxury. If you need the NewCar* variable outside that block of code, set it to 0 at the beginning and then reassign it when you determine which type to create. You also should not use eof() to control your while loop (see one of many threads about this lately). If this is just pseudo-code, then you have the idea, wait until you find the type, then create the appropriate object and pass a pointer to it to the be stored in your vector.
    Last edited by Daved; 05-06-2005 at 04:20 PM.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Come to think of it, I don't think I need to create an instance of NewCar. Something like this would be better, right?

    Code:
            if ( type == ECONOMY )
            {
               Economy* AddCar = new Economy;
            }
            else if ( type == LUXURY )
            {
               Luxury* AddCar = new Luxury;
            }
            else if ( type == MIDSIZE )
            {
               Midsize* AddCar = new Midsize;
            }
    Then simply pass AddCar to the function that adds it to the vector. How does that sound?

    Edit: To clarify, I wrote the classname wrong in the previous post. The 3 derived classes are Luxury, Midsize, and Economy, not NewLuxury, etc.
    Last edited by ChristianTool; 05-06-2005 at 04:33 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Sounds good. You might have to do it like this so that the AddCar variable doesn't go out of scope:
    Code:
    Car* newCar = 0;
    if ( type == ECONOMY )
    {
        newCar = new Economy;
    }
    else if ( type == LUXURY )
    {
        newCar = new Luxury;
    }
    else if ( type == MIDSIZE )
    {
        newCar = new Midsize;
    }
    // Add newCar to your vector here.
    That assumes that Economy, Luxury, and Midsize all derive from Car.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Yes, they all derive from Car. My plan was to do something like

    Code:
    Car* newCar = 0;
    if ( type == ECONOMY )
    {
        newCar = new Economy;
         Rental.AddVehicle ( newCar );
    }
    else if ( type == LUXURY )
    {
        newCar = new Luxury;
         Rental.AddVehicle ( newCar );
    }
    else if ( type == MIDSIZE )
    {
        newCar = new Midsize;
         Rental.AddVehicle ( newCar );
    }
    Since the car pointer vector is a private data member of the Rental class. That should still work, right?

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's fine. If you ever have time to come back to this you should consider using a Factory method instead, so you could do it all in one piece of code instead of duplicating code in the if and else blocks.
    Code:
    // Defined somewhere to return proper derived class.
    newCar* CreateNewCar(CAR_TYPE type);
     
    ...
     
    // Later just call the function ...
    Rental.AddVehicle(CreateNewCar(type));
    Like I said, don't worry about that now, it is just a slightly better design, but if you have time come back to it and understand why it is better.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    Thanks, I'll give it a shot. I'm not quite to the point where I can test it, because I'm still transfering things from the RentalSystem to main, cleaning it up in functions, etc; the dirty work. I'll have to check out Factory methods once the semester is over. 3 more weeks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template object creation problem
    By babu198649 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2008, 04:02 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  5. Problem with template usage
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2002, 06:30 PM