Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

objective: This homework is an advanced introduction to C++ classes with operato

ID: 3855928 • Letter: O

Question

objective: This homework is an advanced introduction to C++ classes with operator overloading. The goal is to gain better knowledge of classes and utilize operator overloading where appropriate. Checklist: 1. Review the assignment specification 2. Review Binary Numbers 3. Please review how to separate your class into an implementation, definition, and driver sections. These will all become separate files as outlined below. 4. Create a binary·cpp (should contain the class implementation only) , binary.h (should contain your class definition, no functions), binary-sandbox.cpp (should contain the main() and #include "binary.h") file within your IDE. 5. Develop a Binary class definition. Do not bother at first to create the individual functions. You can do that later. Have it checked by your instructor. 6. Use the binary-sandbox.cpp to test your Class. 7. Create and test each operator one at a time. The easiest operators for this assignment may be to start with the "cin" and "cout" operators. 8. Verify sample output by providing corresponding sample hand calculations. 9. Make sure that your code complies with the C++ coding standards. 10.Submit the binary.cpp, binary-sandbox.cpp, and binary.h Specification

Explanation / Answer

In C++, the programmer abstracts real world objects using classes as concrete types. Sometimes it is required to convert one concrete type to another concrete type or primitive type implicitly. Conversion operators play smart role in such situations.

For example consider the following class

#include <iostream>

#include <cmath>

using namespace std;

class Complex

{

private:

    double real;

    double imag;

public:

    // Default constructor

    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)

    {}

    // magnitude : usual function style

    double mag()

    {

        return getMag();

    }

    // magnitude : conversion operator

    operator double ()

    {

        return getMag();

    }

private:

    // class helper to get magnitude

    double getMag()

    {

        return sqrt(real * real + imag * imag);

    }

};

int main()

{

    // a Complex object

    Complex com(3.0, 4.0);

    // print magnitude

    cout << com.mag() << endl;

    // same can be done like this

    cout << com << endl;

}

We are printing the magnitude of Complex object in two different ways.

#include <iostream>

#include <cmath>

using namespace std;

class Complex

{

private:

    double real;

    double imag;

public:

    // Default constructor

    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)

    {}

    // magnitude : usual function style

    double mag()

    {

        return getMag();

    }

    // magnitude : conversion operator

    operator double ()

    {

        return getMag();

    }

private:

    // class helper to get magnitude

    double getMag()

    {

        return sqrt(real * real + imag * imag);

    }

};

int main()

{

    // a Complex object

    Complex com(3.0, 4.0);

    // print magnitude

    cout << com.mag() << endl;

    // same can be done like this

    cout << com << endl;

}