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: 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.
C++
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:
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.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images