Cours:DevoirPOOcpp2 : Différence entre versions
(10 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 : | Déclarations : | ||
Ligne 14 : | Ligne 13 : | ||
{ | { | ||
public: | public: | ||
− | Personnage(); | + | Personnage(int); |
string toString(); | string toString(); | ||
private : | private : | ||
+ | int force; | ||
int pointsVie; | int pointsVie; | ||
}; | }; | ||
+ | |||
</source> | </source> | ||
Définitions : | Définitions : | ||
<source lang=cpp> | <source lang=cpp> | ||
− | Personnage::Personnage() | + | Personnage::Personnage(int _force) |
{ | { | ||
− | pointsVie=100; | + | pointsVie = 100; |
+ | force = _force; | ||
} | } | ||
Ligne 37 : | Ligne 39 : | ||
} | } | ||
</source> | </source> | ||
− | Programme principal | + | |
+ | ==Programme principal== | ||
<source lang=cpp> | <source lang=cpp> | ||
#include <iostream> | #include <iostream> | ||
Ligne 45 : | Ligne 48 : | ||
int main() | int main() | ||
{ | { | ||
− | Personnage asterix; | + | Personnage asterix(20); |
+ | Personnage obelix(100); | ||
+ | Personnage romain(10); | ||
+ | |||
cout << asterix.toString(); | 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();
}