d03 ex01 pas encore tout au point sur les constructors

This commit is contained in:
Hugo LAMY
2022-02-21 17:01:52 +01:00
parent 7a9435b869
commit 985d90bb8c
5 changed files with 95 additions and 99 deletions

View File

@@ -6,17 +6,19 @@
ClapTrap::ClapTrap( std::string name ) {
ClapTrap::_increaseNumber();
_name = name;
_class = _dClass;
_name = name;
_hit = _dHit;
_energy = _dEnergy;
_attack = _dAttack;
_number = getNumber();
_number = ClapTrap::_totalNumber;
std::cout << _class << " " << _name << " nb:" << _number << " created\n";
return;
}
ClapTrap::ClapTrap( ClapTrap const & src ) {
ClapTrap::_increaseNumber();
_class = _dClass;
*this = src;
std::cout << _class << " " << _name << " nb:" << _number << " copied from " << src._class << "-" << src._name << "-" << src._number << "\n";
return;
@@ -39,10 +41,11 @@ ClapTrap & ClapTrap::operator=( ClapTrap const & rhs ) {
if ( this != &rhs )
{
_name = rhs.getName();
_hit = rhs.getHit();
_energy = rhs.getEnergy();
_attack = rhs.getAttack();
_name = rhs.getName();
_number = rhs.getNumber();
}
std::cout << _class << " " << _name << " nb:" << _number << " assigned\n";
@@ -59,7 +62,7 @@ std::string ClapTrap::getClass() const {return _class;}
int ClapTrap::getHit() const {return _hit;}
int ClapTrap::getEnergy() const {return _energy;}
int ClapTrap::getAttack() const {return _attack;}
int ClapTrap::getNumber() const {return ClapTrap::_totalNumber;}
int ClapTrap::getNumber() const {return _number;}
void ClapTrap::_increaseNumber() {ClapTrap::_totalNumber++;}
/*********************************************

View File

@@ -1,80 +1,68 @@
#include "ScavTrap.hpp"
/*
* assign values for default or secondary constructors
*/
void ScavTrap::assignValues( ScavTrap & src ) {
src._class = "ScavTrap";
src._hit = 100;
src._energy = 50;
src._attack = 20;
}
/*
* default constructor
*/
ScavTrap::ScavTrap() {
assignValues(*this);
std::cout << _class << " " << " default construction with number " << _number << "\n";
return;
}
/*
* parameters constructor
*/
/*********************************************
* CONSTRUCTORS
*********************************************/
ScavTrap::ScavTrap( std::string name ) : ClapTrap(name) {
assignValues(*this);
std::cout << _class << " " << _name << " named creation with number " << _number << "\n";
_class = _dClass;
_hit = _dHit;
_energy = _dEnergy;
_attack = _dAttack;
std::cout << _class << " " << _name << " nb:" << _number << " created\n";
return;
}
/*
* destructor
*/
ScavTrap::ScavTrap( ScavTrap const & src ) : ClapTrap() {
_class = _dClass;
*this = src;
std::cout << _class << " " << _name << " nb:" << _number << " copied from " << src._class << "-" << src._name << "-" << src._number << "\n";
return;
}
/*********************************************
* DESTRUCTORS
*********************************************/
ScavTrap::~ScavTrap( void ) {
std::cout << _class << " " << _name << " destructed\n";
std::cout << _class << " " << _name << " nb:" << _number << " destructed\n";
return;
}
/*
* special capacity
*/
void ScavTrap::guardGate() {
std::cout << _class << " " << _name << " entered special mode Gate Keeper\n";
}
/*
* copy constructor
*/
ScavTrap::ScavTrap( ScavTrap const & src ) {
*this = src;
std::cout << _class << " " << _name << " copied\n";
return;
}
/*
* assignement operator
*/
/*********************************************
* OPERATORS
*********************************************/
ScavTrap & ScavTrap::operator=( ScavTrap const & rhs ) {
if ( this != &rhs )
{
this->_hit = rhs.getHit();
this->_energy = rhs.getEnergy();
this->_attack = rhs.getAttack();
this->_class = rhs.getClass();
this->_name = rhs.getName();
this->_number = rhs.getNumber();
_hit = rhs.getHit();
_energy = rhs.getEnergy();
_attack = rhs.getAttack();
_name = rhs.getName();
}
std::cout << _class << " " << _name << " assigned\n";
std::cout << _class << " " << _name << " nb:" << _number << " assigned\n";
return *this;
}
/*********************************************
* PUBLIC MEMBER FUNCTIONS
*********************************************/
void ScavTrap::guardGate() {
std::cout << _class << " " << _name << " entered special mode Gate Keeper\n";
}
/*********************************************
* STATICS
*********************************************/
std::string const ScavTrap::_dName = "robot";
std::string const ScavTrap::_dClass = "ScavTrap";
int const ScavTrap::_dHit = 100;
int const ScavTrap::_dEnergy = 50;
int const ScavTrap::_dAttack = 20;

View File

@@ -9,10 +9,9 @@ class ScavTrap : public ClapTrap {
public:
ScavTrap(); // default/parametric constructor
ScavTrap( std::string name = ScavTrap::_dName );
ScavTrap( ScavTrap const & src ); // copy constructor
~ScavTrap(); // destructor
ScavTrap(std::string name);
ScavTrap & operator=( ScavTrap const & rhs ); // assignement operator
@@ -20,7 +19,12 @@ public:
private:
void assignValues( ScavTrap & src);
static const std::string _dName;
static const std::string _dClass;
static const int _dHit;
static const int _dEnergy;
static const int _dAttack;
static const int _dNumber;
};

View File

@@ -46,47 +46,48 @@ void goAttack(ScavTrap & robot1, ClapTrap & robot2) {
int main() {
ClapTrap robot1("robot1");
ScavTrap robot2("robot2");
ScavTrap robot3("robot3");
ClapTrap robot4("robot4");
// ClapTrap robot1("robot1");
// ScavTrap robot2("robot2");
// ScavTrap robot3("robot3");
// ClapTrap robot4("robot4");
std::cout << "assignement 1:\n";
ScavTrap robotmp1("robot5");
robotmp1.guardGate();
ClapTrap robot6(robotmp1); // PBM : it says it's a ScavTrap but it has no guardGate()...
// robot6.guardGate();
goAttack(robot6, robotmp1);
std::cout << "assignement 2:\n";
ScavTrap robotmp2("robot7");
ScavTrap robot8(robotmp2);
robot8.guardGate();
std::cout << "assignement 3:\n";
ClapTrap robotmp3("robot9"); // PBM : assignation doesn't work...
// ScavTrap robot10(robotmp3);
// robot10.guardGate();
std::cout << "assignement 4:\n";
ClapTrap robotmp4("robot11");
ClapTrap robot12(robotmp4);
// robot12.guardGate();
goAttack(robot1, robot2);
goAttack(robot2, robot1);
goAttack(robot1, robot3);
robot2.guardGate();
goAttack(robot1, robot4);
goAttack(robot4, robot2);
goAttack(robot2, robot3);
goAttack(robot2, robot4);
robot3.guardGate();
goAttack(robot3, robot1);
robot2.guardGate();
goAttack(robot3, robot4);
goAttack(robot2, robot1);
goAttack(robot2, robot4);
goAttack(robot1, robot3);
//std::cout << "assignement 2:\n";
// ScavTrap robotmp2("robot7");
// ScavTrap robot8(robotmp2);
// robot8.guardGate();
//
//std::cout << "assignement 3:\n";
// ClapTrap robotmp3("robot9"); // PBM : assignation doesn't work...
//// ScavTrap robot10(robotmp3);
//// robot10.guardGate();
//
//std::cout << "assignement 4:\n";
// ClapTrap robotmp4("robot11");
// ClapTrap robot12(robotmp4);
//// robot12.guardGate();
//
// goAttack(robot1, robot2);
// goAttack(robot2, robot1);
// goAttack(robot1, robot3);
// robot2.guardGate();
// goAttack(robot1, robot4);
// goAttack(robot4, robot2);
// goAttack(robot2, robot3);
// goAttack(robot2, robot4);
// robot3.guardGate();
// goAttack(robot3, robot1);
// robot2.guardGate();
// goAttack(robot3, robot4);
// goAttack(robot2, robot1);
// goAttack(robot2, robot4);
// goAttack(robot1, robot3);
return 0;
}

Binary file not shown.