Question discussion

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

1313Medium DSSSB TGT Computer Science 0 replies 1 views
0

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

#include <iostream>
using namespace std;
class P { public: void f() { cout << "P"; } virtual void g() { cout << "P"; } };
class C : public P { public: void f() { cout << "C"; } void g() { cout << "C"; } };
int main() { P *p = new C; p->f(); p->g(); }
  • APC
  • BPP
  • CCP
  • DCC
Correct answer: A

Explanation

f() is not virtual, so it is bound to the pointer's static type P and prints P; g() is virtual and dispatches to C::g, printing C.

Discussion 0

Nobody has replied yet. Start the discussion!