Kvíz: Konstruktory
1#include <iostream>
2#include <string>
3
4class Prisera {
5private:
6 std::string jmeno;
7 int zivoty;
8
9public:
10 Prisera(std::string jmenoPrisery, int pocatecniZivoty) {
11 jmeno = jmenoPrisery;
12 zivoty = pocatecniZivoty;
13 std::cout << jmeno << " přichází" << std::endl;
14 }
15
16 void uberZivoty(int pocet) {
17 zivoty -= pocet;
18 if (zivoty < 0) {
19 zivoty = 0;
20 }
21 }
22
23 int getZivoty() const {
24 return zivoty;
25 }
26};
27
28int main() {
29 Prisera skret("Skřet", 30);
30 Prisera ork("Ork", 50);
31 skret.uberZivoty(20);
32 ork.uberZivoty(60);
33 skret.uberZivoty(5);
34 std::cout << skret.getZivoty() << " " << ork.getZivoty() << std::endl;
35 return 0;
36}