Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- 프로그래밍
- 개인프로젝트
- 자바
- 사물인터넷
- Python
- C++
- 프로그램
- SNS
- java
- 풀스택프레임워크
- 코딩독학
- backend
- 빅데이터
- 이웃한 칸
- 백준
- frontend
- 코딩테스트
- java정리
- 자바정리
- 개발
- Project
- 모의 주식
- 개발자
- 코스닥
- 프로그래머스
- Reflex
- 주가예측
- 자바독학
- 코딩
- 코테
Archives
- Today
- Total
Creative Code
main.cpp(complex클래스 여러 연산자) 본문
※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 |