From 5ae0b0df8b9aa0a88721b7422bd2da7c5940ad5d Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sun, 17 Sep 2023 23:17:59 +0200 Subject: [PATCH] =?UTF-8?q?Modification=20Etape=203=20et=20Etape=204=20:?= =?UTF-8?q?=20Ajout=20d'op=C3=A9rateurs=20friend.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/etape3/point2d.cpp | 7 ++++--- src/etape3/point2d.h | 2 +- src/etape4/point2d.cpp | 31 +++++++++++++++++++++++++++---- src/etape4/point2d.h | 2 +- src/etape4/point3d.h | 8 +++++++- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/etape3/point2d.cpp b/src/etape3/point2d.cpp index 8606372..bfc49f1 100644 --- a/src/etape3/point2d.cpp +++ b/src/etape3/point2d.cpp @@ -32,9 +32,10 @@ Point2D Point2D::operator+(const Point2D& other) return {this->_x + other._x, this->_y + other._y}; } -Point2D Point2D::operator*(int other) +// Question : Doit-t-on réellement ajouter friend pour pouvoir faire une opération dans les deux sens avec Point2D ? +Point2D operator*(int value, const Point2D& point) { - return {this->_x*other, this->_y*other}; + return {point._x*value, point._y*value}; } int main(void) @@ -50,7 +51,7 @@ int main(void) Point2D A(1,2), B(3,4),C; C = A + B; C.Affiche(); - C = A*5; + C = 5*A; C.Affiche(); return 0; } \ No newline at end of file diff --git a/src/etape3/point2d.h b/src/etape3/point2d.h index 78fb06f..93527f7 100644 --- a/src/etape3/point2d.h +++ b/src/etape3/point2d.h @@ -10,7 +10,7 @@ class Point2D { void Saisie(float x, float y); void Affiche(); Point2D operator+(const Point2D& other); - Point2D operator*(int left); + friend Point2D operator*(int value, const Point2D& point); private: float _x,_y; }; diff --git a/src/etape4/point2d.cpp b/src/etape4/point2d.cpp index 057905e..f70197b 100644 --- a/src/etape4/point2d.cpp +++ b/src/etape4/point2d.cpp @@ -38,9 +38,9 @@ Point2D Point2D::operator+(const Point2D& other) return { this->_x + other._x, this->_y + other._y }; } -Point2D Point2D::operator*(int other) +Point2D operator*(int value, const Point2D& point) { - return { this->_x * other, this->_y * other }; + return {point._x*value, point._y*value}; } Point3D::Point3D() @@ -81,9 +81,29 @@ Point3D Point3D::operator+(const Point3D& other) return { this->_x + other._x, this->_y + other._y, this->_z + other._z }; } -Point3D Point3D::operator*(int left) +Point3D Point3D::operator+(const Point2D& other) { - return { this->_x*left, this->_y*left , this->_z*left }; + Point3D returnOther(other,0.0f); + return { this->_x + returnOther._x, this->_y + returnOther._y, this->_z + returnOther._z }; +} + +/* +Point3D operator+(const Point2D& point2d, const Point3D& point3d) +{ + Point3D returnOther(point2d,0.0f); + return { point3d._x + returnOther._x, point3d._y + returnOther._y, point3d._z + returnOther._z }; +} +*/ + +Point3D operator*(int value, const Point3D& point) +{ + return { point._x*value, point._y*value , point._z*value }; +} + +// Dans les deux sens, un second operator* doit être crée. +Point3D operator*(const Point3D& point, int value) +{ + return (value*point); } int main(void) @@ -98,5 +118,8 @@ int main(void) auto pointE = pointB+pointD; pointE.Affiche(); (pointD*3).Affiche(); + std::cout << "Ajout d'un point2D (A, [2,3]) à un point3D (D, [1,1,1]) :" << std::endl; + auto superPoint = pointD+pointA; + superPoint.Affiche(); return 0; } \ No newline at end of file diff --git a/src/etape4/point2d.h b/src/etape4/point2d.h index 743c8c8..b8f23dc 100644 --- a/src/etape4/point2d.h +++ b/src/etape4/point2d.h @@ -10,7 +10,7 @@ class Point2D { void Saisie(float x, float y); void Affiche(); Point2D operator+(const Point2D& other); - Point2D operator*(int left); + friend Point2D operator*(int value, const Point2D& point); protected: float _x,_y; }; diff --git a/src/etape4/point3d.h b/src/etape4/point3d.h index 06da85c..f000d02 100644 --- a/src/etape4/point3d.h +++ b/src/etape4/point3d.h @@ -14,7 +14,13 @@ class Point3D : public Point2D void Affiche(); // Bonus avec les operateurs. Point3D operator+(const Point3D& other); - Point3D operator*(int left); + // Empêche une erreur lorsque point3D+point2D + Point3D operator+(const Point2D& other); + // Upgrade un point2D en point3D s'il est additioné avec ce dernier + // Ne fonctionne pas à cause de problème d'overload... + //friend Point3D operator+(const Point2D& point2d, const Point3D& point3d); + friend Point3D operator*(int value, const Point3D& point); + friend Point3D operator*(const Point3D& point, int value); private: float _z; };