C++ overloading and picking right function
<p>C++ Overloading of methods question</p>

<p>I have 1 parent class call Vehicle</p>

<p>I have 2 child class call Motorcycle and Car</p>

<p>I have this value call getNoOfWheels();</p>

<p>Parent class got that method, motorcycle and car also got.</p>

<p>Lets say i prompt user for input</p>

<pre><code>string vehicleType;
cout &lt;&lt; "What is your vehicle type" &lt;&lt; endl;
cin &gt;&gt; vehicleType;
</code></pre>

<p>base on user input, how do i make the program pick the right function base on vehicleType , i know i can use if VehicleType== , but thats defeat the purpose of overloading.</p>

<p>was given suggestion on using virtual method earlier on. in such case</p>

<pre><code>virtual int noOfVerticles() const { return 0; }
</code></pre>

<p>For shape.h</p>

<p>I have same function for Car and motorcycle, but how do i make the noOfVerticles pick the right function from the child class base on vehicleType</p>

<p>I tried something like this..</p>

<pre><code>Vehicle cVehicle;
Car &amp;rCar = &amp;cVehicle;


if(inVehicle=="Car")
{
cout &lt;&lt; rCar.noOfWheels() &lt;&lt; endl;
}
</code></pre>

<p>I get an error that say..</p>

<pre><code>invalid initizliation of non-const refenrece of type "Car&amp;" from an rvaleu of type Vehicle*
</code></pre>

<p>and ...</p>

<p>this is my virtual function at Car.cpp</p>

<pre><code>public:
virtual int noOfWheels() const { return 4; }
</code></pre>

<p><strong>Thanks.!</strong></p>
