diff --git a/ex01/Fixed/Fixed.cpp b/ex01/Fixed/Fixed.cpp new file mode 100644 index 0000000..635b22e --- /dev/null +++ b/ex01/Fixed/Fixed.cpp @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos 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); +} \ No newline at end of file diff --git a/ex01/Fixed/Fixed.hpp b/ex01/Fixed/Fixed.hpp new file mode 100644 index 0000000..e618c6a --- /dev/null +++ b/ex01/Fixed/Fixed.hpp @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* Fixed.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos +# include + +class Fixed +{ + private: + int value; + static const int fract = 8; + + public: + // Default constructor + Fixed(); + + // · Constructor with constant integer + Fixed(const int nb); + + // · Constructor with constant floating-point number + Fixed(const float nb); + + // Copy constructor + Fixed(const Fixed &other); + + // Copy assignment operator overload + Fixed& operator=(Fixed const &other); + + // Destructor + ~Fixed(); + + // getRawBits + int getRawBits( void ) const; + + // setRawBits + void setRawBits( int const raw ); + + // · toFloat + float toFloat( void ) const; + + // · toInt + int toInt( void ) const; + +}; + +// · << operator overload +std::ostream& operator<<(std::ostream& os, Fixed const &other); + +#endif \ No newline at end of file diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..03bd326 --- /dev/null +++ b/ex01/Makefile @@ -0,0 +1,25 @@ +NAME = fixed + +SRCS = main.cpp Fixed/Fixed.cpp +OBJS=$(SRCS:.cpp=.o) + +CC=c++ +CFLAGS=-Wall -Wextra -Werror -std=c++98 + +all: $(NAME) + +$(NAME): $(OBJS) + $(CC) $(CFLAGS) $(OBJS) -o $(NAME) + +%.o: %.cpp + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f $(OBJS) + +fclean: clean + rm -f $(NAME) + +re: fclean all + +.PHONY: all clean fclean re diff --git a/ex01/main.cpp b/ex01/main.cpp new file mode 100644 index 0000000..ae387ec --- /dev/null +++ b/ex01/main.cpp @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos