------------------------------------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 first class /* * Arme.h * * Created on: 11 déc. 2020 * Author: vincent */ #ifndef ARME_H_ #define ARME_H_ #include <iostream> #include <string> class Arme { public: Arme(); ~Arme(); Arme(std::string nom, int degats); void changer(std::string nom, int degats); void afficher() const; int getDegats() const; private : std::string m_nom; int m_degats; }; #endif /* ARME_H_ */
------------------------------------------------
------------------------------------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 /* * Arme.cpp * * Created on: 11 déc. 2020 * Author: vincent */ #include "Arme.h" using namespace std; Arme::Arme() : m_nom ("Epée rouillée"),m_degats (10) { } Arme::Arme(string nom, int degats) : m_nom (nom),m_degats (degats) { } void Arme::changer(string nom, int degats) { m_nom= nom; m_degats = degats; } void Arme::afficher()const { cout<<" Arme : " << m_nom << "(Degats : "<< m_degats <<")"<< endl; } Arme::~Arme() { }
------------------------------------------------
seconde class
------------------------------------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 /* * Personnage.h * * Created on: 11 déc. 2020 * Author: vincent */ #ifndef PERSONNAGE_H_ #define PERSONNAGE_H_ #include <iostream> #include <string> #include "Arme.h" class Personnage { public: Personnage(); Personnage(std::string monArme, int degatsArme); ~Personnage(); void recevoirDegats(int nbDegats); void attaquer(Personnage &cible); void boirePotionDeVie (int quantitePotion); void changerArme(std::string nomNouvelleArme, int degatsNouvelleArme); bool estVivant() const; void afficherEtat() const; private: int m_vie; int m_mana; Arme *m_arme; //pointeur }; #endif /* PERSONNAGE_H_ */
------------------------------------------------
-------------------------------------------------------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 /* * Personnage.cpp * * Created on: 11 déc. 2020 * Author: vincent */ #include "Personnage.h" using namespace std; void Personnage::recevoirDegats(int nbDegats) { m_vie -= nbDegats; if (m_vie < 0) { m_vie = 0; } } int Arme::getDegats() const { return m_degats; } void Personnage::attaquer(Personnage &cible) { cible.recevoirDegats(m_arme->getDegats()); } void Personnage::boirePotionDeVie (int quantitePotion) { m_vie += quantitePotion; if (m_vie > 100) { m_vie = 100; } } void Personnage::changerArme(string nomNouvelleArme, int degatsNouvelleArme) { m_arme->changer(nomNouvelleArme, degatsNouvelleArme); //pas dans le bouquin demander par le compilateur //m_nomArme = nomNouvelleArme; //m_degatsArme = degatsNouvelleArme; } bool Personnage::estVivant() const { if (m_vie > 0) { return true; } else { return false; } } Personnage::Personnage() :m_vie(100),m_mana(100),m_arme(0) { // TODO Auto-generated constructor stub } Personnage::Personnage(string nomArme, int degatsArme): m_vie(100), m_mana(100),m_arme(0) { //nomArme, degatsArme m_arme = new Arme(nomArme,degatsArme ); } void Personnage::afficherEtat() const { cout << "Vie : " << m_vie << endl; cout << "Mana : " << m_mana << endl; m_arme->afficher(); } Personnage::~Personnage() { delete m_arme; }
main
-------------------------------------------------------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 #include <iostream> #include <string> #include "Personnage.h" using namespace std; int main() { // Création des personnages Personnage david, goliath("Epée aiguisée", 20); // Au combat ! goliath.attaquer(david); david.boirePotionDeVie(20); goliath.attaquer(david); david.attaquer(goliath); goliath.changerArme("Double hache tranchante vénéneuse de la mort", 40); goliath.attaquer(david); // Temps mort ! Voyons voir la vie de chacun... cout << "David" << endl; david.afficherEtat(); cout << endl << "Goliath" << endl; goliath.afficherEtat(); return 0; }
Partager