Question discussion

What is the output of the following C++ program?#include using namespace std; class Base { public: virtual void show() { cout

983Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following C++ program?

#include <iostream>
using namespace std;
class Base { public:
    virtual void show() { cout << "B"; }
    void hi() { cout << "b"; } };
class Der : public Base { public:
    void show() { cout << "D"; }
    void hi() { cout << "d"; } };
int main() { Base *p = new Der(); p->show(); p->hi(); }
  • ABd
  • BBb
  • CDb
  • DDd
Correct answer: C

Explanation

show() is virtual so the derived version runs (D); hi() is non-virtual so it is bound statically to Base (b).

Discussion 0

Nobody has replied yet. Start the discussion!