From 4efbf7b1e85647d919133d8df7ac8aed001d8847 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Mon, 18 Sep 2023 08:57:37 +0200 Subject: [PATCH] Etape 5 --- .gitignore | 3 +- run.sh | 0 src/etape5/main.cpp | 12 ++++++++ src/etape5/point2d.cpp | 48 +++++++++++++++++++++++++++++++ src/etape5/point2d.h | 19 +++++++++++++ src/etape5/point3d.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++ src/etape5/point3d.h | 30 ++++++++++++++++++++ 7 files changed, 175 insertions(+), 1 deletion(-) mode change 100644 => 100755 run.sh create mode 100644 src/etape5/main.cpp create mode 100644 src/etape5/point2d.cpp create mode 100644 src/etape5/point2d.h create mode 100644 src/etape5/point3d.cpp create mode 100644 src/etape5/point3d.h diff --git a/.gitignore b/.gitignore index c795b05..d835f77 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build \ No newline at end of file +build +.cache \ No newline at end of file diff --git a/run.sh b/run.sh old mode 100644 new mode 100755 diff --git a/src/etape5/main.cpp b/src/etape5/main.cpp new file mode 100644 index 0000000..95f5767 --- /dev/null +++ b/src/etape5/main.cpp @@ -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; +} \ No newline at end of file diff --git a/src/etape5/point2d.cpp b/src/etape5/point2d.cpp new file mode 100644 index 0000000..1cb8ad8 --- /dev/null +++ b/src/etape5/point2d.cpp @@ -0,0 +1,48 @@ +#include "point2d.h" +#include + +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}; +} diff --git a/src/etape5/point2d.h b/src/etape5/point2d.h new file mode 100644 index 0000000..cd02997 --- /dev/null +++ b/src/etape5/point2d.h @@ -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 \ No newline at end of file diff --git a/src/etape5/point3d.cpp b/src/etape5/point3d.cpp new file mode 100644 index 0000000..ee6b483 --- /dev/null +++ b/src/etape5/point3d.cpp @@ -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); +} diff --git a/src/etape5/point3d.h b/src/etape5/point3d.h new file mode 100644 index 0000000..97f54ae --- /dev/null +++ b/src/etape5/point3d.h @@ -0,0 +1,30 @@ +#ifndef HEADER_POINT3D +#define HEADER_POINT3D + +#include "point2d.h" + +#include + +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 \ No newline at end of file