S
subramanian
Consider the program:
#include <iostream>
#include <string>
class A {
public:
virtual void print(void) const;
};
void A:rint(void) const
{
std::cout << "A:rint()\n";
return;
}
class B : public A {
public:
void print(void) const;
};
void B:rint(void) const
{
std::cout << "B:rint()\n";
return;
}
class C : public B {
public:
void print(void) const;
};
void C:rint(void) const
{
std::cout << "C:rint()\n";
return;
}
int main(void)
{
C c;
A *pa = &c;
pa->print();
return 0;
}
If I run this program the output is:
C:rint()
Question:
A:rint() is declared virtual. Since B:rint()const is not virtual,
how does pa->print() calls C:rint() ?
#include <iostream>
#include <string>
class A {
public:
virtual void print(void) const;
};
void A:rint(void) const
{
std::cout << "A:rint()\n";
return;
}
class B : public A {
public:
void print(void) const;
};
void B:rint(void) const
{
std::cout << "B:rint()\n";
return;
}
class C : public B {
public:
void print(void) const;
};
void C:rint(void) const
{
std::cout << "C:rint()\n";
return;
}
int main(void)
{
C c;
A *pa = &c;
pa->print();
return 0;
}
If I run this program the output is:
C:rint()
Question:
A:rint() is declared virtual. Since B:rint()const is not virtual,
how does pa->print() calls C:rint() ?