반응형
ex00은 아직 소수점에 대한 이야기는 하지 않고 복사 생성자, 오버로드 vs 오버라이딩, 복사 할당(대입) 연산자에 대해서 말하고 있다.
나의 글의 C++의 복사 생성자, 오버로드 vs 오버라이딩, 복사 할당 연산자에 대해서 보고 오면 좋을 것 같다.
복사 생성자, 복사 할당(대입) 연산자
'C++/C++ 복사 생성자, 복사 할당 연산자(Canonical form)' 카테고리의 글 목록
예비 프로그래머 42 - jaeyojun
wo-dbs.tistory.com
오버로드, 오버라이딩
https://wo-dbs.tistory.com/124
오버로드 vs 오버라이딩
오버로딩(Overloading)과 오버라이딩(Overriding) 오버로드란? : 함수 중복 정의 오버로딩은 같은 이름의 함수에 매개변수를 다르게 사용하여 매개 변수에 따라 다른 함수가 실행되는 것 오버라이딩(Ove
wo-dbs.tistory.com
main.cpp
#include "Fixed.hpp"
int main( void )
{
Fixed a;
Fixed b( a );
Fixed c;
c = b;
std::cout << a.getRawBits() << std::endl;
std::cout << b.getRawBits() << std::endl;
std::cout << c.getRawBits() << std::endl;
return 0;
}
fixed.hpp
#ifndef FIXED_HPP
# define FIXED_HPP
# include <iostream>
class Fixed{
private:
const static int bits = 8;
int number;
public:
Fixed();
Fixed(const Fixed &obj);
Fixed& operator=(const Fixed &obj);
~Fixed();
int getRawBits(void) const;
void setRawBits(int const raw);
};
# endif
fixed.cpp
#include "Fixed.hpp"
Fixed::Fixed(void)
{
std::cout << "Default constructor called" << std::endl;
this->number = 0;
}
Fixed::Fixed(const Fixed &obj)
{
std::cout << "Copy constructor called" << std::endl;
this->number = obj.getRawBits();
}
Fixed& Fixed::operator=(const Fixed &obj)
{
std::cout << "Copy assignment operator called" << std::endl;
if (this == &obj)
return *this;
this->number = obj.getRawBits();
return *this;
}
Fixed::~Fixed(void)
{
std::cout << "Destructor called" << std::endl;
}
int Fixed::getRawBits(void) const
{
std::cout << "getRawBits member function called" << std::endl;
return (this->number);
}
void Fixed::setRawBits(int const raw)
{
this->number = raw;
}
반응형
'42Seoul > CPP Module 02' 카테고리의 다른 글
ex02(비교, 산술, 증감 연산자 오버로딩) (1) | 2024.01.24 |
---|---|
ex01(>>, << 쉬프트 연산, 입출력 연산자 오버로딩) (1) | 2024.01.24 |