From 17d77d99de7b3d5e29a423ed2a6026ea40cade55 Mon Sep 17 00:00:00 2001 From: whaffman Date: Fri, 4 Apr 2025 12:37:17 +0200 Subject: [PATCH] better --- ex00/inc/Fixed.hpp | 7 ++++--- ex00/src/Fixed.cpp | 6 +++++- ex01/inc/Fixed.hpp | 4 ++-- ex01/src/Fixed.cpp | 6 +++++- ex02/inc/Fixed.hpp | 4 ++-- ex02/src/Fixed.cpp | 12 ++++++------ ex02/src/main.cpp | 12 ++++++------ ex03/inc/Fixed.hpp | 4 ++-- ex03/inc/Point.hpp | 5 ++--- ex03/src/Fixed.cpp | 6 ++++-- ex03/src/Point.cpp | 13 ++++--------- ex03/src/main.cpp | 4 ++-- 12 files changed, 44 insertions(+), 39 deletions(-) diff --git a/ex00/inc/Fixed.hpp b/ex00/inc/Fixed.hpp index e88a9fa..df68571 100644 --- a/ex00/inc/Fixed.hpp +++ b/ex00/inc/Fixed.hpp @@ -6,13 +6,14 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ -/* Updated: 2025/03/28 10:02:16 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:17:35 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #pragma once -class Fixed { +class Fixed +{ public: Fixed(); Fixed(const Fixed &fixed); @@ -23,5 +24,5 @@ public: private: int fixedPointValue; - static const int fractionalBits = 8; + static const int fractionalBits; }; \ No newline at end of file diff --git a/ex00/src/Fixed.cpp b/ex00/src/Fixed.cpp index 431e396..221f061 100644 --- a/ex00/src/Fixed.cpp +++ b/ex00/src/Fixed.cpp @@ -6,13 +6,15 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ -/* Updated: 2025/03/24 15:31:12 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:22:32 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include #include "Fixed.hpp" +const int Fixed::fractionalBits = 8; + Fixed::Fixed() { fixedPointValue = 0; @@ -32,6 +34,8 @@ Fixed::~Fixed() Fixed &Fixed::operator=(const Fixed &fixed) { std::cout << "Copy assignment operator called" << std::endl; + if (this == &fixed) + return *this; fixedPointValue = fixed.getRawBits(); return *this; } diff --git a/ex01/inc/Fixed.hpp b/ex01/inc/Fixed.hpp index 87c5844..8d5c4da 100644 --- a/ex01/inc/Fixed.hpp +++ b/ex01/inc/Fixed.hpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ -/* Updated: 2025/03/28 10:17:53 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:16:59 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ public: private: int fixedPointValue; - static const int fractionalBits = 8; + static const int fractionalBits; }; std::ostream &operator<<(std::ostream &out, const Fixed &fixed); \ No newline at end of file diff --git a/ex01/src/Fixed.cpp b/ex01/src/Fixed.cpp index 15924d8..6565649 100644 --- a/ex01/src/Fixed.cpp +++ b/ex01/src/Fixed.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ -/* Updated: 2025/03/27 10:57:00 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:22:01 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ #include "Fixed.hpp" #include +const int Fixed::fractionalBits = 8; + Fixed::Fixed() { fixedPointValue = 0; @@ -45,6 +47,8 @@ Fixed::~Fixed() Fixed &Fixed::operator=(const Fixed &fixed) { std::cout << "Copy assignment operator called" << std::endl; + if (this == &fixed) + return *this; fixedPointValue = fixed.fixedPointValue; return *this; } diff --git a/ex02/inc/Fixed.hpp b/ex02/inc/Fixed.hpp index 3bf4101..a434006 100644 --- a/ex02/inc/Fixed.hpp +++ b/ex02/inc/Fixed.hpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ -/* Updated: 2025/03/28 10:17:39 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:16:53 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -55,7 +55,7 @@ public: private: int fixedPointValue; - static const int fractionalBits = 8; + static const int fractionalBits; }; std::ostream &operator<<(std::ostream &out, const Fixed &fixed); diff --git a/ex02/src/Fixed.cpp b/ex02/src/Fixed.cpp index 7e6d7d5..075aec6 100644 --- a/ex02/src/Fixed.cpp +++ b/ex02/src/Fixed.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ -/* Updated: 2025/03/27 12:28:05 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:31:08 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ #include "Fixed.hpp" #include - +const int Fixed::fractionalBits = 8; Fixed::Fixed() { @@ -67,28 +67,28 @@ std::ostream &operator<<(std::ostream &out, const Fixed &fixed) Fixed Fixed::operator+(const Fixed &fixed) const { Fixed result; - result.setRawBits(getRawBits() + fixed.getRawBits()); + result.fixedPointValue = fixedPointValue + fixed.fixedPointValue;; return (result); } Fixed Fixed::operator-(const Fixed &fixed) const { Fixed result; - result.setRawBits(getRawBits() - fixed.getRawBits()); + result.fixedPointValue = fixedPointValue - fixed.fixedPointValue; return (result); } Fixed Fixed::operator*(const Fixed &fixed) const { Fixed result; - result.setRawBits((int)(((long long)getRawBits() * fixed.getRawBits()) >> fractionalBits)); + result.fixedPointValue = ((int)(((long long)fixedPointValue * fixed.fixedPointValue) >> fractionalBits)); return (result); } Fixed Fixed::operator/(const Fixed &fixed) const { Fixed result; - result.setRawBits((int)(((long long)getRawBits() << fractionalBits) / fixed.getRawBits())); + result.fixedPointValue = (int)(((long long)fixedPointValue << fractionalBits) / fixed.fixedPointValue); return (result); } diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp index 2d8f251..0c521c3 100644 --- a/ex02/src/main.cpp +++ b/ex02/src/main.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ -/* Updated: 2025/04/04 11:32:06 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:33:42 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -67,11 +67,11 @@ int main(void) std::cout << "min(a, c): " << Fixed::min(a, c) << std::endl; std::cout << "max(a, c): " << Fixed::max(a, c) << std::endl; - - std::cout << "c++: " << a << std::endl; - std::cout << "++c: " << ++a << std::endl; - std::cout << "c--: " << a << std::endl; - std::cout << "--c: " << --a << std::endl; + // Not possible because of const: + // std::cout << "c++: " << c++ << std::endl; + // std::cout << "++c: " << ++c << std::endl; + // std::cout << "c--: " << c-- << std::endl; + // std::cout << "--c: " << --c << std::endl; return 0; } \ No newline at end of file diff --git a/ex03/inc/Fixed.hpp b/ex03/inc/Fixed.hpp index ad45a96..d48d7ae 100644 --- a/ex03/inc/Fixed.hpp +++ b/ex03/inc/Fixed.hpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:22:48 by whaffman #+# #+# */ -/* Updated: 2025/03/27 16:09:40 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:16:48 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -55,7 +55,7 @@ public: private: int fixedPointValue; - static const int fractionalBits = 8; + static const int fractionalBits; }; std::ostream &operator<<(std::ostream &out, const Fixed &fixed); diff --git a/ex03/inc/Point.hpp b/ex03/inc/Point.hpp index 3295a8a..d4f7ca3 100644 --- a/ex03/inc/Point.hpp +++ b/ex03/inc/Point.hpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/27 13:09:47 by whaffman #+# #+# */ -/* Updated: 2025/03/27 16:09:07 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:13:03 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,8 +18,7 @@ class Point { public: Point(); Point(const Point &point); - Point(float x, float y); - Point(int x, int y); + Point(Fixed x, Fixed y); ~Point(); Point &operator=(const Point &point); diff --git a/ex03/src/Fixed.cpp b/ex03/src/Fixed.cpp index 7e6d7d5..6c30f87 100644 --- a/ex03/src/Fixed.cpp +++ b/ex03/src/Fixed.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:24:34 by whaffman #+# #+# */ -/* Updated: 2025/03/27 12:28:05 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:22:55 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ #include "Fixed.hpp" #include - +const int Fixed::fractionalBits = 8; Fixed::Fixed() { @@ -53,6 +53,8 @@ Fixed &Fixed::operator=(const Fixed &fixed) { if (DEBUG) std::cout << "Copy assignment operator called" << std::endl; + if (this == &fixed) + return *this; fixedPointValue = fixed.fixedPointValue; return *this; } diff --git a/ex03/src/Point.cpp b/ex03/src/Point.cpp index 8bc28ed..4e21fa7 100644 --- a/ex03/src/Point.cpp +++ b/ex03/src/Point.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/27 15:45:12 by whaffman #+# #+# */ -/* Updated: 2025/03/27 16:01:14 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:23:35 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,16 +22,9 @@ Point::Point(const Point &point) *this = point; } -Point::Point(float x, float y) -{ - _x = Fixed(x); - _y = Fixed(y); -} -Point::Point(int x, int y) +Point::Point(Fixed x, Fixed y) : _x(Fixed(x)), _y(Fixed(y)) { - _x = Fixed(x); - _y = Fixed(y); } Point::~Point() @@ -40,6 +33,8 @@ Point::~Point() Point &Point::operator=(const Point &point) { + if (this == &point) + return *this; _x = point.x(); _y = point.y(); return *this; diff --git a/ex03/src/main.cpp b/ex03/src/main.cpp index 748c7d9..a58f71d 100644 --- a/ex03/src/main.cpp +++ b/ex03/src/main.cpp @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/03/24 15:31:26 by whaffman #+# #+# */ -/* Updated: 2025/04/04 11:41:46 by whaffman ######## odam.nl */ +/* Updated: 2025/04/04 12:35:39 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ void doTriangleCheck(const Point &a, const Point &b, const Point &c, const Point int main(void) { - doTriangleCheck(Point(0, 0), Point(5, 0), Point(2.5f, 5.0f), Point(2.5f, 2.0f)); + doTriangleCheck(Point(0, 0), Point(5, 0), Point(2.5f, 5), Point(2.5f, 2.0f)); doTriangleCheck(Point(-3, -3), Point(3, -3), Point(0, 3), Point(0, 0)); doTriangleCheck(Point(1, 1), Point(4, 1), Point(2.5f, 4.0f), Point(2.5f, 2.5f)); doTriangleCheck(Point(-2, -2), Point(2, -2), Point(0, 2), Point(-1, -1));