As an example:
Shouldn't the second d.print() call during upcasting print "base"?
Isn't it a "d" derived object that has been upcasted to a base class object?
And what advantages does downcasting have?
Could you describe upcast and downcast in more detail?
#include <iostream>
using namespace std;
class Base {
public:
void print() { cout << "base" << endl; }
};
class Derived :public Base{
public:
void print() { cout << "derived" << endl; }
};
void main()
{
// Upcasting
Base *pBase;
Derived d;
d.print();
pBase = &d;
d.print();
// Downcasting
Derived *pDerived;
Base *b;
pDerived = (Derived*)b;
}