printf("ho_tari\n");

Complex 본문

C++

Complex

호타리 2023. 9. 2. 12:33

<main.cpp>

#include <iostream>
#include "complex.h"

int main()
{
	Complex c1(3.0,4.0); // 3.0 + 4.0i
	Complex c2(3.0);     // 3.0 + 0i
	Complex c3;			  // 0 + 0i

	c3.real(c1.real());
	c3.imag(c1.imag());

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

	return 0;
}

 

<complex.h>

#ifndef COMPLEX_H
#define COMPLEX_H

class Complex{
private:
	double re;   // real part
	double im;   // imaginary part

public:
	Complex(double re, double im);
	Complex(double re);
	Complex();
	~Complex();

	double real();
	double imag();

	void real(double re);
	void imag(double im);
};

#endif

 

<complex.cpp>

#include "complex.h"
//member function

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

}

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 결과>

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

Rational (사칙연산)  (0) 2023.09.02
Complex2 (연산)  (0) 2023.09.02
Empty (컴파일러가 자동생성하는 함수)  (0) 2023.09.01
String2  (0) 2023.09.01
Rational3 (여러연산자)  (0) 2023.08.31