d02 ex02 operator < ok

This commit is contained in:
hugogogo
2022-02-15 00:31:59 +01:00
parent b08663e74e
commit c9a833bc00
6 changed files with 60 additions and 31 deletions

View File

@@ -125,8 +125,8 @@ float Fixed::toFloat( void ) const {
* took here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp
*/
std::ostream & operator<<(std::ostream & out, Fixed const & fixed)
std::ostream & operator<<(std::ostream & o, Fixed const & rhs)
{
out << fixed.toFloat();
return (out);
o << rhs.toFloat();
return (o);
}

View File

@@ -30,6 +30,6 @@ private:
};
std::ostream & operator<<(std::ostream & out, Fixed const & fixed);
std::ostream & operator<<(std::ostream & o, Fixed const & rhs);
#endif

View File

@@ -53,18 +53,15 @@ int const Fixed::_max = -1U >> (_frac +1);
*/
Fixed::Fixed() : _value(0) {
std::cout << "Default constructor called" << '\n';
return;
}
Fixed::Fixed(Fixed const & src) {
std::cout << "Copy constructor called" << '\n';
*this = src;
return;
}
Fixed::~Fixed( void ) {
std::cout << "Destructor called" << '\n';
return;
}
@@ -73,8 +70,6 @@ Fixed::~Fixed( void ) {
*/
Fixed::Fixed(int integer) {
std::cout << "Int constructor called" << '\n';
if (integer < ~this->_max || integer > this->_max)
std::cout << "error: integer out of range" << '\n';
else
@@ -82,8 +77,6 @@ Fixed::Fixed(int integer) {
}
Fixed::Fixed(float const floater) {
std::cout << "Float constructor called" << '\n';
if (floater < ~this->_max || floater > this->_max)
std::cout << "error: float out of range" << '\n';
else
@@ -95,12 +88,33 @@ Fixed::Fixed(float const floater) {
*/
Fixed & Fixed::operator=( Fixed const & rhs ) {
std::cout << "Copy assignment operator called" << '\n';
if ( this != &rhs )
this->_value = rhs.getRawBits();
return *this;
}
/*
* other operators
*/
//Fixed Fixed::operator+( Fixed const & rhs ) const {
// return Fixed( this->toFloat() + rhs.toFloat() );
//}
bool Fixed::operator< (Fixed const & rhs) const {
return this->toFloat() > rhs.toFloat();
}
//Fixed operator> (Fixed const & rhs) const {
//}
//Fixed operator<=(Fixed const & rhs) const {
//}
//Fixed operator>=(Fixed const & rhs) const {
//}
//Fixed operator==(Fixed const & rhs) const {
//}
//Fixed operator!=(Fixed const & rhs) const {
//}
/*
* functions that returns _value
*/
@@ -125,8 +139,8 @@ float Fixed::toFloat( void ) const {
* took here : https://github.com/pgomez-a/42_CPP_Piscine/blob/master/cpp02/ex01/Fixed.cpp
*/
std::ostream & operator<<(std::ostream & out, Fixed const & fixed)
std::ostream & operator<<(std::ostream & o, Fixed const & rhs)
{
out << fixed.toFloat();
return (out);
o << rhs.toFloat();
return (o);
}

View File

@@ -15,7 +15,14 @@ public:
Fixed(int integer);
Fixed(float const floater);
Fixed & operator=(Fixed const & rhs); // assignement operator
Fixed & operator= (Fixed const & rhs); // assignement operator
// Fixed operator+ (Fixed const & rhs) const;
bool operator< (Fixed const & rhs) const;
// Fixed operator> (Fixed const & rhs) const;
// Fixed operator<=(Fixed const & rhs) const;
// Fixed operator>=(Fixed const & rhs) const;
// Fixed operator==(Fixed const & rhs) const;
// Fixed operator!=(Fixed const & rhs) const;
int getRawBits(void) const;
void setRawBits(int const raw);
@@ -30,6 +37,6 @@ private:
};
std::ostream & operator<<(std::ostream & out, Fixed const & fixed);
std::ostream & operator<<(std::ostream & o, Fixed const & rhs);
#endif

BIN
d02/ex02/fixed Executable file

Binary file not shown.

View File

@@ -3,22 +3,30 @@
int main( void ) {
Fixed a;
Fixed const b( 10 );
Fixed const c( 42.42f );
Fixed const d( b );
Fixed a(10);
Fixed b(5);
Fixed c;
a = Fixed( 1234.4321f );
std::cout << "a: " << a << '\n';
std::cout << "b: " << b << '\n';
if (a < b)
c = a;
else
c = b;
std::cout << "c: " << c << '\n';
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;
// Fixed a;
// Fixed const b( Fixed( 5.05f ) * Fixed( 2 ) );
//
// std::cout << a << std::endl;
// std::cout << ++a << std::endl;
// std::cout << a << std::endl;
// std::cout << a++ << std::endl;
// std::cout << a << std::endl;
//
// std::cout << b << std::endl;
//
// std::cout << Fixed::max( a, b ) << std::endl
return 0;
}