생성일: 2022년 11월 29일 오후 11:08
최종 편집 일시: 2022년 12월 6일 오후 9:17
태그: cpp
cpp notion
Orthodox Canonical class form
정식 클래스 형식
https://www.francescmm.com/orthodox-canonical-class-form/
Fixed( void );
Fixed::Fixed( void ){ ... }
Fixed::Fixed(const Fixed &fixed){ ... }
Fixed fix_copy = Fixed::Fixed(fix_origin);
Fixed &Fixed::operator=(const Fixed &fixed)
연산자 오버로딩
오버로딩 vs 오버라이딩
오버로딩 overloading : 매개변수의 타입이나 개수가 다른 함수 중복 정의
오버라이딩 overriding : 상속과 다형성에서 사용되며 자식클래스가 부모클래스로부터 상속받은 함수에 재정의 하는것.
ex00 My First Class in Orthodox Canonical Form
orthodox canonical form 에 맞추어 class를 만들어보자.
float 와 int 사이의 변환을 해결한 부동소수점 클래스 기반 만들기
고정소수점
부동소수점
뜰 부, 움직일 동, 떠다니는 움직이는 소수점
http://www.tcpschool.com/cpp/cpp_datatype_floatingPointNumber
제한된 자릿수로 인해 표현 범위가 작은 고정 소수점에 비해 부동 소수점은 매우 큰 실수 까지 표현할 수 있다.
하지만 오차또한 존재한다는 단점도 있다. 10진수를 정확하게 표현하지 못하고 근사치를 표현한다.
263.3 = 0 / 10000111 / 00000111010011001100110
10000111 (127 + 8 = 135)
00000111 / 010011001100110 (0.29998779296875,, 0.3의 근사치)
E 지수 표기법
https://seulcode.tistory.com/461 E 지수 표기법
복사생성자, 대입연산자 오버로딩
1. Fixed a;
2. Fixed b( a );
3. Fixed c;
4. c = b;
/*
1. Default constructor called
2.
복사생성자 안에서 오버로딩된 대입연산자 사용
Copy constructor called
Copy assaignment operator called
getRawBits memeber function called
3.
Default constructor called
4.
Copy assaignment operator called
getRawBits memeber function called
*/
**ex01 Towards a more useful fixed-point number class**
float 와 int 사이의 변환을 부동소수점 클래스를 만들어서 해결하자.
허용함수 float roundf(float x)
;
<cmath>
const static int fraction = 8
Fixed::Fixed( const int num )
this->fixed_point = num << fraction;
Fixed::Fixed( const float num )
this->fixed_point = roundf(num * (1 << 8));
float Fixed::toFloat( void ) const{... }
int toInt( void ) const{ ... }
42.42f 과정
0 10000100 01010011010111000010100
**00101010 01101100
(bit) 부동소수점 42.421875**std::ostream &operator<<(std::ostream &**os**, const Fixed &**fixed**){ ... }
std::cout << fixed.toInt() << std::endl;
rvalue lvalue
a = Fixed( 1234.4321f );
a =
에는 대입연사자 오버로딩으로 Fixed( 1234.4321f)
가 반환하는 참조Fixed클래스가 반환하는 값을 복사해 저장한다.
Fixed( 1234.4321f)
가 반환하는 참조Fixed클래스는 rvalue로 실행이 끝나면 사라진다
Float constructor called
Copy assaignment operator called
Destructor called
r-value : 메모리 위치와 식별자를 특정할수 없는 데이터. (보통 오른쪽에 위치)
l-value : 메모리 위치를 참조하는 식 .
https://learn.microsoft.com/ko-kr/cpp/cpp/lvalues-and-rvalues-visual-cpp?view=msvc-170
https://assortrock.com/236 c++11 이전과 이후
ex02 Now we’re talking
멤버함수로 연산자 오버로딩 추가하기 + 정적맴버함수 min, max 작성
Fixed &operator++( void ); Fixed &operator--( void );
const Fixed operator++( int ); const Fixed operator--( int );
call by value
C++의 연산특성이 후위증가의 연속된 표현식을 배제하기 때문에 오버로딩하는 함수 또한 배제하는 것이다.
후위 연산자는 실행 다음행(세미콜론 후)에서 바뀌도록 정의하기 때문에 const를 붙여주어야 한다.
min, max
const
const int* const ptr = #
const int& ref = num;
ex03 BSP
bsp stands for Binary space partitioning
• const_cast<new_type>(expression)
• const_cast<바꿀타입>(대상)
포인터 또는 참조형의 상수성(const)를 잠깐 제거해주는데 사용한다. volatile키워드도 잠깐 제거해주는데 상요한다. 형변환은 불가능하다. 함수포인터도 불가능하다고 한다. https://blockdmask.tistory.com/240http://www.tcpschool.com/cpp/cpp_datatype_floatingPointNumber
Fixed f1 = line1.getX() - line2.getX();
Fixed f2 = point.getY() - line2.getY();
Fixed f3 = line1.getY() - line2.getY();
Fixed fx = f1 * f2 / f3 + line2.getX();
if (point.getX() < fx)
return true;