This commit is contained in:
Yohan Boujon 2023-09-18 08:57:37 +02:00
parent 5ae0b0df8b
commit 4efbf7b1e8
7 changed files with 175 additions and 1 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
build
.cache

0
run.sh Normal file → Executable file
View file

12
src/etape5/main.cpp Normal file
View file

@ -0,0 +1,12 @@
#include "point2d.h"
#include "point3d.h"
int main(void)
{
Point2D pointA(2.0f, 3.0f);
Point3D pointB(pointA, 4.0f);
pointB = pointB*5;
pointA.Affiche2();
pointB.Affiche2();
return 0;
}

48
src/etape5/point2d.cpp Normal file
View file

@ -0,0 +1,48 @@
#include "point2d.h"
#include <iostream>
Point2D::Point2D()
: Point2D(0, 0)
{
}
Point2D::Point2D(float x)
: Point2D(x, 0)
{
}
Point2D::Point2D(float x, float y)
: _x(x)
, _y(y)
{
}
Point2D::~Point2D()
{
}
void Point2D::Saisie(float x, float y)
{
_x = x;
_y = y;
}
void Point2D::Affiche()
{
std::cout << "[POINT] X= " << _x << "\tY=" << _y << std::endl;
}
void Point2D::Affiche2()
{
Affiche();
}
Point2D Point2D::operator+(const Point2D& other)
{
return { this->_x + other._x, this->_y + other._y };
}
Point2D operator*(int value, const Point2D& point)
{
return {point._x*value, point._y*value};
}

19
src/etape5/point2d.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef HEADER_POINT2D
#define HEADER_POINT2D
class Point2D {
public:
Point2D();
Point2D(float x);
Point2D(float x, float y);
~Point2D();
void Saisie(float x, float y);
virtual void Affiche();
virtual void Affiche2();
Point2D operator+(const Point2D& other);
friend Point2D operator*(int value, const Point2D& point);
protected:
float _x,_y;
};
#endif

64
src/etape5/point3d.cpp Normal file
View file

@ -0,0 +1,64 @@
#include "point3d.h"
Point3D::Point3D()
: Point2D()
, _z(0.0f)
{
}
Point3D::Point3D(float x, float y, float z)
: Point2D(x, y)
, _z(z)
{
}
Point3D::Point3D(Point2D point, float z)
: Point2D(point)
, _z(z)
{
}
Point3D::~Point3D()
{
}
void Point3D::Saisie(float x, float y, float z)
{
Point2D::Saisie(x, y);
_z = z;
}
void Point3D::Affiche()
{
std::cout << "[POINT] X= " << _x << "\tY=" << _y << "\tZ= " << _z << std::endl;
}
Point3D Point3D::operator+(const Point3D& other)
{
return { this->_x + other._x, this->_y + other._y, this->_z + other._z };
}
Point3D Point3D::operator+(const Point2D& other)
{
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);
}

30
src/etape5/point3d.h Normal file
View file

@ -0,0 +1,30 @@
#ifndef HEADER_POINT3D
#define HEADER_POINT3D
#include "point2d.h"
#include <iostream>
class Point3D : public Point2D
{
public:
Point3D();
Point3D(float x, float y, float z);
Point3D(Point2D point,float z);
~Point3D();
void Saisie(float x, float y, float z);
void Affiche();
// Bonus avec les operateurs.
Point3D operator+(const Point3D& other);
// 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;
};
#endif