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
- 프로그래밍
- 사물인터넷
- 자바
- 자바정리
- 백준
- 프로그래머스
- C++
- backend
- SNS
- frontend
- 모의 주식
- Project
- Python
- Reflex
- 주가예측
- java
- 개발
- 프로그램
- 개발자
- 코딩독학
- 이웃한 칸
- java정리
- 자바독학
- 코스닥
- 풀스택프레임워크
- 개인프로젝트
- 코테
- 코딩
- 빅데이터
- 코딩테스트
Archives
- Today
- Total
Creative Code
main.cpp(타입캐스팅) 본문
※main.cpp파일
#include <iostream>
#include "complex.h"
#include "string.h"
int main() {
Complex c1;
Complex c2 = 3.0;
Complex c3(3.0,4.0);
const Complex c4 = c3; // Complex c4(c3)
c1.real(c3.real());
c1.imag(c3.imag());
c1 = c3;
if (c1 == c3) std::cout << "c1 and c3 are equal" << std::endl;
else std::cout << "c1 and c3 are not equal" << std::endl;
c1 != c3;
c2 = c1 + c3;
c2 = c1 - c3;
++c1;
c1++;
std::cout << "c1 : " << c1 << std::endl;
std::cout << "c2 : " << c2 << std::endl;
std::cout << "c3 : " << c3 << std::endl;
std::cout << "c4 : " << c4 << std::endl;
String s;
s = c1; // s = (String)c1;
std::cout << "s : " << s << std::endl;
return 0;
}
※complex.h파일
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
#include "string.h"
class Complex {
friend std::ostream& operator<<(std::ostream& out,const Complex& rhs);
private:
double re_;
double im_;
public:
Complex(double re = 0.0, double im = 0.0);
double real() const;
double imag() const;
void real(double re);
void imag(double im);
bool operator==(const Complex& rhs) const;
bool operator!=(const Complex& rhs) const;
operator String() const; // 타입캐스팅
const Complex operator+(const Complex& rhs) const;
const Complex operator-(const Complex& rhs) const;
const Complex& operator++(); // ++a
const Complex operator++(int ); // a++
};
#endif
※complex.cpp파일
#include "complex.h"
#include <cstdio>
std::ostream& operator<<(std::ostream& out,const Complex& rhs) {
out << "(" << rhs.re_ << ", " << rhs.im_ << "i)";
return out;
}
Complex::Complex(double re, double im) {
re_ = re; // this-> 는 생략가능(멤버변수와 함수의 지역변수 이름이 다를 때)
im_ = im;
}
double Complex::real() const {
return re_;
}
double Complex::imag() const {
return im_;
}
void Complex::real(double re) {
re_ = re;
}
void Complex::imag(double im) {
im_ = im;
}
Complex::operator String() const {
char buf[100];
sprintf(buf,"(%f,%fi)",re_,im_);
String result(buf);
return result;
}
bool Complex::operator==(const Complex& rhs) const {
return re_ == rhs.re_ && im_ == rhs.im_;
}
bool Complex::operator!=(const Complex& rhs) const {
return !this->operator==(rhs);
}
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;
}
const Complex& Complex::operator++() {
re_ +=1.0;
return *this;
}
const Complex Complex::operator++(int ) {
Complex result = *this;
re_ += 1.0;
return result;
}
'C++ Programming' 카테고리의 다른 글
add.cpp(cin으로 입력받기) (0) | 2023.09.05 |
---|---|
main.cpp(Rational클래스 라이브러리) (0) | 2023.09.04 |
main.cpp(rational클래스 증감연산자) (0) | 2023.09.04 |
main.cpp(complex클래스 증감연산자) (0) | 2023.09.01 |
main.cpp(employee클래스) (0) | 2023.09.01 |