Creative Code

main.cpp(complex클래스 여러 연산자) 본문

C++ Programming

main.cpp(complex클래스 여러 연산자)

빛하루 2023. 8. 31. 12:07

※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;     // default constructor

	//c4 = c3;        // assignment

	

	//c1.real(c3.real());

	//c1.imag(c3.imag());

	c1 = c3;

	

	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 : " << 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); 

//전역함수이지만 Complex클래스의 private멤버에 접근하고 싶을 때 friend로 선언해준다.

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 &);			  //같은 class의 객체로만든 생성자를 copy constructor라고 한다.

	~Complex();

	

	double real();

	double imag();

	

	void real(double re);

	void imag(double im);

	

	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);

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



};



#endif

※complex.cpp파일

#include "complex.h"

#include <cmath>



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

	out << "(" << rhs.re << ", " << rhs.im << ")";

	return out;

}

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() {

}



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;

}



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

	this->re = rhs.re;

	this->im = rhs.im;

	return *this;

}



bool Complex::operator==(const Complex &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){

	Complex result(this->re - rhs.re, this->im - rhs.im);

	return result;

}



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

	this->re += rhs.re;

	this->im += rhs.im;

	return *this;

}



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

	return (this->re * this->re + this->im * this->im) > (rhs.re * rhs.re + rhs.im * rhs.im); 

}



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

	return !(*this==rhs);

	//return !this->operator==(rhs);

	//return (this->re != rhs.re || this->im != rhs.im);

}

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

main.cpp(string)  (0) 2023.08.31
main.cpp(rational클래스 여러 연산자)  (0) 2023.08.31
main.cpp(Rational 사칙연산)  (0) 2023.08.30
main.cpp(complex 연산)  (0) 2023.08.30
main.cpp(complex 클래스)  (0) 2023.08.30