Cours:DevoirPOOcpp2 : Différence entre versions
Ligne 1 : | Ligne 1 : | ||
<accesscontrol>Acces:Prof</accesscontrol> | <accesscontrol>Acces:Prof</accesscontrol> | ||
− | = | + | =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 31 : | Ligne 33 : | ||
{ | { | ||
string message; | string message; | ||
+ | message.append("\nForce : "); | ||
+ | message.append(to_string(force)); | ||
+ | message.append("\n"); | ||
message.append("\nPoints de vie : "); | message.append("\nPoints de vie : "); | ||
message.append(to_string(pointsVie)); | message.append(to_string(pointsVie)); | ||
Ligne 37 : | Ligne 42 : | ||
} | } | ||
</source> | </source> | ||
− | Programme principal | + | |
+ | ==Programme principal== | ||
<source lang=cpp> | <source lang=cpp> | ||
#include <iostream> | #include <iostream> | ||
Ligne 45 : | Ligne 51 : | ||
int main() | int main() | ||
{ | { | ||
− | Personnage asterix; | + | Personnage asterix(20); |
− | cout << asterix.toString(); | + | Personnage obelix(100); |
+ | Personnage romain(10): | ||
+ | |||
+ | cout << asterix.toString() << endl; | ||
+ | cout << obelix.toString() << endl; | ||
+ | cout << romain.toString() << endl; | ||
} | } | ||
− | |||
</source> | </source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Version du 1 octobre 2021 à 08:51
Il s’agit d’une page protégée.
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("\nForce : ");
message.append(to_string(force));
message.append("\n");
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() << endl;
cout << obelix.toString() << endl;
cout << romain.toString() << endl;
}