title: Looping through the derived classes by referencing the base class in C++
body: <p>Sorry, if this has been asked before, just learning C++, had tried to search for it but not sure what the keyword is.</p>
  
  <p>would it possible to do this?</p>
  
  <pre><code>class Car {
     public:
        void addColor(string c) {
            color = c;
        }
  
     private:
        string color;
  }
  
  class Honda:public Car {}
  class Toyota:public Car {}
  
  int main() {
      vector&lt;Car&gt; v;
  
      Honda *car1 = new Honda();
      car1.addColor("green");
  
      Toyota *car2 = new Toyota();
      car2.addColor("blue");
  
      v.push_back(car1);
      v.push_back(car2);
  
      for (int i = 0; i &lt; v.size(); i++) {
          cout &lt;&lt; v[i].color &lt;&lt; endl; // should output green, blue
      }
  
  }
  </code></pre>
  
  <p>if it's possible, what is the most right/effective way to do it?</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>wow, thank you everybody!</p>