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

Trying to see where I went wrong and can\'t find it I know there is probably pro

ID: 3537357 • Letter: T

Question

Trying to see where I went wrong and can't find it I know there is probably problems with my constructors but I can't find them.

Looking for help.

code is as follows


ComplexType.h



#ifndef COMPLEXTYPE_H_
#define COMPLEXTYPE_H_

#include

class ComplexType // Concrete class, not designed to be a parent class in a class hierarchy
{
public:

    ComplexType();
    ComplexType( double va, double vb );
    ComplexType( double va );
    ComplexType( const ComplexType &cd ); // Default copy constructor is OK but we want to implement it anyway

    double getReal        () const;
    double getImaginary    () const;

    void setReal        ( double va );
    void setImaginary    ( double vb );

    // ~ComplexType(); // Default destructor is OK, this is a concrete class so the virtual destructor is not needed

    const ComplexType& operator=(const ComplexType& rightObject);

    //ComplexType add       (const ComplexType& cd) const; //bad style as member function
    //ComplexType operator+ (const ComplexType& cd) const; //bad style
    //bool operator==(const ComplexType& cd) const;        //bad style as member function

    // Using friend functions instead of member functions so implicit argument conversions are allowed
    // for example, c+5.0 where c is a complex number

    friend ComplexType operator+ (const ComplexType& ab,
                                  const ComplexType& cd);
    friend bool operator== (const ComplexType& ab,
                            const ComplexType& cd);
    // ToDo   operator!= declaration                       
    friend bool operator!= (const ComplexType& ab,
                            const ComplexType& cd);

    // ToDo   operator*, operator-,operator/ declarations               
    // ToDo   operator~, operator! declarations                       
    friend ComplexType operator* (const ComplexType& ab, const ComplexType& cd);
                               
    friend ComplexType operator- (const ComplexType& ab, const ComplexType& cd);
                               
    friend ComplexType operator/ (const ComplexType& ab,
                                const ComplexType& cd);
   
    friend ComplexType operator~ (const ComplexType& cd);
                               
    friend ComplexType operator! (const ComplexType& cd);
                               

    friend std::istream& operator>> (std::istream& isObject, ComplexType& cd);
    friend std::ostream& operator<< (std::ostream& osObject, const ComplexType& cd);

private:

    // a+bi

    double a;    // real part
    double b;    // imaginary part
};

#endif /* COMPLEXTYPE_H_ */




ComplexType.cpp



#include "ComplexType.h"
#include
#include
#include
#include

using namespace std;

ComplexType::ComplexType() : a(0.0), b(0.0) {}

ComplexType::ComplexType( double va, double vb ) : a(va), b(vb) {}

// ToDo ComplexType( double va )
ComplexType::ComplexType(double va)
{
    a = va;
}
// ToDo ComplexType( const ComplexType &cd ); // Default copy constructor is OK but we want to implement it anyway
ComplexType::ComplexType(const ComplexType& cd)
    {
    a = cd.a;
    }
// ComplexType::~ComplexType() {} // Default destructot is OK

istream& operator>> (istream& isObject, ComplexType& cd)
{
    char pl, pr, comma;

    isObject >> pl >> cd.a >> comma >> cd.b >> pr;

    if ( pl != '(' || pr != ')' || comma != ',' )
        throw runtime_error("Invalid complex number format");

    return isObject;
}

ostream& operator<< (ostream& osObject, const ComplexType& cd)
{
    return osObject << '(' << cd.a << ',' << cd.b << ')';
}

//friend function
ComplexType operator+ (const ComplexType& ab, const ComplexType& cd)
{
    ComplexType temp( ab.a+cd.a, ab.b+cd.b );
    return temp;
}

const ComplexType& ComplexType::operator= (const ComplexType& rightObject)
{
    a = rightObject.a;
    b = rightObject.b;
    return *this;// this is a pointer to the object
    //*this means the object that this points to
    //ComplexType& is reference to some other object of ComplexType
}

//operator= cannot be friend, it must be member function
//arithmetic operators better friend than member because of typo conversion,
//for example 5+6.1 -> type conversion 5.0+6.1, in case of friend with both
//operands can be done type conversion, in case of member function, type
//conversion can be done only for second operand.

/* member function
ComplexType ComplexType::operator+ (const ComplexType& cd) const
{
    ComplexType temp;
    temp.a = a+cd.a;
    temp.b = b+cd.b;
    return temp;
}
*/

// ToDo   operator== definition
// ToDo   operator!= definition that calls operator==

// ToDo   operator*, operator-,operator/ definitions
// ToDo   operator~, operator! definitions
bool operator== (const ComplexType& ab, const ComplexType& cd)
    {
    return (ab.a == cd.a && ab.b == cd.b);
    }

bool operator!= (const ComplexType& ab, const ComplexType& cd)
    {
    return(ab.a != cd.a && ab.b != cd.b);
    }
   
ComplexType operator* (const ComplexType& ab, const ComplexType& cd)
    {
    ComplexType temp;
    temp.a = (ab.a * cd.a) - (ab.b * cd.b);
    temp.b = (ab.a * cd.b) + (ab.b * cd.a);
    return temp;
    }
   
ComplexType operator- (const ComplexType& ab, const ComplexType& cd)
    {
    ComplexType temp;
    temp.a = (ab.a - cd.a);
    temp.b = (ab.b - cd.b);
    return temp;
    }
   
ComplexType operator/ (const ComplexType& ab,
                                const ComplexType& cd)
    {
    ComplexType temp;
    temp.a = (ab.a * cd.a + ab.b * cd.b) / ((cd.a * cd.a) + (cd.b * cd.b));
    temp.b = (ab.b * cd.a - ab.a * cd.b) / ((cd.a * cd.a) + (cd.b * cd.b));
    return temp;
    }
   
ComplexType operator~ (const ComplexType& cd)
    {
    ComplexType temp;
    temp.a = (cd.a);
    temp.b = (cd.b * (-1));
    return temp;
    }

ComplexType operator! (const ComplexType& cd)
    {
    double abs;
    ComplexType temp;
    temp.a = (cd.a*cd.a);
    temp.b = (cd.b*cd.b);
    abs = sqrt(temp.a + temp.b);
    return abs;
    }
   



DriverComplexType.h



#include
#include "ComplexType.h"

using namespace std;

int main ()
{   
    ComplexType ab;
    // ToDo Set floating numbers output format and precision
    cout << "ab = " << ab << ", ab+ab = "<<(ab+ab) endl="" br="">
    ComplexType cd;

//    cout << "Enter a complex number in (a,b) format: ";
//    cin >> cd;
//    cout << "Entered " << cd << endl;
    // ToDo test operator== cout << ab << " == " << cd << " is " << ( ab == cd ? "true" : "false" ) << endl;
    // ToDo test operator!=

    //ToDo test all friend functions
}




Explanation / Answer

Simple Comple numbers adding Program


#include<iostream>

#include<conio.h>


using namespace std;


class complex

{

private:

double re,im;

public:

complex()

{

}

complex(double r,double i)

{

re=r;

im=i;

};

complex operator+(complex c);

void show();

};

complex complex::operator+(complex c)

{

complex a;

a.re=re+c.re;

a.im=im+c.im;

return a;

}

void complex:: show()

{cout<<"the addition is"<<re<<"+i"<<im;

}

int main()

{

complex x(2,3);

complex y(3,4);

complex z=x+y;

z.show();

getch();

}