S
Soumen
Hi,
I've following code :
class Base {
public:
virtual void func() { std::cout << "Base" << std::endl; }
};
class Derived : public Base {
public:
void func() { std::cout << "Derived" << std::endl; }
};
class MostDerived : public Derived {
public:
void func() { std::cout << "MostDerived" << std::endl; }
};
int main(int argc, char **argv)
{
Base *pBase = 0;
Derived *pDerived = 0;
try {
pBase = new MostDerived;
pDerived = new MostDerived;
if (pBase) {
pBase->func();
delete pBase;
pBase = 0;
}
if (pDerived) {
pDerived->func();
delete pDerived;
pDerived = 0;
}
} catch (...) {
if (pBase) delete pBase;
if (pDerived) delete pDerived;
}
return 0;
}
On execution, why the o/p is
MostDerived
MostDerived
I's expecting it to be
Derived
Derived
as after Derived, it's no longer virtual ... Please clarify me the
reasons ...
I've verified it with g++ and Sun compiler ... same behavior with both
the compiler ...
Regards,
~ Soumen
I've following code :
class Base {
public:
virtual void func() { std::cout << "Base" << std::endl; }
};
class Derived : public Base {
public:
void func() { std::cout << "Derived" << std::endl; }
};
class MostDerived : public Derived {
public:
void func() { std::cout << "MostDerived" << std::endl; }
};
int main(int argc, char **argv)
{
Base *pBase = 0;
Derived *pDerived = 0;
try {
pBase = new MostDerived;
pDerived = new MostDerived;
if (pBase) {
pBase->func();
delete pBase;
pBase = 0;
}
if (pDerived) {
pDerived->func();
delete pDerived;
pDerived = 0;
}
} catch (...) {
if (pBase) delete pBase;
if (pDerived) delete pDerived;
}
return 0;
}
On execution, why the o/p is
MostDerived
MostDerived
I's expecting it to be
Derived
Derived
as after Derived, it's no longer virtual ... Please clarify me the
reasons ...
I've verified it with g++ and Sun compiler ... same behavior with both
the compiler ...
Regards,
~ Soumen