From ef71402c9482af553b6cdae9b4c856cabd819908 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Mon, 18 Sep 2023 14:03:08 +0200 Subject: [PATCH] Etape 6 --- src/etape6/main.cpp | 16 +++++++++++ src/etape6/point2d.cpp | 62 ++++++++++++++++++++++++++++++++++++++++ src/etape6/point2d.h | 23 +++++++++++++++ src/etape6/point3d.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++ src/etape6/point3d.h | 30 ++++++++++++++++++++ 5 files changed, 195 insertions(+) create mode 100644 src/etape6/main.cpp create mode 100644 src/etape6/point2d.cpp create mode 100644 src/etape6/point2d.h create mode 100644 src/etape6/point3d.cpp create mode 100644 src/etape6/point3d.h diff --git a/src/etape6/main.cpp b/src/etape6/main.cpp new file mode 100644 index 0000000..288028e --- /dev/null +++ b/src/etape6/main.cpp @@ -0,0 +1,16 @@ +#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(); + std::cout << "pointA: " << pointA << std::endl; + Point2D pointC; + std::cin >> pointC; + std::cout << "pointC entered: " << pointC << std::endl; + return 0; +} \ No newline at end of file diff --git a/src/etape6/point2d.cpp b/src/etape6/point2d.cpp new file mode 100644 index 0000000..60003dd --- /dev/null +++ b/src/etape6/point2d.cpp @@ -0,0 +1,62 @@ +#include "point2d.h" +#include +#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 }; +} + +std::ostream& operator<<(std::ostream& stream, const Point2D& point) +{ + stream << "x: " << point._x << ", y: " << point._y; + return stream; +} + +std::istream& operator>>(std::istream& stream, Point2D& point) +{ + stream >> point._x; + stream >> point._y; + return stream; +} + +Point2D operator*(int value, const Point2D& point) +{ + return {point._x*value, point._y*value}; +} \ No newline at end of file diff --git a/src/etape6/point2d.h b/src/etape6/point2d.h new file mode 100644 index 0000000..1d42e27 --- /dev/null +++ b/src/etape6/point2d.h @@ -0,0 +1,23 @@ +#ifndef HEADER_POINT2D +#define HEADER_POINT2D + +#include + +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 std::ostream& operator<<(std::ostream& stream, const Point2D& point); + friend std::istream& operator>>(std::istream& stream, Point2D& point); + friend Point2D operator*(int value, const Point2D& point); + protected: + float _x,_y; +}; + +#endif \ No newline at end of file diff --git a/src/etape6/point3d.cpp b/src/etape6/point3d.cpp new file mode 100644 index 0000000..ee6b483 --- /dev/null +++ b/src/etape6/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/etape6/point3d.h b/src/etape6/point3d.h new file mode 100644 index 0000000..97f54ae --- /dev/null +++ b/src/etape6/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