Creative Code

complex6(inline함수, namespace) 본문

C++ Programming

complex6(inline함수, namespace)

빛하루 2023. 9. 11. 11:37

※main.cpp파일

#include <iostream>

#include "complex.h"



using iot::Complex;

int main() {

	iot::Complex c1(3.0,4.0);

	Complex c2(3.0);    

	Complex c3;

	const Complex c4 = c1;

	

	c2 = c1;

	c2.real(c1.real());

	c2.imag(c1.imag());

	

	std::cout << "c1 : (" << c1.real() << ", " <<c1.imag() << "i)" << std::endl;

	

	c3 = c1 + c2;

	c3 = c1 - c2;

	

	c1 += c2;

	c1 -= c2;

	

	c1>c2;

	c1<c2;

	c1>=c2;

	c1<=c2;

	

	if (c1 == c2) {

		std::cout << "c1 and c2 are equal" << std::endl;

	} else {

		std::cout << "c1 and c2 are not equal" << std::endl;

	}

	

	c1 != c2;

	

	std::cin >> c3;

	

	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>





namespace iot{



class Complex {

friend std::ostream& operator<<(std::ostream& out, const Complex& rhs);

friend std::istream& operator>>(std::istream& in, Complex &rhs);

private:

	double re_;

	double im_;



protected:



public:

	Complex(double re = 0.0 , double im = 0.0);

	

	double real() const;

	double imag() const;



	void real(double re);

	void imag(double im);

	

	const Complex operator+(const Complex &rhs) const;

	const Complex operator-(const Complex &rhs) const;

	

	Complex& operator+=(const Complex &rhs);

	Complex& operator-=(const Complex &rhs);

	

	bool operator==(const Complex &rhs) const;

	bool operator!=(const Complex &rhs) const;

	bool operator>(const Complex &rhs) const;

	bool operator<(const Complex &rhs) const;

	bool operator>=(const Complex & rhs) const;

	bool operator<=(const Complex & rhs) const;

	

};

inline Complex::Complex(double re, double im) 

:re_(re),im_(im)

{



}



inline double Complex::real() const {

	return re_;

}



inline double Complex::imag() const {

	return im_;

}



inline void Complex::real(double re) {

	re_ = re;

}



inline void Complex::imag(double im) {

	im_ = im;

}

}

#endif

※complex.cpp파일

#include "complex.h"

namespace iot{

std::ostream& operator<<(std::ostream& out,const Complex& rhs) {

	out << "(" << rhs.re_ << ", "<<rhs.im_ << "i)";

	return out;

}



std::istream& operator>>(std::istream& in,  Complex& rhs) {

	double re = 0.0, im = 0.0;

	char c = 0;

	in >>c;

	if (c == '(') {

		in>>re>>c;

		if (c == ',') in >> im>>c;

		if (c == 'i') in >>c;

		if (c != ')') in.clear(std::ios::failbit);

	}else {

		in.putback(c);

		in>>re;

	}

	if (in )

		rhs = Complex(re,im);

	return in;

}







const Complex Complex::operator+(const Complex& rhs) const{

	Complex result(re_ + rhs.re_, im_+rhs.im_);

	return result;

}



const Complex Complex::operator-(const Complex& rhs) const {

	Complex result(re_ - rhs.re_, im_ - rhs.im_);

	return result;

}



Complex& Complex::operator+=(const Complex& rhs) {

	return *this = *this+rhs;

}



Complex& Complex::operator-=(const Complex& rhs) {

	return *this = *this - rhs;

}



bool Complex::operator==(const Complex &rhs) const{

	return (re_ == rhs.re_ && im_ ==rhs.im_);

}



bool Complex::operator!=(const Complex &rhs) const{

	return !(*this == rhs);

}



bool Complex::operator>(const Complex& rhs) const {

	return re_ * re_ + im_ * im_ > rhs.re_ * rhs.re_ + rhs.im_ * rhs.im_;

}



bool Complex::operator<(const Complex& rhs) const {

	return re_ * re_ + im_ * im_ < rhs.re_ * rhs.re_ + rhs.im_ * rhs.im_;

}



bool Complex::operator>=(const Complex& rhs) const {

	return !(*this < rhs);

}



bool Complex::operator<=(const Complex& rhs) const {

	return !(*this >rhs);

}

}

 

'C++ Programming' 카테고리의 다른 글

shape(dynamic_cast)  (0) 2023.09.08
string4(const_cast)  (0) 2023.09.08
pointer3.cpp(reinterpret_cast)  (0) 2023.09.08
genderRatio.cpp(static_cast)  (0) 2023.09.08
safeArray4(try-throw-catch)  (0) 2023.09.08