Home » C++ Programming » Operators » Question
  1. What is the output of this program?
    #include <iostream>
    #include <cmath>
    using namespace std;
    class ComplexNumber
    {
    private:
    double RealNumber;
    double ImagNumber;
    public:
    ComplexNumber(double r = 0.0, double i = 0.0) : RealNumber(r), ImagNumber(i)
    {}
    double mag()
    {
    return getMag();
    }
    operator double ()
    {
    return getMag();
    }
    private:
    double getMag()
    {
    return sqrt(RealNumber * RealNumber + ImagNumber * ImagNumber);
    }
    };
    int main()
    {
    ComplexNumber comp(2.0, 5.0);
    cout << comp.mag()<<" ";
    cout << comp;
    return 0;
    }
    1. 2.0
    2. 5.0
    3. 5.38516
    4. 5.38516 5.38516
    5. None of these
Correct Option: D

In this program, we are calculating the magnitude value by two ways.



Your comments will be displayed only after manual approval.