Programmation objet en c++
On a fait un petit exercice pour voir les bases de la programmation objet en c++.
Le sujet
Créer un objet "wall" avec deux attributs "texture" et "color" tous les deux de type string. Créer les accesseurs et les mutateurs correspondants à ces deux attributs puis créer un programme de test (dans un main.cpp) afin de vérifier le bon fonctionnement des méthodes.
Solutions
wall.h
#include <string>
using namespace std; // pour le type string
class wall
{
private:
string color;
string texture;
public:
// constructeur
wall();
// accesseurs
string get_color();
string get_texture();
// mutateurs
void set_color(string color);
void set_texture(string texture);
};
wall.cpp
#include "wall.h"
wall::wall() {};
string wall::get_color()
{
return this->color;
}
string wall::get_texture()
{
return this->texture;
}
void wall::set_color(string _color)
{
this->color = _color;
}
void wall::set_texture(string _texture)
{
this->texture = _texture;
}
main.cpp
/*
* compilation et execution
* g++ *.cpp -o test && ./test
*/
#include <iostream>
#include "wall.h"
int main()
{
/* on test notre objet */
wall *w = new wall();
w->set_color("red");
w->set_texture("wood");
std::cout<<"Couleur : " << w->get_color() << std::endl;
std::cout<<"Texture : " << w->get_texture() << std::endl;
return 0;
}