What will be printed out as a result of the following code execution?
#include <iostream>
using namespace std;

class ABase {
public:
    void f(int i) const { cout << 1;}
    void f(char ch) const { cout << 2; }
};

class BBase {
public:
    void f(double d) const { cout << 3;}
};

class ABBase : public ABase, public BBase {
public:
    using ABase::f;
    using BBase::f;        
    void f(char ch) const { cout << 4; }
};

void g(ABBase& ab) {
    ab.f('c');
    ab.f(2.5);
    ab.f(4);
}

int main() {
    ABBase ab;
    g(ab);
}
Explanation
using ABase::f and using BBase::f declarations protect the void ABase::f(int i) const and void BBase::f(double d) const functions from hiding and allow you to create a set of overloaded functions from base and derived classes. The function void ABase::f(char ch) const is hidden by the function void ABBase::f(char ch) const. Therefore:
ab.f('c') - calling ABBase::f(char) { cout << 4;}
ab.f(2.5) - calling BBase::f(double) {cout << 3;}
ab.f(4) - calling ABase::f(int) {cout << 1;}

Follow CodeGalaxy

Mobile Beta

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