Kvíz: Útok v cyklu
Tento program má každé příšeře ubrat 10 životů a pak vypsat 20 a 40, ale nepřeloží se. Klikněte na řádek s chybou.
1#include <iostream>
2#include <string>
3#include <vector>
4
5class Prisera {
6private:
7 std::string jmeno;
8 int zivoty;
9
10public:
11 Prisera(std::string jmenoPrisery, int pocatecniZivoty) {
12 jmeno = jmenoPrisery;
13 zivoty = pocatecniZivoty;
14 }
15
16 void uberZivoty(int pocet) {
17 zivoty -= pocet;
18 }
19
20 int getZivoty() const {
21 return zivoty;
22 }
23};
24
25int main() {
26 std::vector<Prisera> prisery = {Prisera("Skřet", 30), Prisera("Ork", 50)};
27 for (const Prisera& prisera : prisery) {
28 prisera.uberZivoty(10);
29 }
30 for (const Prisera& prisera : prisery) {
31 std::cout << prisera.getZivoty() << std::endl;
32 }
33 return 0;
34}