HELP ME CODE COMPLEX.CC IN istream& operator>>(istream &lhs,Complex& rhs)PLEASE complex.cc #include #include "complex.h" using namespace std; //Class definition file for Complex //YOU: Fill in all of these functions //There are stubs (fake functions) in there now so that it will compile //The stubs should be removed and replaced with your own c Dr // default Complex::Complex() { real = 0; imagine = 0; } Complex::Complex(int new_real, Imaginary new_imagine) { real = new_real; imagine= new_imagine; } // + operator overload Complex Complex::operator+(const Complex &rhs) const { Complex num; num.real = real + rhs.real; num.imagine = imagine + rhs.imagine; return num; } // - operator overload Complex Complex::operator-(const Complex &rhs) const { Complex num; num.real = real - rhs.real; num.imagine = imagine - rhs.imagine; return num; }
HELP ME CODE COMPLEX.CC IN istream& operator>>(istream &lhs,Complex& rhs)PLEASE
complex.cc
#include <iostream>
#include "complex.h"
using namespace std;
//Class definition file for Complex
//YOU: Fill in all of these functions
//There are stubs (fake functions) in there now so that it will compile
//The stubs should be removed and replaced with your own c Dr
// default
Complex::Complex() {
real = 0;
imagine = 0;
}
Complex::Complex(int new_real, Imaginary new_imagine) {
real = new_real;
imagine= new_imagine;
}
// + operator overload
Complex Complex::operator+(const Complex &rhs) const {
Complex num;
num.real = real + rhs.real;
num.imagine = imagine + rhs.imagine;
return num;
}
// - operator overload
Complex Complex::operator-(const Complex &rhs) const {
Complex num;
num.real = real - rhs.real;
num.imagine = imagine - rhs.imagine;
return num;
}
// * operator overload
Complex Complex::operator*(const Complex &rhs) const {
Complex num;
num.real = real * rhs.real;
num.imagine = imagine * rhs.imagine;
return num;
}
// = operator overload
bool Complex::operator==(const Complex &rhs) const {
if (real == rhs.real && imagine == rhs.imagine) {
return true;
}
else{
return false;
}
}
Complex Complex::cmul(Complex c1, Complex c2)
{
Complex c;
//for real part
c.real=( c1.real *c2.real) - (c1.imagine.get_coeff() * c2.imagine.get_coeff());
// for imaginary part
c.imagine= (c1.imagine.get_coeff() *c2.real) + (c2.imagine.get_coeff()* c1.real);
return c;
}
Complex Complex::operator^(const int &exponent) const {
//return Complex(0,0);
int product=1;
// multiplying the real part with itself exponent number of times
for(int i=0; i<exponent; i++)
{
product *= real;
}
// returning complex number
return Complex(product, imagine);
}
//This function should output 3+5i for Complex(3,5), etc.
ostream& operator<<(ostream &lhs,const Complex& rhs) {
//Output a Complex here
lhs <<rhs.real << rhs.imagine;
return lhs;
}
//This function should read in two ints, and construct a
// new Complex with those two ints
istream& operator>>(istream &lhs,Complex& rhs) {
//Read in a Complex here
HElP ME CODE HERE c++
}
complex.h
#pragma once
#include <iostream>
#include "imaginary.h"
#include <complex>
using namespace std;
class Complex {
private:
int real;
Imaginary imagine;
public:
//YOU: Implement all these functions
Complex(); //Default constructor
Complex(int new_real, Imaginary new_imagine); //Two parameter constructor
Complex operator+(const Complex &rhs) const;
Complex operator-(const Complex &rhs) const;
Complex operator*(const Complex &rhs) const;
bool operator==(const Complex &rhs) const;
Complex cmul(Complex c1, Complex c2);
Complex operator^(const int &exponent) const;
friend ostream& operator<<(ostream &lhs,const Complex& rhs);
friend istream& operator>>(istream &lhs,Complex& rhs);
};
imaginary.h
#pragma once
#include <iostream>
using namespace std;
class Imaginary {
private:
int coeff; //If 5, then means 5i
public:
Imaginary();
Imaginary(int new_coeff);
int get_coeff() const;
Imaginary operator+(const Imaginary& rhs) const; //This is a "constant method"
Imaginary operator-(const Imaginary& rhs) const;
int operator*(const Imaginary& rhs) const;
Imaginary operator*(const int& rhs) const;
Imaginary operator=(const int& rhs);
Imaginary operator=(const Imaginary& rhs);
bool operator==(const Imaginary& rhs) const;
friend ostream& operator<<(ostream& lhs, const Imaginary& rhs);
friend istream& operator>>(istream& lhs, Imaginary& rhs);
};
Trending now
This is a popular solution!
Step by step
Solved in 3 steps