ex01 done
This commit is contained in:
87
ex01/Fixed/Fixed.cpp
Normal file
87
ex01/Fixed/Fixed.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* Fixed.cpp :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/09/10 00:54:50 by tigos #+# #+# */
|
||||
/* Updated: 2025/09/10 22:02:52 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "Fixed.hpp"
|
||||
|
||||
// Default constructor
|
||||
Fixed::Fixed()
|
||||
{
|
||||
std::cout << "Default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
// · Constructor with constant integer
|
||||
Fixed::Fixed(const int nb)
|
||||
{
|
||||
std::cout << "Int constructor called" << std::endl;
|
||||
this->value = nb * (1 << this->fract);
|
||||
}
|
||||
|
||||
// · Constructor with constant floating-point number
|
||||
Fixed::Fixed(const float nb)
|
||||
{
|
||||
std::cout << "Float constructor called" << std::endl;
|
||||
this->value = roundf(nb * (1 << this->fract));
|
||||
}
|
||||
|
||||
// Copy constructor
|
||||
Fixed::Fixed(const Fixed &other)
|
||||
{
|
||||
std:: cout << "Copy constructor called" << std::endl;
|
||||
this->value = other.value;
|
||||
}
|
||||
|
||||
// Copy assignment operator overload
|
||||
Fixed& Fixed::operator=(Fixed const &other)
|
||||
{
|
||||
std::cout << "Copy assignment operator called" << std::endl;
|
||||
if (this != &other)
|
||||
this->value = other.getRawBits();
|
||||
return (*this);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Fixed::~Fixed()
|
||||
{
|
||||
std::cout << "Destructor called" << std::endl;
|
||||
}
|
||||
|
||||
// getRawBits()
|
||||
int Fixed::getRawBits( void ) const
|
||||
{
|
||||
std::cout << "getRawBits member function called" << std::endl;
|
||||
return (this->value);
|
||||
}
|
||||
|
||||
// setRawBits()
|
||||
void Fixed::setRawBits( int const raw )
|
||||
{
|
||||
this->value = raw;
|
||||
}
|
||||
|
||||
// · toFloat
|
||||
float Fixed::toFloat( void ) const
|
||||
{
|
||||
return (this->value / (float)(1 << this->fract));
|
||||
}
|
||||
|
||||
// · toInt
|
||||
int Fixed::toInt( void ) const
|
||||
{
|
||||
return (this->value >> this->fract);
|
||||
}
|
||||
|
||||
// · << operator overload
|
||||
std::ostream& operator<<(std::ostream& os, Fixed const &other)
|
||||
{
|
||||
os << other.toFloat();
|
||||
return (os);
|
||||
}
|
||||
Reference in New Issue
Block a user