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:
    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
void ABBase::f(char ch) const function overrides all f() functions in the base classes, so it will be called with the corresponding type conversions: double to char with loss of information (call ab.f(2.5)), int to char with loss of information (call ab.f(4)). In these cases, the compiler will issue warnings about the possibility of data loss.

Follow CodeGalaxy

Mobile Beta

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