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
- 개인프로젝트
- 백준
- Reflex
- 사물인터넷
- 자바정리
- 코테
- 프로그램
- 개발
- 프로그래밍
- java
- 빅데이터
- 개발자
- 이웃한 칸
- 코딩
- 코딩독학
- SNS
- 자바
- backend
- 모의 주식
- 주가예측
- java정리
- 코딩테스트
- 프로그래머스
- Python
- C++
- 풀스택프레임워크
- 코스닥
- 자바독학
- frontend
- Project
Archives
- Today
- Total
Creative Code
complex6(inline함수, namespace) 본문
※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 |