What will the program print out?
#include <iostream>
using namespace std;

class a {
    virtual void f(int) { cout<<"a int"; }

class b : public a {
    class a; friend
    void f(double) { cout<<"b double"; }
    void f(int) { cout<<"b int"; }

int main (void) {
  b* o = new b;
  return 0;
In class b, the friend directive refers to f(double), i.e. it is a friendly function (not a class member), although defined in the class.
In main() there is a call to o->f(), i.e. the function that is the member of the class b, i.e. the function b::f(int).

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes