Modification Etape 3 et Etape 4 : Ajout d'opérateurs friend.
This commit is contained in:
parent
9d2afb792a
commit
5ae0b0df8b
5 changed files with 40 additions and 10 deletions
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue