Open In App
Related Articles

C++ | Operator Overloading | Question 6

Improve Article
Improve
Save Article
Save
Like Article
Like

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;
}


(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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads