absorbe
This commit is contained in:
67
ex01/src/Fixed.cpp
Normal file
67
ex01/src/Fixed.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
#include "Fixed.hpp"
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <ostream>
|
||||
|
||||
Fixed::~Fixed()
|
||||
{
|
||||
std::cout << "Destructor called" << std::endl;
|
||||
}
|
||||
|
||||
Fixed::Fixed()
|
||||
{
|
||||
std::cout << "Default constructor called" << std::endl;
|
||||
}
|
||||
|
||||
Fixed::Fixed(const Fixed& src)
|
||||
{
|
||||
std::cout << "Copy constructor called" << std::endl;
|
||||
this->_value = src.getRawBits();
|
||||
}
|
||||
|
||||
Fixed::Fixed(const int& src)
|
||||
{
|
||||
std::cout << "Copy constructor called" << std::endl;
|
||||
this->_value = src << this->_nb_bits;
|
||||
}
|
||||
|
||||
Fixed::Fixed(const float& src)
|
||||
{
|
||||
std::cout << "Copy constructor called" << std::endl;
|
||||
this->_value = (int) roundf(src * (1 << this->_nb_bits));
|
||||
}
|
||||
|
||||
Fixed& Fixed::operator=(const Fixed& src)
|
||||
{
|
||||
std::cout << "Copy assignment operator called" << std::endl;
|
||||
this->_value = src.getRawBits();
|
||||
return (*this);
|
||||
}
|
||||
|
||||
int Fixed::getRawBits() const
|
||||
{
|
||||
std::cout << "getRawBits member function called" << std::endl;
|
||||
return (this->_value);
|
||||
}
|
||||
|
||||
void Fixed::setRawBits(const int raw)
|
||||
{
|
||||
std::cout << "setRawBits member function called" << std::endl;
|
||||
this->_value = raw;
|
||||
}
|
||||
|
||||
float Fixed::toFloat(void) const
|
||||
{
|
||||
return ((float) this->_value / (1 << this->_nb_bits));
|
||||
}
|
||||
|
||||
int Fixed::toInt() const
|
||||
{
|
||||
return ((int) (roundf(this->toFloat())));
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream &out, const Fixed& fixed)
|
||||
{
|
||||
out << (fixed.toFloat());
|
||||
return (out);
|
||||
}
|
29
ex01/src/Fixed.hpp
Normal file
29
ex01/src/Fixed.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef FIXED_H
|
||||
# define FIXED_H
|
||||
|
||||
# include <ostream>
|
||||
|
||||
class Fixed
|
||||
{
|
||||
private:
|
||||
int _value;
|
||||
static const int _nb_bits = 8;
|
||||
|
||||
public:
|
||||
Fixed();
|
||||
Fixed(const Fixed& src);
|
||||
Fixed(const int &n);
|
||||
Fixed(const float &f);
|
||||
~Fixed();
|
||||
|
||||
Fixed &operator=(const Fixed& src);
|
||||
|
||||
int getRawBits( void ) const;
|
||||
void setRawBits( int const raw );
|
||||
float toFloat(void) const ;
|
||||
int toInt(void) const ;
|
||||
};
|
||||
|
||||
std::ostream& operator << (std::ostream &out, const Fixed& fixed);
|
||||
|
||||
#endif
|
20
ex01/src/main.cpp
Normal file
20
ex01/src/main.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
#include "Fixed.hpp"
|
||||
#include <iostream>
|
||||
|
||||
int main( void ) {
|
||||
Fixed a;
|
||||
Fixed const b( 10 );
|
||||
Fixed const c( 42.42f );
|
||||
Fixed const d( b );
|
||||
a = Fixed( 1234.4321f );
|
||||
std::cout << "a is " << a << std::endl;
|
||||
std::cout << "b is " << b << std::endl;
|
||||
std::cout << "c is " << c << std::endl;
|
||||
std::cout << "d is " << d << std::endl;
|
||||
std::cout << "a is " << a.toInt() << " as integer" << std::endl;
|
||||
std::cout << "b is " << b.toInt() << " as integer" << std::endl;
|
||||
std::cout << "c is " << c.toInt() << " as integer" << std::endl;
|
||||
std::cout << "d is " << d.toInt() << " as integer" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user