Cours:DevoirPOOcpp2 : Différence entre versions
(Page créée avec « <accesscontrol>Acces:Prof</accesscontrol> =Ébauche de nouveau DS= 1. On donne une classe Personnage a) lister les attributs, méthodes, etc ... b) créer un projet c) a... ») |
|||
(15 révisions intermédiaires par 2 utilisateurs non affichées) | |||
Ligne 1 : | Ligne 1 : | ||
− | |||
− | |||
− | + | =POO - Examen n°1 - octobre= | |
− | |||
− | |||
− | |||
− | =Classe Personnage= | + | ==Classe Personnage== |
+ | Déclarations : | ||
<source lang=cpp> | <source lang=cpp> | ||
− | #include < | + | #include <string> |
using namespace std; | using namespace std; | ||
class Personnage | class Personnage | ||
{ | { | ||
− | + | public: | |
− | + | Personnage(int); | |
− | + | string toString(); | |
− | + | ||
− | + | private : | |
− | + | int force; | |
+ | int pointsVie; | ||
}; | }; | ||
+ | |||
+ | </source> | ||
+ | Définitions : | ||
+ | <source lang=cpp> | ||
+ | Personnage::Personnage(int _force) | ||
+ | { | ||
+ | pointsVie = 100; | ||
+ | force = _force; | ||
+ | } | ||
+ | |||
+ | string Personnage::toString() | ||
+ | { | ||
+ | string message; | ||
+ | message.append("\nPoints de vie : "); | ||
+ | message.append(to_string(pointsVie)); | ||
+ | message.append("\n"); | ||
+ | return message; | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | ==Programme principal== | ||
+ | <source lang=cpp> | ||
+ | #include <iostream> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | Personnage asterix(20); | ||
+ | Personnage obelix(100); | ||
+ | Personnage romain(10); | ||
+ | |||
+ | cout << asterix.toString(); | ||
+ | cout << obelix.toString(); | ||
+ | cout << romain.toString(); | ||
+ | } | ||
</source> | </source> |
Version actuelle datée du 28 septembre 2023 à 11:08
POO - Examen n°1 - octobre
Classe Personnage
Déclarations :
#include <string>
using namespace std;
class Personnage
{
public:
Personnage(int);
string toString();
private :
int force;
int pointsVie;
};
Définitions :
Personnage::Personnage(int _force)
{
pointsVie = 100;
force = _force;
}
string Personnage::toString()
{
string message;
message.append("\nPoints de vie : ");
message.append(to_string(pointsVie));
message.append("\n");
return message;
}
Programme principal
#include <iostream>
using namespace std;
int main()
{
Personnage asterix(20);
Personnage obelix(100);
Personnage romain(10);
cout << asterix.toString();
cout << obelix.toString();
cout << romain.toString();
}