C++: Derived virtual function is returning strange results
<p>I created a derivable class with a virtual function in a .HPP file, and then gave it a default return value in the class's .CPP file. Next, I created a class which inherited the latter derivable class and overloaded it's virtual function, giving it a new return value. However, the return value is not always the same (either the default return value or the overloaded return value). Can someone help me fix my code or find the issue. Thanks.</p>

<p>Note: I am providing example code, which I believe will suffice in displaying the problem.</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;sstream&gt;

using std::cout;
using std::ostream;

class Fruit;
class Apple;

class Fruit
{
    public:
        int Type;

        Fruit();
        ~Fruit();

        Fruit(int = 0);

        virtual const int getVal() const;
};

class Apple : public Fruit
{
    public:
        Apple();
        ~Apple();

        const int getVal() const;
};


Fruit::Fruit() : Type(0) {}
Fruit::~Fruit() {}

Fruit::Fruit(int type) : Type(type) {}

//const int Fruit::getVal() const { return 0; } //Uncommenting this results in function //always returning ZERO; even in Apple::getVal().
const int Fruit::getVal() const { return Type; }

Apple::Apple() : Fruit(1) {}
Apple::~Apple() {}

const int Apple::getVal() const { return Type; }

ostream& operator&lt;&lt;(ostream& a, Fruit b)
{
    return a &lt;&lt; b.getVal();
}

int main(int *argc, char **argv)
{
    cout &lt;&lt; Apple() &lt;&lt; "\n\n";

    #ifdef _WIN32
        system("pause");
    #endif

    return 0;
}</code></pre>
