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

An Override That Calls the Base Version

hardInheritance

📄 Code

Read carefully — what does this print?
1#include <iostream>
2#include <string>
3using namespace std;
4 
5class Employee {
6protected:
7 string m_name;
8 double m_base;
9public:
10 Employee(const string& name, double base) : m_name(name), m_base(base) {
11 cout << "Employee " << m_name << endl;
12 }
13 virtual double pay() const { return m_base; }
14 virtual void show() const {
15 cout << m_name << " earns " << pay() << endl;
16 }
17 virtual ~Employee() {}
18};
19 
20class Manager : public Employee {
21 double m_bonus;
22public:
23 Manager(const string& name, double base, double bonus)
24 : Employee(name, base), m_bonus(bonus) {
25 cout << "Manager " << m_name << endl;
26 }
27 double pay() const override {
28 return Employee::pay() + m_bonus;
29 }
30};
31 
32int main() {
33 Manager m("Ana", 1000, 250);
34 m.show();
35 Employee& e = m;
36 e.show();
37 cout << e.Employee::pay() << endl;
38 return 0;
39}

🎯 Your Answer