#include <iostream>

using namespace std;

class Base{
public:
    int data;
    Base(int din = 3): data(din){}
    virtual int compareTo(const Base& rhs) const;
};

class Derived: public Base{
public:
    int fake;
    Derived(int dataIn = 7, int fakeIn = 17):
	Base(),
	//data(dataIn),
	fake(fakeIn)
    {}
    virtual int compareTo(const Base& rhs) const;
};

/*
 * Returns 0 when equal, positive when this is greater, negative if
 * rhs is greater
 */
int Base::compareTo(const Base& rhs) const {
    cout << "base compare\n";
    return data - rhs.data;
}

int Derived::compareTo(const Base& rhs) const {
    cout << "Derived compare, fake is " << fake <<endl;
    const Derived * rhsP = dynamic_cast<const Derived*>(&rhs);
    if (rhsP)
	cout << "Derived compare, rhs fake is " << 
	    rhsP->fake <<endl;
    else
	cout << "Error, type cast to Derived failed" << endl;
    return data - rhs.data;
}

int main(){

    Base b;
    b.data = 5;
    cout << b.compareTo(b) << endl;

    Derived d;
    d.data = 6;
    
    cout << d.compareTo(d) << endl;

    Base * bp = new Derived;
    cout << bp->compareTo(d) << endl;

    return 0;
}

