Design and implement a C++ program that defines a base class A, which has a subc
ID: 3878180 • Letter: D
Question
Design and implement a C++ program that defines a base class A, which has a subclass B, which itself has a subclass C. The A class must implement a method, m, which is overridden in both B and C. You must also write a test class that instantiates objects from A, B, and C and includes three calls to m. One of the calls must be statically bound to A’s method. One call must be dynamically bound to B’s method, and one must be dynamically bound to C’s method. All of the method calls must be through a pointer to class A.
Explanation / Answer
#include <iostream>
using namespace std;
class A
{
public:
virtual void m(){
std::cout << "method calling from class A" << std::endl;
}
A(){
m();
}
};
// B class is derived from base class A.
class B : public A
{
public:
void m(){
std::cout << "method calling from class B" << std::endl;
}
};
// C class is derived from base class B.
class C : public B
{
public:
void m() {
std::cout << "method calling from class C" << std::endl;
}
};
int main()
{
A *a;
B b;
a=&b;
a->m();
C c;
a=&c;
a->m();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.