CPP03 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
ClapTrap.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ClapTrap.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/05/08 12:48:46 by kamitsui #+# #+# */
9/* Updated: 2025/05/15 13:06:48 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
27#include "ClapTrap.hpp"
28#include <iostream>
29
34 : name("Default"), hitPoints(defaultHitPoints), energyPoints(defaultEnergyPoints),
35 attackDamage(defaultAttackDamage) {
36 std::cout << "ClapTrap Default constructor called" << std::endl;
37}
38
43ClapTrap::ClapTrap(const std::string &newName)
44 : name(newName), hitPoints(defaultHitPoints), energyPoints(defaultEnergyPoints), attackDamage(defaultAttackDamage) {
45 std::cout << "ClapTrap String constructor called for " << name << std::endl;
46}
47
52ClapTrap::ClapTrap(const ClapTrap &other)
53 : name(other.name), hitPoints(other.hitPoints), energyPoints(other.energyPoints), attackDamage(other.attackDamage) {
54 std::cout << "ClapTrap Copy constructor called for " << name << std::endl;
55}
56
67ClapTrap::ClapTrap(const std::string &newName, unsigned int hp, unsigned int ep, unsigned int ad)
68 : name(newName), hitPoints(hp), energyPoints(ep), attackDamage(ad) {
69 std::cout << "ClapTrap Parameterized constructor called for " << name << std::endl;
70}
71
75ClapTrap::~ClapTrap() { std::cout << "ClapTrap Destructor called for " << name << std::endl; }
76
83 std::cout << "ClapTrap Assignation operator called for " << name << std::endl;
84 if (this != &other) {
85 name = other.name;
86 hitPoints = other.hitPoints;
87 energyPoints = other.energyPoints;
88 attackDamage = other.attackDamage;
89 }
90 return *this;
91}
92
97void ClapTrap::attack(const std::string &target) {
98 if (hitPoints > 0 && energyPoints > 0) {
99 std::cout << "ClapTrap " << name << " attacks " << target << ", causing " << attackDamage
100 << " points of damage!" << std::endl;
101 energyPoints--;
102 } else if (hitPoints == 0) {
103 std::cout << "ClapTrap " << name << " is out of hit points and cannot attack!" << std::endl;
104 } else {
105 std::cout << "ClapTrap " << name << " is out of energy points and cannot attack!" << std::endl;
106 }
107}
108
113void ClapTrap::takeDamage(unsigned int amount) {
114 if (hitPoints == 0) {
115 std::cout << "ClapTrap " << name << " is already out of hit points!" << std::endl;
116 return;
117 }
118 std::cout << "ClapTrap " << name << " takes " << amount << " points of damage!" << std::endl;
119 if (amount >= hitPoints) {
120 hitPoints = 0;
121 std::cout << "ClapTrap " << name << " has died!" << std::endl;
122 } else {
123 hitPoints -= amount;
124 std::cout << "ClapTrap " << name << " has " << hitPoints << " hit points remaining." << std::endl;
125 }
126}
127
132void ClapTrap::beRepaired(unsigned int amount) {
133 if (hitPoints > 0 && energyPoints > 0) {
134 std::cout << "ClapTrap " << name << " repairs itself, regaining " << amount << " hit points!" << std::endl;
135 hitPoints += amount;
136 energyPoints--;
137 std::cout << "ClapTrap " << name << " now has " << hitPoints << " hit points and " << energyPoints
138 << " energy points." << std::endl;
139 } else if (hitPoints == 0) {
140 std::cout << "ClapTrap " << name << " cannot be repaired as it has no hit points!" << std::endl;
141 } else {
142 std::cout << "ClapTrap " << name << " is out of energy points and cannot repair!" << std::endl;
143 }
144}
145
150const std::string &ClapTrap::getName() const { return name; }
151
156unsigned int ClapTrap::getHitPoints() const { return hitPoints; }
157
162unsigned int ClapTrap::getEnergyPoints() const { return energyPoints; }
163
168unsigned int ClapTrap::getAttackDamage() const { return attackDamage; }
Represents a basic robot character.
Definition ClapTrap.hpp:34
void beRepaired(unsigned int amount)
beRepaired function implementation.
Definition ClapTrap.cpp:108
unsigned int getHitPoints() const
Gets the current hit points of the ClapTrap.
Definition ClapTrap.cpp:132
unsigned int getAttackDamage() const
Gets the attack damage of the ClapTrap.
Definition ClapTrap.cpp:144
ClapTrap()
Default constructor implementation.
Definition ClapTrap.cpp:27
void takeDamage(unsigned int amount)
takeDamage function implementation.
Definition ClapTrap.cpp:89
void attack(const std::string &target)
Attack function implementation.
Definition ClapTrap.cpp:73
ClapTrap & operator=(const ClapTrap &other)
Copy assignment operator implementation.
Definition ClapTrap.cpp:58
~ClapTrap()
Destructor implementation.
Definition ClapTrap.cpp:51
const std::string & getName() const
Gets the name of the ClapTrap.
Definition ClapTrap.cpp:126
unsigned int getEnergyPoints() const
Gets the current energy points of the ClapTrap.
Definition ClapTrap.cpp:138
T endl(T... args)
Header file for the ClapTrap class.