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

Hello, my proffesor says this is wrong.... \" Why are you printing on every func

ID: 3772380 • Letter: H

Question

Hello,

my proffesor says this is wrong.... " Why are you printing on every function. This is not right" i am very confused now can someone help me?

This are the instructions:
Create the implementation portion of the complex class described in the complex definition file "complex.h"
Thank you!

complex.cpp

#include <iostream>

#include <cmath>
#include "complex.h"//header file

using namespace std;
complex::complex()

{//constructor without argument present in complex
real=0;
  
imag=0;
cout<<"real="<<real<<", imag="<<imag<<endl;
}
complex::complex(double a)

{//constructor with one argument
  
real=a;
  
imag=a;
cout<<"real="<<real<<"imag"<<imag<<endl;
  
}

complex::complex(double a,double b)

{//constructor with two arguments
  
real=a;
  
imag=b;
cout<<"real="<<real<<","<<"imag="<<imag<<endl;
  
}

complex::complex(int a,int b)

{//constructor with two arguments
  
real=a;
  
imag=b;
cout<<"real="<<real<<","<<"imag="<<imag<<endl;   
}
double complex::getImag() const

{//class complex contains getImage method
  
return imag;
cout<<"imag="<<imag<<endl;
  
}
double complex::getReal() const

{//getReal method present in complex class
  
return real;
  
}
void complex::print() const

{//print method in complex
  
cout<<getReal()<<"+j"<<getImag()<<endl;
  
}


void complex::get(double& a,double& b) const

{//constructor with object arguments
  
a=real;
  
b=imag;
cout<<"a="<<a<<"b="<<b<<endl;
  
}
complex complex:: operator+ (double& a)

{//opeator overloading with object argumnet
  
return complex(a+real, a+imag);
  
  
}
complex complex::operator+ ( complex& c2)

{//operating overloading with constructor as object
  
return complex(real+c2.real+imag+c2.imag);
  
}
complex complex:: operator*= (double& a)

{//operator overloading *
  
real=real*a;
  
imag=imag*a;
  
return *this;
  
}
complex complex:: conj() const

{//conj method in complex
  
return complex(real,-imag);
  
}



double complex::norm() const

{//norm method in complex
  
return (real*real+imag*imag);
  
}
double complex :: modulus() const

{
  
  
cout<<"sqrt(real*real+imag*imag)="<<sqrt(real*real+imag*imag);
  
}
ostream &operator<<(std::ostream &out, complex c)

{
  
out<<"Complex Number is"<<endl;
  
out<<c.real<<"+j"<<c.imag;
  
return out;
  
}

int main(){

complex c;//calling all methods in class complex
complex c1(1.3);
complex c2(1.2,1.4);
complex c3(10,20);
complex c4;
c4.getImag();
complex c5;
c5.getReal();
complex c6;
c6.print();
double x(10);
double y(10);
complex c7;
c7.get(x,y);
double m(10.23);
double n(20.12);
double o=m+n;
cout<<"o="<<o<<endl;
complex c8(10);
complex c9(20);
complex c10=c8+c9;
double f(10.23);
double g(20.12);
double h=f*g;
cout<<"h="<<h<<endl;
complex c11;
c11.conj();
c11.norm();
c11.modulus();
double k(1.2);
complex c12(12);
return 0;
}

complex.h

#ifndef COMPLEX_H

#define COMPLEX_H

class complex {

public:

complex();

complex(double a);

complex(double a,double b);

complex(int a,int b);

void print() const;

void set(double a=0,double b=0);

void set(int a=0,int b=0);

double getReal() const;

double getImag() const;

void get(double&,double&) const;

complex operator+ (double&);

complex operator+ (complex&);

complex operator+= (complex&);

complex operator+= (int&);

complex operator++ (int);

complex operator++ ();

complex operator- (double&);

complex operator- (complex&);

complex operator-= (complex&);

complex operator-= (double&);

complex operator-- (int);

complex operator-- ();

complex operator* (complex&);

complex operator* (double&);

complex operator*= (complex&);

complex operator*= (double&);

complex operator/ (complex&);

complex operator/= (complex&);

complex operator/= (double);

complex operator/ (double);

void operator= (const complex&);

bool operator== (complex&);

bool operator!=(complex &c);

friend std::istream &operator>> (std::istream &in, complex& c);

friend std::ostream &operator<<(std::ostream &out, complex c);

complex conj() const;

double norm() const;

double modulus() const;

private:

double real;

double imag;

};

#endif // COMPLEX_H

Explanation / Answer

Hi, There is nothing wrong with your code infact. The only thing is you are not supposed to print the values in every member function. Instead, as you declared a separate member function called print(), when and where you want to print the value, just call that particular member function, instead of writing the cout code to print in every function. Other than that, everything seems good enough. Even your professor commented the same.

So, just remove the cout statments, in every member functions. Let the complex.cpp code look like this:

#include <iostream>
#include <cmath>
#include "complex.h"//header file

using namespace std;
complex::complex()

{//constructor without argument present in complex
real=0;
  
imag=0;
//cout<<"real="<<real<<", imag="<<imag<<endl;
}
complex::complex(double a)

{//constructor with one argument
  
real=a;
  
imag=a;
//cout<<"real="<<real<<"imag"<<imag<<endl;
  
}

complex::complex(double a,double b)

{//constructor with two arguments
  
real=a;
  
imag=b;
//cout<<"real="<<real<<","<<"imag="<<imag<<endl;
  
}

complex::complex(int a,int b)

{//constructor with two arguments
  
real=a;
  
imag=b;
//cout<<"real="<<real<<","<<"imag="<<imag<<endl;   
}
double complex::getImag() const

{//class complex contains getImage method
  
return imag;
//cout<<"imag="<<imag<<endl;
  
}
double complex::getReal() const

{//getReal method present in complex class
  
return real;
  
}
void complex::print() const

{//print method in complex
  
cout<<getReal()<<"+j"<<getImag()<<endl;   //This is good.
  
}

void complex::get(double& a,double& b) const

{//constructor with object arguments
  
a=real;
  
b=imag;
//cout<<"a="<<a<<"b="<<b<<endl;
  
}
complex complex:: operator+ (double& a)

{//opeator overloading with object argumnet
  
return complex(a+real, a+imag);
  
  
}
complex complex::operator+ ( complex& c2)

{//operating overloading with constructor as object
  
return complex(real+c2.real+imag+c2.imag);
  
}
complex complex:: operator*= (double& a)

{//operator overloading *
  
real=real*a;
  
imag=imag*a;
  
return *this;
  
}
complex complex:: conj() const

{//conj method in complex
  
return complex(real,-imag);
  
}

double complex::norm() const

{//norm method in complex
  
return (real*real+imag*imag);
  
}
double complex :: modulus() const

{
  
//cout<<"sqrt(real*real+imag*imag)="<<sqrt(real*real+imag*imag);
return sqrt(real*real+imag*imag);   //This function returns a double value.
  
}
ostream &operator<<(std::ostream &out, complex c)   //This logic is to write the complex number to a file.

{
  
out<<"Complex Number is"<<endl;
  
out<<c.real<<"+j"<<c.imag;
  
return out;
  
}
int main(){

complex c;//calling all methods in class complex
complex c1(1.3);
c1.print();   //If you want to print.
complex c2(1.2,1.4);
c2.print(); //If you want to print.
complex c3(10,20);
c3.print(); //If you want to print.
complex c4;
cout<<"Imaginary value of c4 is: "<<c4.getImag()<<endl;   //This prints the returned value.  
complex c5;
c5.print();   //If you want to print.
cout<<"Real value of c5 is: "<<c5.getReal()<<endl; //This prints the returned value.
complex c6;
c6.print();
double x(10);
double y(10);
complex c7;
c7.get(x,y);
double m(10.23);
double n(20.12);
double o=m+n;
cout<<"o="<<o<<endl;
complex c8(10);
complex c9(20);
complex c10=c8+c9;
double f(10.23);
double g(20.12);
double h=f*g;
cout<<"h="<<h<<endl;
complex c11;
c11.conj();
c11.norm();
c11.modulus();
double k(1.2);
complex c12(12);
return 0;
}

Note: Instead of removing, I just commented the unnecessary code. If you are ok with the final output, just remove the commented code. If you have any further queries, just get back to me.