title: Member variables polymorphism
body: <p>I begin with C++ and I have trouble to use the polymorphism, especially when I want to use it in constructors of a new class. I know the issue has been treated <a href="https://stackoverflow.com/questions/17035951/member-variable-polymorphism-argument-by-reference">here</a> but I did not understand and my case is a little bit different.</p>
  
  <p>To understand how to use it I built this little program. Here is the <code>main.cpp</code> : </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;headers.h&gt;
  using namespace std;
  
  void Display(Car &amp;c)
  {
      c.Show();
  }
  
  int main()
  {
      Car Clio(50);
      SportCar Ferrari(200,200);
      Clio.Show();
      Ferrari.Show();
  
      Display(Ferrari);
  
      Race(Ferrari, Track());
  
      return 0;
  }
  </code></pre>
  
  <p>The <code>headers.h</code> file :</p>
  
  <pre><code>#ifndef CAR_H
  #define CAR_H
  
  class Car
  {
      public:
          Car(double s);
          Car();
          virtual void Show() const;
  
      protected:
          double m_Speed;
  
      private:
  };
  
  #endif // CAR_H
  
  
  #ifndef SPORTCAR_H
  #define SPORTCAR_H
  
  class SportCar : public Car
  {
      public:
          SportCar(double s, double p);
          SportCar();
          virtual void Show() const;
  
      protected:
  
      private:
          double m_Price;
  };
  
  #endif // SPORTCAR_H
  
  
  #ifndef TRACK_H
  #define SPORTCAR_H
  
  class Track
  {
      public:
          Track(double l);
          Track();
  
      protected:
  
      private:
          double m_Length;
  };
  
  #endif // SPORTCAR_H
  
  #ifndef RACE_H
  #define RACE_H
  
  
  class Race
  {
      public:
          Race(const Car&amp; , Track t);
  
      protected:
          Car &amp;m_Car;
          Track m_Track;
  
      private:
  };
  
  #endif // RACE_H
  </code></pre>
  
  <p>And finally, the <code>src.cpp</code> file : </p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;headers.h&gt;
  using namespace std;
  
  Car::Car(double s)
  {
       m_Speed = s;
  }
  
  Car::Car()
  {
      m_Speed = 50;
  }
  
  void Car::Show() const
  {
      cout &lt;&lt; "I'm a car" &lt;&lt; endl;
  }
  
  SportCar::SportCar(double s, double p) : Car(s)
  {
      m_Price = p;
  }
  
  SportCar::SportCar() : Car()
  {
      m_Price = 200;
  }
  
  void SportCar::Show() const
  {
      cout &lt;&lt; "I'm a sportcar" &lt;&lt; endl;
  }
  
  Track::Track(double l)
  {
      m_Length = l;
  }
  
  Track::Track()
  {
      m_Length = 10;
  }
  
  Race::Race(const Car&amp; c, Track t)
  {
      m_Car = c;
      m_Track = t;
      m_Car.Show();
  }
  </code></pre>
  
  <p>In summary I have two differents classes of car, one of which (<code>SportCart</code>) inherits from the other (<code>Car</code>) with two differents <code>Show()</code> methods. And I have a class <code>Race</code> whose members are a kind of car and a <code>Track</code>. When I use the <code>Race</code> constructor I can't pass in argument the car as a <code>SportCar</code>.</p>
  
  <p>In result I have : </p>
  
  <pre><code>I'm a car
  I'm a sportcar
  I'm a sportcar
  I'm a car
  </code></pre>
  
  <p>And I want :</p>
  
  <pre><code>I'm a car
  I'm a sportcar
  I'm a sportcar
  I'm a sportcar
  </code></pre>
  
  <p>I try to use references like that :</p>
  
  <pre><code>protected:
      Car &amp;m_Car;
      Track m_Track;
  </code></pre>
  
  <p>But it doesn't work either, I have this error I can't fix :</p>
  
  <pre><code>||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|
  E:\documents\c++\test\src\src.cpp||In constructor 'Race::Race(const Car&amp;, Track)':|
  E:\documents\c++\test\src\src.cpp|46|error: uninitialized reference member in 'class Car&amp;' [-fpermissive]|
  include\headers.h|66|note: 'Car&amp; Race::m_Car' should be initialized|
  ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
  </code></pre>
  
  <p>Thank you for your help.</p>