title: Is there a way to call derived class methods when iterating through base class references?
body: <p>I have a base class <code>Auto_Part</code> and several derived classes like <code>class Battery</code>, <code>class Engine</code>, <code>class Frame</code>, etc. I also have a grandchild derived <code>class Car</code> that inherits all of the derived parts classes.</p>
  
  <p><strong>My goal:</strong> I have an <code>Inventory class</code> and an inventory object that is a <code>map&lt;Auto_Part*, int&gt;</code> . The first value is the Auto_Part reference being stored, and the second value is how many are there available in the inventory. <strong>The user should be able to create a car object using parts available in the inventory.</strong> I want to iterate through this map and use the appropriate accessor method to get the data needed to create my car object.</p>
  
  <p>Here is the function in question. I'm trying to use the MVC model so it is a Controller function.</p>
  
  <pre><code>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&lt;Auto_Part*,int&gt;
  
      if (x.first-&gt;get_part_number() == part_number){ // If part is in the inventory
          if (x.first-&gt;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-&gt;get_name(); // auto_part function, available in all auto_part classes including class Auto_Part
              price = x.first-&gt;get_price(); // auto_part function, available in all auto_part classes including class Auto_Part
              cranking_amps = x.first-&gt;get_cranking_amps(); // battery function only
              cold_cranking_amps = x.first-&gt;get_cold_cranking_amps(); // battery function only
              voltage = x.first-&gt;get_voltage(); // battery function only
              reserve_capacity_minutes = x.first-&gt;get_reserve_capacity_minutes();  //battery function only
              inventory.remove_parts(x.first,1); // removes part from inventory, second value is quantity removed
          }
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>So <code>x.first-&gt;get_part_number()</code>, <code>x.first-&gt;get_type()</code>, <code>x.first-&gt;get_name()</code>, and <code>x.first-&gt;get_price()</code> 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. <strong>The first error</strong> is at the line <code>cranking_amps = x.first-&gt;get_cranking_amps()</code> and my compiler says "class Auto_Part has no get_cranking_amps() function, only battery function."</p>
  
  <p>I thought that since the map is made of <code>Auto_Part*</code> and not just <code>Auto_Part</code> 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. <strong>How do I call the derived class functions while iterating through this list of base class references?</strong>  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.</p>
  
  <p>Thank you for your time.</p>