1.

Predict the output?

Answer»

#include&LT;iostream> using NAMESPACE std; class ClassA { public: ClassA(int ii = 0) : i(ii) {} void show() { cout << "i = " << i << ENDL;} private: int i; }; class ClassB { public: ClassB(int xx) : x(xx) {} operator ClassA() const { return ClassA(x); } private: int x; }; void g(ClassA a) { a.show(); } int main() { ClassB b(10); g(b); g(20); getchar(); return 0; }

Output:

i = 10i = 20

Reason:
ClassA contains a conversion constructor. Due to this, the OBJECTS of ClassA can have integer values. So the STATEMENT g(20) works. Also, ClassB has a conversion operator overloaded. So the statement g(b) also works.



Discussion

No Comment Found