Ask Question
10 May, 08:35

What is printed by the following program provided all necessary standard header files are included? Explain each line of the output by stating which member function generates the result and why to justify your answer.#include iostream> using namespace std; class A { public: virtual void f1 () cout<< ("fl in A/n") voidf2 () { cout << "f2 in A/n"; } }class B: public A {public: void f1 () cout <<"fl in B/n"; void f2 () cout <<"f2 in B/n }void g (A &x) { x. f1 (); x. f2 (); int main () {A; aB:bg (a); g (b); a. f1 (); a. f2 (); b. f1 (); b. f2 (); }

+3
Answers (1)
  1. 10 May, 09:48
    0
    Output:

    f1 in A

    f2 in A

    f1 in B

    f2 in A

    f1 in A

    f2 in A

    f1 in B

    f2 in B

    Explanation:

    In this snippet, the code makes use of virtual functions. A virtual function is defined as a function that is defined in the base class and redefined in the derived class. If the derived function accesses the virtual function, the program will get executed with the derived class's version of the function.

    In this code, we define the virtual function f1 () in class A and also redefine it in class B which is the derived class of A. While executing the program, the function g which takes the object b (class B's object) as a parameter. It will print class B's version of f1 () rather than class A's version. This is working off the virtual function.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “What is printed by the following program provided all necessary standard header files are included? Explain each line of the output by ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers