Question: Consider the following code: class A { public: virtual void printChar() { cout < < 'A' < < endl; } }; class B : public
Consider the following code:
class A
{
public:
virtual void printChar()
{ cout << 'A' << endl; }
};
class B : public A
{
public:
void printChar()
{ cout << 'B' << endl; }
};
int main()
{
A a;
B b;
A *ptr;
ptr = &a;
ptr->printChar();
ptr = &b;
ptr->printChar();
}
Choose the best statement below:
a.) This code compiles, but has poor style. You shouldn't have a function printChar() defined in both classes.
b.) This code compiles, and there is an example of polymorphism in the code.
c.) This code compiles, and there is no example of polymorphism in the code.
d.) This code does not compile.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
