cpp/CPP02/ex01/Fixed.cpp
2024-12-04 17:29:31 +01:00

55 lines
1.1 KiB
C++

#include "Fixed.hpp"
Fixed::Fixed(void) {
this->rawBits = 0;
std::cout << "Default constructor called" << std::endl;
}
Fixed::Fixed(const int raw) {
this->rawBits = int(raw * (1 << bits));
std::cout << "Int constructor called" << std::endl;
}
Fixed::Fixed(const float raw) {
this->rawBits = int(roundf(raw * (1 << bits)));
std::cout << "Float constructor called" << std::endl;
}
Fixed::~Fixed() {
std::cout << "Destructor called" << std::endl;
}
Fixed& Fixed::operator=(const Fixed &fixed) {
if (this != &fixed) {
this->rawBits = fixed.rawBits;
}
std::cout << "Copy assignment operator called" << std::endl;
return *this;
}
std::ostream& operator<<(std::ostream& os, const Fixed &fixed) {
os << fixed.toFloat();
return os;
}
Fixed::Fixed(const Fixed &fixed) {
this->rawBits = fixed.rawBits;
std::cout << "Copy constructor called" << std::endl;
}
int Fixed::toInt(void) const{
return int(this->rawBits) / int(1 << this->bits);
}
float Fixed::toFloat(void) const{
return float(this->rawBits) / float(1 << this->bits);
}
int Fixed::getRawBits(void) const {
return this->rawBits;
}
void Fixed::setRawBits(const int raw) {
this->rawBits = raw;
}