S
Subra
In a interview I was asked how to make the below code work ( without
changing the base class ) ?
#include <iostream>
#include <string>
using namespace std;
class base
{
public:
virtual void m1() { cout <<"m1 " << endl ; }
};
class der : public base
{
public:
virtual void m2() { cout <<"m2 " << endl ; } //m2 is not there in base
class
};
int main() {
base* ptr;
der obj;
ptr = &obj;
ptr->m2(); // base ptr is trying to call m2 , and hence the code
cant compile
}
changing the base class ) ?
#include <iostream>
#include <string>
using namespace std;
class base
{
public:
virtual void m1() { cout <<"m1 " << endl ; }
};
class der : public base
{
public:
virtual void m2() { cout <<"m2 " << endl ; } //m2 is not there in base
class
};
int main() {
base* ptr;
der obj;
ptr = &obj;
ptr->m2(); // base ptr is trying to call m2 , and hence the code
cant compile
}