TP1, Exercice 1.

This commit is contained in:
Yohan Boujon 2023-12-06 17:02:52 +01:00
parent f825c6e5f5
commit 7db3257e4d
5 changed files with 119 additions and 23 deletions

24
tp/include/coloriable.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef HEADER_COLORIABLE
#define HEADER_COLORIABLE
#include <string>
class Coloriable {
protected:
std::string couleur;
public:
Coloriable(std::string coul)
{
couleur = coul;
}
std::string getCouleur()
{
return couleur;
}
void setCouleur(std::string coul)
{
couleur = coul;
}
};
#endif // HEADER_COLORIABLE

View file

@ -5,7 +5,7 @@
class Figure { class Figure {
public: public:
virtual int perimetre()=0; virtual float perimetre()=0;
virtual std::string afficherCaracteristiques()=0; virtual std::string afficherCaracteristiques()=0;
}; };

View file

@ -1,24 +1,30 @@
#ifndef HEADER_POLYGONE #ifndef HEADER_POLYGONE
#define HEADER_POLYGONE #define HEADER_POLYGONE
#include "figure.h"
#include <sys/types.h> #include <sys/types.h>
#include "figure.h"
#include "coloriable.h"
class Polygone : public Figure { class Polygone : public Figure {
public: public:
Polygone(u_int64_t nb_cotes); Polygone(u_int64_t nb_cotes);
~Polygone();
virtual std::string afficherCaracteristiques() override; virtual std::string afficherCaracteristiques() override;
virtual int perimetre() override; virtual float perimetre() override;
static uint getPolyCount() { return _polycount; };
protected: protected:
u_int64_t _nbcotes; u_int64_t _nbcotes;
private:
static uint _polycount;
}; };
class Rectangle : public Polygone { class Rectangle : public Polygone {
public: public:
Rectangle(uint hauteur, uint longueur); Rectangle(uint hauteur, uint longueur);
virtual std::string afficherCaracteristiques() override; virtual std::string afficherCaracteristiques() override;
virtual int perimetre() override; virtual float perimetre() override;
protected: protected:
uint _hauteur, _longueur; uint _hauteur, _longueur;
@ -29,15 +35,27 @@ public:
Carre(uint cote); Carre(uint cote);
}; };
class TriangleEquilateral : public Polygone class TriangleEquilateral : public Polygone {
{
public: public:
TriangleEquilateral(uint cote); TriangleEquilateral(uint cote);
virtual std::string afficherCaracteristiques() override; virtual std::string afficherCaracteristiques() override;
virtual int perimetre() override; virtual float perimetre() override;
private: private:
uint _cote; uint _cote;
float _angle; float _angle;
}; };
class Cercle : public Polygone, public Coloriable {
public:
Cercle(uint rayon, const std::string& couleur);
virtual std::string afficherCaracteristiques() override;
virtual float perimetre() override;
uint getRayon();
void setRayon(uint rayon);
private:
uint _rayon;
};
#endif // HEADER_POLYGONE #endif // HEADER_POLYGONE

View file

@ -1,14 +1,46 @@
#include "../include/polygone.h" #include "../include/polygone.h"
#include <cstddef>
#include <iostream> #include <iostream>
/**
* @brief Fonction de test utilisées pour les question 1 à 5
*/
void test();
int main(void) int main(void)
{
// Le polymorphisme ne fonctionne qu'avec des pointeurs
Polygone* polyTab[3] = { new Carre(2), new Cercle(5, "rouge"), new TriangleEquilateral(3) };
std::cout << "Polycount: " << Polygone::getPolyCount() << std::endl;
for (size_t i = 0; i < Polygone::getPolyCount(); i++) {
std::cout << polyTab[i]->afficherCaracteristiques() << std::endl;
std::cout << "Perimetre: " << polyTab[i]->perimetre() << std::endl;
if (i == 1) {
auto cercle = static_cast<Cercle*>(polyTab[i]);
std::cout << "Couleur du cercle: " << cercle->getCouleur() << std::endl;
}
}
// Suppression de chaque Polygone dans le tableau
while (Polygone::getPolyCount() > 0)
delete polyTab[Polygone::getPolyCount() - 1];
// ! WARNING: Potential memory leak (je ne sais pas pourquoi...)
return 0;
}
void test()
{ {
Polygone coolPoly(5); Polygone coolPoly(5);
std::cout << coolPoly.afficherCaracteristiques() << std::endl; std::cout << coolPoly.afficherCaracteristiques() << std::endl;
Rectangle rect(4,8); Rectangle rect(4, 8);
std::cout << rect.afficherCaracteristiques() << std::endl; std::cout << rect.afficherCaracteristiques() << std::endl;
Carre car(2); Carre car(2);
std::cout << car.afficherCaracteristiques() << std::endl; std::cout << car.afficherCaracteristiques() << std::endl;
TriangleEquilateral tri(5); TriangleEquilateral tri(5);
std::cout << tri.afficherCaracteristiques() << std::endl; std::cout << tri.afficherCaracteristiques() << std::endl;
Cercle cer(2, "bleu");
std::cout << cer.afficherCaracteristiques() << std::endl;
std::cout << "Perimetre du cercle: " << cer.perimetre() << std::endl;
std::cout << "On change le rayon de 5" << std::endl;
cer.setRayon(5);
std::cout << "Perimetre du cercle: " << cer.perimetre() << std::endl;
} }

View file

@ -1,10 +1,22 @@
#include "../include/polygone.h" #include "../include/polygone.h"
#include <string> #include "coloriable.h"
#include <cmath>
#include <sys/types.h> #include <sys/types.h>
#include <numbers>
#include <string>
uint Polygone::_polycount = 0;
Polygone::Polygone(u_int64_t nb_cotes) Polygone::Polygone(u_int64_t nb_cotes)
: _nbcotes(nb_cotes) : _nbcotes(nb_cotes)
{ {
_polycount++;
}
Polygone::~Polygone()
{
_polycount--;
} }
std::string Polygone::afficherCaracteristiques() std::string Polygone::afficherCaracteristiques()
@ -12,10 +24,7 @@ std::string Polygone::afficherCaracteristiques()
return std::string("Nombre de cotes : " + std::to_string(_nbcotes)); return std::string("Nombre de cotes : " + std::to_string(_nbcotes));
} }
int Polygone::perimetre() float Polygone::perimetre() { return 0; }
{
return 0;
}
Rectangle::Rectangle(uint hauteur, uint longueur) Rectangle::Rectangle(uint hauteur, uint longueur)
: Polygone(4) : Polygone(4)
@ -26,13 +35,10 @@ Rectangle::Rectangle(uint hauteur, uint longueur)
std::string Rectangle::afficherCaracteristiques() std::string Rectangle::afficherCaracteristiques()
{ {
return Polygone::afficherCaracteristiques()+std::string("\tPerimetre :"+std::to_string(perimetre())); return Polygone::afficherCaracteristiques() + std::string("\tPerimetre :" + std::to_string(perimetre()));
} }
int Rectangle::perimetre() float Rectangle::perimetre() { return (2 * _longueur) + (2 * _hauteur); }
{
return (2*_longueur)+(2*_hauteur);
}
Carre::Carre(uint cote) Carre::Carre(uint cote)
: Rectangle(cote, cote) : Rectangle(cote, cote)
@ -43,14 +49,30 @@ TriangleEquilateral::TriangleEquilateral(uint cote)
: Polygone(3) : Polygone(3)
, _cote(cote) , _cote(cote)
, _angle(60.0f) , _angle(60.0f)
{} {
}
std::string TriangleEquilateral::afficherCaracteristiques() std::string TriangleEquilateral::afficherCaracteristiques()
{ {
return Polygone::afficherCaracteristiques()+std::string("\tPerimetre :"+std::to_string(perimetre())+"\t Angles :"+std::to_string(_angle)); return Polygone::afficherCaracteristiques() + std::string("\tPerimetre :" + std::to_string(perimetre()) + "\t Angles :" + std::to_string(_angle));
} }
int TriangleEquilateral::perimetre() float TriangleEquilateral::perimetre() { return 3 * _cote; }
Cercle::Cercle(uint rayon, const std::string& couleur)
: Polygone(INFINITY)
, Coloriable(couleur)
, _rayon(rayon)
{ {
return 3*_cote;
} }
std::string Cercle::afficherCaracteristiques()
{
return std::string("Rayon: " + std::to_string(_rayon));
}
float Cercle::perimetre() { return 2 * std::numbers::pi * _rayon; }
uint Cercle::getRayon() { return _rayon; }
void Cercle::setRayon(uint rayon) { _rayon = rayon; }