Skip to content

Commit

Permalink
removed usage of friend keyword and implemented min/max functions. ch…
Browse files Browse the repository at this point in the history
…anged some functions to const
  • Loading branch information
loyc12 committed Jul 4, 2023
1 parent 47e71c0 commit 93dbb29
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 68 deletions.
14 changes: 8 additions & 6 deletions mod2/ex00/Fixed.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#include "Fixed.hpp"

// Constructors
// Constructors / Destructor
Fixed::Fixed()
{
this->setRawBits(0);
std::cout << "[ default constructor called ] ";
}
Fixed::Fixed(const Fixed &other)
{
this->setRawBits(other.getRawBits());
std::cout << "[ copy constructor called ] ";
}
Fixed::~Fixed()
{
std::cout << "[ destructor called ] ";
}

// Operators
Fixed &Fixed::operator= (const Fixed &other)
{
this->setRawBits(other.getRawBits());
std::cout << "[ operator constructor called ] ";
return *this;
}

// Destructor
Fixed::~Fixed()
{
}

// Getters / Setters
int Fixed::getRawBits() const
{
Expand Down
9 changes: 7 additions & 2 deletions mod2/ex00/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
int main(void)
{
Fixed a;
std::cout << std::endl;

Fixed b(a);
Fixed c = b;
std::cout << std::endl;

Fixed c;
c = b;
std::cout << std::endl << std::endl;

std::cout << "value A : " << a.getRawBits() << std::endl;
std::cout << "value B : " << b.getRawBits() << std::endl;
std::cout << "value C : " << c.getRawBits() << std::endl;
std::cout << "value C : " << c.getRawBits() << std::endl << std::endl;
}
10 changes: 8 additions & 2 deletions mod2/ex01/Fixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,37 @@
Fixed::Fixed()
{
this->setRawBits(0);
std::cout << "[ default constructor called ] ";
}
Fixed::Fixed(const int value)
{
this->setRawBits(value * this->_frac_bit);
std::cout << "[ int constructor called ] ";

}
Fixed::Fixed(const float value)
{
this->setRawBits((int)(value * this->_frac_bit));
std::cout << "[ float constructor called ] ";

}
Fixed::Fixed(const Fixed &other)
{
this->setRawBits(other.getRawBits());
std::cout << "[ copy constructor called ] ";
}
Fixed::~Fixed()
{
std::cout << "[ destructor called ] ";
}

// Operators
Fixed &Fixed::operator= (const Fixed &other)
{
this->setRawBits(other.getRawBits());
std::cout << "[ operator constructor called ] ";
return *this;
}
// "friend" keyword allows us to create/override non-member functions
// function cannot be member of Fixed cause it needs to be a member of ostream
// this is because the ostream instance if the first argument of the "<<" operator
std::ostream &operator<< (std::ostream &out, const Fixed &fpn)
Expand All @@ -36,7 +43,6 @@ std::ostream &operator<< (std::ostream &out, const Fixed &fpn)
return (out);
}


// Others
int Fixed::getRawBits() const
{
Expand Down
6 changes: 3 additions & 3 deletions mod2/ex01/Fixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class Fixed

// Operators
Fixed &operator= (const Fixed &other);
friend std::ostream &operator<< (std::ostream &out, const Fixed &fpn);

// Other
// Others
int getRawBits() const;
void setRawBits(int raw_value);
float toFloat(void) const;
int toInt(void) const;

};

std::ostream &operator<< (std::ostream &out, const Fixed &fpn);

#endif
14 changes: 12 additions & 2 deletions mod2/ex01/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ int main(void)
{
Fixed a;
a.setRawBits(42 * 8);
std::cout << std::endl;

Fixed b(42);
std::cout << std::endl;

Fixed c(42.5f);
std::cout << std::endl;

Fixed d;
d = Fixed(42.25f);
std::cout << std::endl;

Fixed d = Fixed(42.25f);
Fixed e(b);
Fixed f = c;
std::cout << std::endl;

Fixed f;
f = c;
std::cout << std::endl << std::endl;

std::cout << "int value of A : " << a.toInt() << std::endl;
std::cout << "int value of B : " << b.toInt() << std::endl;
Expand Down
64 changes: 47 additions & 17 deletions mod2/ex02/Fixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,33 @@ Fixed &Fixed::operator= (const Fixed &other)
std::cout << "[ operator constructor called ] ";
return *this;
}
// function cannot be member of Fixed cause it needs to be a member of ostream
// this is because the ostream instance is the first argument of the "<<" operator
std::ostream &operator<< (std::ostream &out, const Fixed &fpn)
{
out << fpn.toFloat();
return (out);
}

Fixed Fixed::operator+ (const Fixed &other)
Fixed Fixed::operator+ (const Fixed &other) const
{
Fixed tmp;
tmp.setRawBits(this->getRawBits() + other.getRawBits());
return (tmp);
}
Fixed Fixed::operator- (const Fixed &other)
Fixed Fixed::operator- (const Fixed &other) const
{
Fixed tmp;
tmp.setRawBits(this->getRawBits() - other.getRawBits());
return (tmp);
}
Fixed Fixed::operator* (const Fixed &other)
Fixed Fixed::operator* (const Fixed &other) const
{
Fixed tmp;
tmp.setRawBits((this->getRawBits() * other.getRawBits()) / this->_frac_bit);
return (tmp);
}
Fixed Fixed::operator/ (const Fixed &other)
Fixed Fixed::operator/ (const Fixed &other) const
{
Fixed tmp;
tmp.setRawBits((this->getRawBits() * this->_frac_bit) / other.getRawBits());
Expand Down Expand Up @@ -82,27 +89,27 @@ Fixed &Fixed::operator/= (const Fixed &other)
}


bool Fixed::operator!= (const Fixed &other)
bool Fixed::operator!= (const Fixed &other) const
{
return (this->getRawBits() != other.getRawBits());
}
bool Fixed::operator> (const Fixed &other)
bool Fixed::operator> (const Fixed &other) const
{
return (this->getRawBits() > other.getRawBits());
}
bool Fixed::operator>= (const Fixed &other)
bool Fixed::operator>= (const Fixed &other) const
{
return (this->getRawBits() >= other.getRawBits());
}
bool Fixed::operator== (const Fixed &other)
bool Fixed::operator== (const Fixed &other) const
{
return (this->getRawBits() == other.getRawBits());
}
bool Fixed::operator<= (const Fixed &other)
bool Fixed::operator<= (const Fixed &other) const
{
return (this->getRawBits() <= other.getRawBits());
}
bool Fixed::operator< (const Fixed &other)
bool Fixed::operator< (const Fixed &other) const
{
return (this->getRawBits() < other.getRawBits());
}
Expand Down Expand Up @@ -137,15 +144,38 @@ Fixed Fixed::operator-- (int)
return (tmp);
}

// "friend" keyword allows us to create/override non-member functions
// function cannot be member of Fixed cause it needs to be a member of ostream
// this is because the ostream instance if the first argument of the "<<" operator
std::ostream &operator<< (std::ostream &out, const Fixed &fpn)
// Min/Max
// static member functions have no 'this' to use
Fixed &Fixed::min(Fixed &fpn1, Fixed &fpn2)
{
out << fpn.toFloat();
return (out);
if (fpn1 > fpn2)
return (fpn2);
else
return (fpn1);
}
Fixed &Fixed::max(Fixed &fpn1, Fixed &fpn2)
{
if (fpn1 > fpn2)
return (fpn1);
else
return (fpn2);
}
// function's return has to be const to be able to return a const var
const Fixed &Fixed::min(const Fixed &fpn1, const Fixed &fpn2)
{
if (fpn1 > fpn2)
return (fpn2);
else
return (fpn1);
}
// function's return has to be const to be able to return a const var
const Fixed &Fixed::max(const Fixed &fpn1, const Fixed &fpn2)
{
if (fpn1 > fpn2)
return (fpn1);
else
return (fpn2);
}


// Others
int Fixed::getRawBits() const
Expand Down
31 changes: 18 additions & 13 deletions mod2/ex02/Fixed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,41 @@ class Fixed
// Operators
Fixed &operator= (const Fixed &other);
// Arithmetics
Fixed operator+ (const Fixed &other);
Fixed operator- (const Fixed &other);
Fixed operator* (const Fixed &other);
Fixed operator/ (const Fixed &other);
Fixed operator+ (const Fixed &other) const;
Fixed operator- (const Fixed &other) const;
Fixed operator* (const Fixed &other) const;
Fixed operator/ (const Fixed &other) const;
Fixed &operator+= (const Fixed &other);
Fixed &operator-= (const Fixed &other);
Fixed &operator*= (const Fixed &other);
Fixed &operator/= (const Fixed &other);
// Comparisons
bool operator!= (const Fixed &other);
bool operator> (const Fixed &other);
bool operator>= (const Fixed &other);
bool operator== (const Fixed &other);
bool operator<= (const Fixed &other);
bool operator< (const Fixed &other);
bool operator!= (const Fixed &other) const;
bool operator> (const Fixed &other) const;
bool operator>= (const Fixed &other) const;
bool operator== (const Fixed &other) const;
bool operator<= (const Fixed &other) const;
bool operator< (const Fixed &other) const;
// Incrementations
Fixed &operator++ ();
Fixed &operator-- ();
Fixed operator++ (int);
Fixed operator-- (int);

friend std::ostream &operator<< (std::ostream &out, const Fixed &fpn);
// Min/Max
static Fixed &min(Fixed &fpn1, Fixed &fpn2);
static Fixed &max(Fixed &fpn1, Fixed &fpn2);
static const Fixed &min(const Fixed &fpn1, const Fixed &fpn2);
static const Fixed &max(const Fixed &fpn1, const Fixed &fpn2);

// Other
// Others
int getRawBits() const;
void setRawBits(int raw_value);
float toFloat() const;
int toInt() const;
int getFracBit() const;

};

std::ostream &operator<< (std::ostream &out, const Fixed &fpn);

#endif
Loading

0 comments on commit 93dbb29

Please sign in to comment.