5.1 연산자 오버로딩(비교, 대입 연산자)
Last updated
Last updated
bool operator==(MyString& str);bool MyString::operator==(MyString& str) {
...
}Complex& operator=(const Complex& c);a=b=c;Complex& operator=(const Complex& c)
{
real = c.real;
img = c.img;
return *this;
}#include <iostream>
class Complex {
private:
double real, img;
public:
Complex(double real, double img) : real(real), img(img) {}
Complex(const Complex& c) { real = c.real, img = c.img; }
Complex operator+(const Complex& c) const;
Complex operator-(const Complex& c) const;
Complex operator*(const Complex& c) const;
Complex operator/(const Complex& c) const;
Complex& operator=(const Complex& c);
void println() { std::cout << "( " << real << " , " << img << " ) " << std::endl; }
};
Complex Complex::operator+(const Complex& c) const {
Complex temp(real + c.real, img + c.img);
return temp;
}
Complex Complex::operator-(const Complex& c) const {
Complex temp(real - c.real, img - c.img);
return temp;
}
Complex Complex::operator*(const Complex& c) const {
Complex temp(real * c.real - img * c.img, real * c.img + img * c.real);
return temp;
}
Complex Complex::operator/(const Complex& c) const {
Complex temp(
(real * c.real + img * c.img) / (c.real * c.real + c.img * c.img),
(img * c.real - real * c.img) / (c.real * c.real + c.img * c.img));
return temp;
}
Complex& Complex::operator=(const Complex& c) {
real = c.real;
img = c.img;
return *this;
}
int main() {
Complex a(1.0, 2.0);
Complex b(3.0, -2.0);
Complex c(0.0, 0.0);
c = a * b + a / b + a + b;
c.println();
}