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/11 04:38:18 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
27#include "ClapTrap.hpp"
28#include <iostream>
29
33ClapTrap::ClapTrap() : name("Default"), hitPoints(10), energyPoints(10), attackDamage(0) {
34 std::cout << "ClapTrap Default constructor called" << std::endl;
35}
36
41ClapTrap::ClapTrap(const std::string &name) : name(name), hitPoints(10), energyPoints(10), attackDamage(0) {
42 std::cout << "ClapTrap String constructor called for " << name << std::endl;
43}
44
49ClapTrap::ClapTrap(const ClapTrap &other)
50 : name(other.name), hitPoints(other.hitPoints), energyPoints(other.energyPoints), attackDamage(other.attackDamage) {
51 std::cout << "ClapTrap Copy constructor called for " << name << std::endl;
52}
53
57ClapTrap::~ClapTrap() { std::cout << "ClapTrap Destructor called for " << name << std::endl; }
58
65 std::cout << "ClapTrap Assignation operator called for " << name << std::endl;
66 if (this != &other) {
67 name = other.name;
68 hitPoints = other.hitPoints;
69 energyPoints = other.energyPoints;
70 attackDamage = other.attackDamage;
71 }
72 return *this;
73}
74
79void ClapTrap::attack(const std::string &target) {
80 if (hitPoints > 0 && energyPoints > 0) {
81 std::cout << "ClapTrap " << name << " attacks " << target << ", causing " << attackDamage
82 << " points of damage!" << std::endl;
83 energyPoints--;
84 } else if (hitPoints == 0) {
85 std::cout << "ClapTrap " << name << " is out of hit points and cannot attack!" << std::endl;
86 } else {
87 std::cout << "ClapTrap " << name << " is out of energy points and cannot attack!" << std::endl;
88 }
89}
90
95void ClapTrap::takeDamage(unsigned int amount) {
96 if (hitPoints == 0) {
97 std::cout << "ClapTrap " << name << " is already out of hit points!" << std::endl;
98 return;
99 }
100 std::cout << "ClapTrap " << name << " takes " << amount << " points of damage!" << std::endl;
101 if (amount >= hitPoints) {
102 hitPoints = 0;
103 std::cout << "ClapTrap " << name << " has died!" << std::endl;
104 } else {
105 hitPoints -= amount;
106 std::cout << "ClapTrap " << name << " has " << hitPoints << " hit points remaining." << std::endl;
107 }
108}
109
114void ClapTrap::beRepaired(unsigned int amount) {
115 if (hitPoints > 0 && energyPoints > 0) {
116 std::cout << "ClapTrap " << name << " repairs itself, regaining " << amount << " hit points!" << std::endl;
117 hitPoints += amount;
118 energyPoints--;
119 std::cout << "ClapTrap " << name << " now has " << hitPoints << " hit points and " << energyPoints
120 << " energy points." << std::endl;
121 } else if (hitPoints == 0) {
122 std::cout << "ClapTrap " << name << " cannot be repaired as it has no hit points!" << std::endl;
123 } else {
124 std::cout << "ClapTrap " << name << " is out of energy points and cannot repair!" << std::endl;
125 }
126}
127
132const std::string &ClapTrap::getName() const { return name; }
133
138unsigned int ClapTrap::getHitPoints() const { return hitPoints; }
139
144unsigned int ClapTrap::getEnergyPoints() const { return energyPoints; }
145
150unsigned 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.