CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List
C++ Code Walkthrough #74

Overrides Across Two Levels

hardInheritance

📄 Code

Read carefully — what does this print?
1#include <iostream>
2using namespace std;
3 
4class A {
5public:
6 virtual void f() const { cout << "A::f" << endl; }
7 virtual void g() const { cout << "A::g" << endl; }
8 void h() const { cout << "A::h" << endl; }
9 virtual ~A() {}
10};
11 
12class B : public A {
13public:
14 void f() const override { cout << "B::f" << endl; }
15};
16 
17class C : public B {
18public:
19 void g() const override { cout << "C::g" << endl; }
20 void h() const { cout << "C::h" << endl; }
21};
22 
23int main() {
24 C c;
25 A* a = &c;
26 B* b = &c;
27 
28 a->f(); a->g(); a->h();
29 b->f(); b->g(); b->h();
30 c.f(); c.g(); c.h();
31 return 0;
32}

🎯 Your Answer