I am having some difficulty finishing the last section of an assignment. I need
ID: 3534459 • Letter: I
Question
I am having some difficulty finishing the last section of an assignment. I need to add new operators ~ and ! to the code I have below. ~ should return a conjugate of a complex number and ! should return the absolute value.
For complex number (a,b), conjugate is (a, -b)
For complex number (a,b), absolute value is sqrt(a*a + b*b)
//Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public:
Complex (double = 0.0, double = 0.0);
Complex operator +(const Complex &) const;
Complex operator -(const Complex &) const;
void print() const;
private:
double real;
double imaginary;
};
//Complex.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
//Constructor
Complex::Complex(double realPart, double imaginaryPart)
:real(realPart),
imaginary(imaginaryPart)
{
//empty body
}//end Complex constructor
//Addition operator
Complex Complex::operator +(const Complex &operand2) const
{
return Complex(real + operand2.real,
imaginary + operand2.imaginary);
}//end function operator +
//subtraction operator
Complex Complex::operator -(const Complex &operand2) const
{
return Complex(real - operand2.real,
imaginary - operand2.imaginary);
}//end function operator -
//Display a complex object in the form (a, b)
void Complex::print() const
{
cout << '(' << real << ", " << imaginary << ')';
}//End function print
Complex_test_file.cpp
#include <iostream>
#include "Complex.h"
using namespace std;
int main() {
Complex x;
Complex y(4.3, 8.2);
Complex z(3.3, 1.1);
cout << "x: ";
x.print();
cout << " y: ";
y.print();
cout << " z: ";
z.print();
x = y + z;
cout << " x = y + z:" << endl;
x.print();
cout << " = ";
y.print();
cout << " + ";
z.print();
x = y - z;
cout << " x = y - z:" << endl;
x.print();
cout << " = ";
y.print();
cout << " - ";
z.print();
cout << endl;
//Pause the console so user can read the output
cin.ignore (numeric_limits <streamsize>::max(), ' ');
cin.get();
return 0;
}
Thanks for any help in advance.
Explanation / Answer
#include <iostream>
#include "Complex.h"
using namespace std;
class Complex
{
public:
Complex ()
{
real=0.0;
imaginary=0.0;
}
Complex (doble a,double b)
{
real=a;
imaginary=b;
}
void print()
{
cout<<"real ="<<real<<" imaginary "<<imaginary;
}
complex add(complex a,complex b)
{
complex c;
c.real=a.real+b.real;
c.imaginary=a.imaginary+b.imaginary;
return c;
}
complex sub(complex a,complex b)
{
complex c;
c.real=a.real-b.real;
c.imaginary=a.imaginary-b.imaginary;
return c;
}
void conju( )
{
imaginary=imaginary;
}
double absoulte()
{
double b;
b=sqrt(real*real+imaginary*imaginary);
return b;
}
private:
double real;
double imaginary;
};
int main() {
Complex x;
Complex y(4.3, 8.2);
Complex z(3.3, 1.1);
cout << "x: ";
x.print();
cout << " y: ";
y.print();
cout << " z: ";
z.print();
x =x.add(y,z);
cout << " x = y + z:" << endl;
x.print();
cout << " = ";
y.print();
cout << " + ";
z.print();
x =x.sub(y,z);
cout << " x = y - z:" << endl;
x.print();
cout << " = ";
y.print();
cout << " - ";
z.print();
cout << endl;
x=x.conju(x); // two function added......
cout<<"conjugate is "<<a.real<< a.imaginary;
double a;
a=y.absoulte();
cout<<"absoult value is "<<a;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.