Q:

What is the conversion operator in C++?

0

What is the conversion operator in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

A class can have a public method for specific data type conversions. It means you can define a member function of a class that converts from the type of its class to another specified type. It is called a conversion function, See the below example,

#include <iostream>
using namespace std;
class Demo
{
    double value;
public:
    Demo(double data )
    {
        value = data;
    }
    operator double()
    {
        return value;
    }
};
int main()
{
    Demo BooObject(3.4);
    /*assigning object to variable mydata of type double.
    Now conversion operator gets called to assign the value.*/
    double mydata = BooObject;
    cout << mydata <<endl;
}

Output: 3.4

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions For Experienced

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
When do we need to write a user-defined destructor... >>
<< What is the difference between a copy constructor ...