I have a base class Auto_Part
and several derived classes like class Battery
, class Engine
, class Frame
, etc. I also have a grandchild derived class Car
that inherits all of the derived parts classes.
My goal: I have an Inventory class
and an inventory object that is a map<Auto_Part*, int>
. The first value is the Auto_Part reference being stored, and the second value is how many are there available in the inventory. The user should be able to create a car object using parts available in the inventory. I want to iterate through this map and use the appropriate accessor method to get the data needed to create my car object.
Here is the function in question. I'm trying to use the MVC model so it is a Controller function.
void Controller::add_car_from_parts(){
//All variables used in all of the parts
std::string type, frame_type, category, color, bolt_pattern, tire_type, speed_rating,load_range, w_frame_type;
std::string name, fuel_type, grip_type, horn_type, display_color, seat_material, seat_warmers, door_material, butterfly_doors;
int part_number, cranking_amps, cold_cranking_amps, voltage,diameter,width,ratio, w_diameter,w_width, num_doors,
length,reserve_capacity_minutes,num_cylinders, steering_wheel_diameter, num_seats, display_length;
double price;
int quantity;
Dialogs::message("You will be shown the parts available in the inventory. Please write down the part number of the part you will add to you vehicle.", "PART NUMBER");
view.view_all_inventory();
part_number = view.pn_prompt();
//quantity = view.quantity_prompt();
for(auto x: inventory.get_inventory()){ //inventory is a map<Auto_Part*,int>
if (x.first->get_part_number() == part_number){ // If part is in the inventory
if (x.first->get_type() == "Battery"){// and if the part is a battery, auto_part function available in all auto_part classes including class Auto_Part
name = x.first->get_name(); // auto_part function, available in all auto_part classes including class Auto_Part
price = x.first->get_price(); // auto_part function, available in all auto_part classes including class Auto_Part
cranking_amps = x.first->get_cranking_amps(); // battery function only
cold_cranking_amps = x.first->get_cold_cranking_amps(); // battery function only
voltage = x.first->get_voltage(); // battery function only
reserve_capacity_minutes = x.first->get_reserve_capacity_minutes(); //battery function only
inventory.remove_parts(x.first,1); // removes part from inventory, second value is quantity removed
}
}
}
}
So x.first->get_part_number()
, x.first->get_type()
, x.first->get_name()
, and x.first->get_price()
work fine because they are in the Auto_Part class as well as in all of the derived classes and so when iterating through it points to the appropriate get function. The first error is at the line cranking_amps = x.first->get_cranking_amps()
and my compiler says "class Auto_Part has no get_cranking_amps() function, only battery function."
I thought that since the map is made of Auto_Part*
and not just Auto_Part
objects, it would reference the appropriate class. As in the x would change as it iterated through the map to a Battery object or an Engine object depending on what it was, and I could therefore use its unique functions. How do I call the derived class functions while iterating through this list of base class references? Surely, I don't need to create each of those derived class functions in the base class, right? That seems like it would defeat the purpose of having derived classes with unique functions.
Thank you for your time.