#include<iostream>
#include<conio.h>
using namespace std;
class base{
public:
base() {
cout<<"base constructor"<<endl;
getch();
}
virtual void out()
{
cout<<"I AM BASE"<<endl;
getch();
}
};
class derived : public base
{
public:
derived(){
cout<<"derived constructor"<<endl;
getch();
}
void out(){
cout<<"I am derived"<<endl;
getch();
}
};
int main(){
base b,*bp;
derived d;
bp=&b;
bp->base::out();//works fine
bp=&d;
bp->out();//prints i am derived whereas it should print i am base
//bp->derived::out();//error 'derived is not a base of 'base'
return 0;
}
when the base pointer points to derived object it prints i am derived.
I am using Dev c++.