Predict the output
#include<iostream> using namespace std; class A { int i; public: A(int ii = 0) : i(ii) {} void show() { cout << i << endl; } }; class B { int x; public: B(int xx) : x(xx) {} operator A() const { return A(x); } }; void g(A a) { a.show(); } int main() { B b(10); g(b); g(20); return 0; } |
chevron_right
filter_none
(A) Compiler Error
(B)
10 20
(C)
20 20
(D)
10 10
Answer: (B)
Explanation: Note that the class B has as conversion operator overloaded, so an object of B can be converted to that of A.
Also, class A has a constructor which can be called with single integer argument, so an int can be converted to A.
Quiz of this Question
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 2
- C++ | Operator Overloading | Question 3
- C++ | Operator Overloading | Question 4
- C++ | Operator Overloading | Question 5
- C++ | Operator Overloading | Question 7
- C++ | Operator Overloading | Question 10
- C++ | Operator Overloading | Question 9
- C++ | Operator Overloading | Question 10
- C++ Program to concatenate two strings using Operator Overloading
- C++ program to compare two Strings using Operator Overloading
- Matrix operations using operator overloading
- Count number of Unique Triangles using Operator overloading
- C++ | Function Overloading and Default Arguments | Question 5
- C++ | Function Overloading and Default Arguments | Question 2
- C++ | Function Overloading and Default Arguments | Question 3
- C++ | Function Overloading and Default Arguments | Question 4
- C++ | Function Overloading and Default Arguments | Question 5
- Does C support function overloading?
- Function overloading and const keyword

