Recent Posts
printf("ho_tari\n");
Complex3 (여러연산자) 본문
<main.cpp>
#include <iostream>
#include "complex.h"
int main()
{
Complex c1; // (0.0, 0.0i)
//Complex c2(3.0); // (3.0, 0.0i)
Complex c2 = 3.0;
Complex c3(3.0, 4.0); // (3.0, 4.0i)
Complex c4 = c3; // Complex c4(c3);
//Complex c4; // default constructor
//c4 = c3; // assignment.
//c1.real(c3.real());
//c1.imag(c3.imag());
c1 = c3; // c1.operator = (c3) or operator = (c1, c3)
// c1 = c2 = c3; // daisy-chain
c1 += c2;
c1 > c2;
c1 != c2;
if (c1 == c3)
{
std::cout << "c1 and c3 are equal" << std::endl;
}
else
{
std::cout << "c1 and c3 are not equal" << std::endl;
}
c1 = c2 + c3;
c1 = c2 - c3;
//c1 = c2 * c3;
//c1 = c2 / c3;
//std::cout << c1; // std::cout.operator<<(c1) or operator<<(std::cout, c1)
//std::cout << "c1 : (" << c1.real() << ", " << c1.imag() << "i)" << std::endl;
//std::cout << "c2 : (" << c2.real() << ", " << c2.imag() << "i)" << std::endl;
//std::cout << "c3 : (" << c3.real() << ", " << c3.imag() << "i)" << std::endl;
//std::cout << "c4 : (" << c4.real() << ", " << c4.imag() << "i)" << std::endl;
std::cout << "c1 : " << c1 << std::endl;
std::cout << "c2 : " << c2 << std::endl;
std::cout << "c3 : " << c3 << std::endl;
std::cout << "c4 : " << c4 << std::endl;
return 0;
}
<complex.h>
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
class Complex {
friend std::ostream& operator<< (std::ostream& out, const Complex& rhs);
private:
double re;
double im;
public:
//Complex(); // default constructor
//Complex(double re); // convert constructor
Complex(double re = 0.0, double im = 0.0); // default argument
Complex(const Complex &rhs); // copy constructor
~Complex();
Complex& operator=(const Complex& rhs);
Complex& operator+=(const Complex& rhs);
// -=, *=, /=
bool operator>(const Complex& rhs);
bool operator==(const Complex& rhs);
bool operator!=(const Complex& rhs);
// <, >=, <=
const Complex operator+(const Complex& rhs);
const Complex operator-(const Complex& rhs);
// *, /
// pointer *, &
// not use
// %, ++, --
// &&, ||, !
// &, |, ~, ^, <<, >>
//type casting!
double real();
double imag();
void real(double re);
void imag(double im);
};
#endif
<complex.cpp>
#include "complex.h"
#include <cmath>
std::ostream& operator<< (std::ostream& out, const Complex& rhs)
{
out << "(" << rhs.re << ", " << rhs.im << "i)";
return out;
}
/*
Complex::Complex()
{
this->re = 0.0;
this->im = 0.0;
}
Complex::Complex(double re)
{
this->re = re;
this->im = 0.0;
}
*/
Complex::Complex(double re, double im)
{
this->re = re;
this->im = im;
}
Complex::Complex(const Complex &rhs)
{
this->re = rhs.re;
this->im = rhs.im;
}
Complex::~Complex()
{
}
Complex& Complex::operator=(const Complex& rhs)
{
this->re = rhs.re;
this->im = rhs.im;
return *this;
}
Complex& Complex::operator+=(const Complex& rhs)
{
this->re += rhs.re;
this->im += rhs.im;
return *this;
}
bool Complex::operator>(const Complex& rhs)
{
//return sqrt(this->re * this->re + this->im * this->im) > sqrt(rhs.re * rhs.re + rhs.im * rhs.im);
return (this->re * this->re + this->im * this->im) > (rhs.re * rhs.re + rhs.im * rhs.im);
}
bool Complex::operator==(const Complex& rhs)
{
/*
if (this->re == rhs.re && this->im = rhs.im)
{
return true;
}
else
{
return false;
}
*/
return (this->re == rhs.re && this->im == rhs.im);
}
bool Complex::operator!=(const Complex& rhs)
{
//return this->operator==(rhs);
//return !(*this == rhs);
return (this->re != rhs.re || this->im != rhs.im);
}
const Complex Complex::operator+(const Complex& rhs)
{
Complex result(this->re + rhs.re, this->im + rhs.im);
return result;
}
const Complex Complex::operator-(const Complex& rhs)
{
return Complex(this->re - rhs.im, this->im - rhs.im);
}
double Complex::real()
{
return this->re;
}
double Complex::imag()
{
return this->im;
}
void Complex::real(double re)
{
this->re = re;
}
void Complex::imag(double im)
{
this->im = im;
}
<compile 결과>