diff options
Diffstat (limited to 'cpp04/ex02')
| -rw-r--r-- | cpp04/ex02/AssaultTerminator.cpp | 11 | ||||
| -rw-r--r-- | cpp04/ex02/AssaultTerminator.hpp | 10 | ||||
| -rw-r--r-- | cpp04/ex02/Squad.cpp | 8 | ||||
| -rw-r--r-- | cpp04/ex02/Squad.hpp | 10 | ||||
| -rw-r--r-- | cpp04/ex02/TacticalMarine.cpp | 11 | ||||
| -rw-r--r-- | cpp04/ex02/TacticalMarine.hpp | 10 | ||||
| -rw-r--r-- | cpp04/ex02/main.cpp | 116 |
7 files changed, 138 insertions, 38 deletions
diff --git a/cpp04/ex02/AssaultTerminator.cpp b/cpp04/ex02/AssaultTerminator.cpp index c00a00c..a62c9e4 100644 --- a/cpp04/ex02/AssaultTerminator.cpp +++ b/cpp04/ex02/AssaultTerminator.cpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 15:47:06 by charles #+# #+# */ -/* Updated: 2020/11/12 15:37:48 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 12:19:15 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,11 @@ AssaultTerminator::AssaultTerminator() std::cout << "* teleports from space *" << std::endl; } -AssaultTerminator::AssaultTerminator(AssaultTerminator const& other) { *this = other; } +AssaultTerminator::AssaultTerminator(AssaultTerminator const& other) +{ + std::cout << "* teleports from space *" << std::endl; + *this = other; +} AssaultTerminator& AssaultTerminator::operator=(AssaultTerminator const& other) { @@ -32,8 +36,7 @@ AssaultTerminator::~AssaultTerminator() ISpaceMarine* AssaultTerminator::clone() const { - ISpaceMarine* cloned = new AssaultTerminator(); - *cloned = *this; + ISpaceMarine* cloned = new AssaultTerminator(*this); return cloned; } diff --git a/cpp04/ex02/AssaultTerminator.hpp b/cpp04/ex02/AssaultTerminator.hpp index b2064f1..e1ac9f1 100644 --- a/cpp04/ex02/AssaultTerminator.hpp +++ b/cpp04/ex02/AssaultTerminator.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 15:46:20 by charles #+# #+# */ -/* Updated: 2020/11/12 15:36:54 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 12:18:55 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,10 +24,10 @@ public: AssaultTerminator& operator=(AssaultTerminator const& other); virtual ~AssaultTerminator(); - virtual ISpaceMarine* clone() const; - virtual void battleCry() const; - virtual void rangedAttack() const; - virtual void meleeAttack() const; + virtual ISpaceMarine* clone() const; + virtual void battleCry() const; + virtual void rangedAttack() const; + virtual void meleeAttack() const; private: }; diff --git a/cpp04/ex02/Squad.cpp b/cpp04/ex02/Squad.cpp index d1d988f..1cc59f0 100644 --- a/cpp04/ex02/Squad.cpp +++ b/cpp04/ex02/Squad.cpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 15:08:35 by charles #+# #+# */ -/* Updated: 2020/11/12 15:38:55 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 12:13:57 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,12 @@ Squad::Squad() : m_units(new ISpaceMarine*[0]), m_size(0) {} -Squad::Squad(Squad const& other) { *this = other; } +Squad::Squad(Squad const& other) : m_units(NULL), m_size(0) { *this = other; } Squad& Squad::operator=(Squad const& other) { destroyUnits(); - m_size = other.m_size; + m_size = other.m_size; m_units = new ISpaceMarine*[m_size]; for (int i = 0; i < m_size; i++) m_units[i] = other.m_units[i]->clone(); @@ -57,6 +57,8 @@ int Squad::push(ISpaceMarine* spaceMarine) void Squad::destroyUnits() { + if (m_units == NULL) + return; for (int i = 0; i < m_size; i++) delete m_units[i]; delete [] m_units; diff --git a/cpp04/ex02/Squad.hpp b/cpp04/ex02/Squad.hpp index 9ea5268..48e53cd 100644 --- a/cpp04/ex02/Squad.hpp +++ b/cpp04/ex02/Squad.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 15:07:57 by charles #+# #+# */ -/* Updated: 2020/11/12 15:39:26 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 12:06:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,15 +25,15 @@ public: Squad& operator=(Squad const& other); virtual ~Squad(); - virtual int getCount() const; - virtual ISpaceMarine* getUnit(int n) const; - virtual int push(ISpaceMarine* spaceMarine); + virtual int getCount() const; + virtual ISpaceMarine* getUnit(int n) const; + virtual int push(ISpaceMarine* spaceMarine); private: void destroyUnits(); ISpaceMarine** m_units; - int m_size; + int m_size; }; #endif diff --git a/cpp04/ex02/TacticalMarine.cpp b/cpp04/ex02/TacticalMarine.cpp index df771e8..cf30d2a 100644 --- a/cpp04/ex02/TacticalMarine.cpp +++ b/cpp04/ex02/TacticalMarine.cpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 15:31:43 by charles #+# #+# */ -/* Updated: 2020/11/12 15:37:41 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 12:18:14 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,11 @@ TacticalMarine::TacticalMarine() std::cout << "Tactical Marine ready for battle!" << std::endl; } -TacticalMarine::TacticalMarine(TacticalMarine const& other) { *this = other; } +TacticalMarine::TacticalMarine(TacticalMarine const& other) +{ + std::cout << "Tactical Marine ready for battle!" << std::endl; + *this = other; +} TacticalMarine& TacticalMarine::operator=(TacticalMarine const& other) { @@ -32,8 +36,7 @@ TacticalMarine::~TacticalMarine() ISpaceMarine* TacticalMarine::clone() const { - ISpaceMarine* cloned = new TacticalMarine(); - *cloned = *this; + ISpaceMarine* cloned = new TacticalMarine(*this); return cloned; } diff --git a/cpp04/ex02/TacticalMarine.hpp b/cpp04/ex02/TacticalMarine.hpp index 43722d6..965cb30 100644 --- a/cpp04/ex02/TacticalMarine.hpp +++ b/cpp04/ex02/TacticalMarine.hpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 15:30:48 by charles #+# #+# */ -/* Updated: 2020/11/12 15:38:03 by cacharle ### ########.fr */ +/* Updated: 2020/11/13 12:16:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,10 +24,10 @@ public: TacticalMarine& operator=(TacticalMarine const& other); virtual ~TacticalMarine(); - virtual ISpaceMarine* clone() const; - virtual void battleCry() const; - virtual void rangedAttack() const; - virtual void meleeAttack() const; + virtual ISpaceMarine* clone() const; + virtual void battleCry() const; + virtual void rangedAttack() const; + virtual void meleeAttack() const; private: }; diff --git a/cpp04/ex02/main.cpp b/cpp04/ex02/main.cpp index 137ce46..c53dadb 100644 --- a/cpp04/ex02/main.cpp +++ b/cpp04/ex02/main.cpp @@ -6,7 +6,7 @@ /* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/14 15:50:35 by charles #+# #+# */ -/* Updated: 2020/04/14 15:53:35 by charles ### ########.fr */ +/* Updated: 2020/11/13 12:36:22 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,20 +18,112 @@ int main() { - ISpaceMarine* bob = new TacticalMarine; - ISpaceMarine* jim = new AssaultTerminator; + { + std::cout << "================ SUBJECT MAIN =====================" << std::endl; + ISpaceMarine* bob = new TacticalMarine; + ISpaceMarine* jim = new AssaultTerminator; + + ISquad* vlc = new Squad; + vlc->push(bob); + vlc->push(jim); + for (int i = 0; i < vlc->getCount(); ++i) + { + ISpaceMarine* cur = vlc->getUnit(i); + cur->battleCry(); + cur->rangedAttack(); + cur->meleeAttack(); + } + delete vlc; + } + + std::cout << std::endl; + + { + std::cout << "================ TACTICAL MARINE =====================" << std::endl; + TacticalMarine t; + t.battleCry(); + t.rangedAttack(); + t.meleeAttack(); + TacticalMarine t2(t); + TacticalMarine t3; + t3 = t; + std::cout << "################ INTERFACE" << std::endl; + ISpaceMarine *sm = new TacticalMarine(); + sm->battleCry(); + sm->rangedAttack(); + sm->meleeAttack(); + delete sm; + } + + std::cout << std::endl; - ISquad* vlc = new Squad; - vlc->push(bob); - vlc->push(jim); - for (int i = 0; i < vlc->getCount(); ++i) { - ISpaceMarine* cur = vlc->getUnit(i); - cur->battleCry(); - cur->rangedAttack(); - cur->meleeAttack(); + std::cout << "================ ASSAULT TERMINATOR =====================" << std::endl; + AssaultTerminator a; + a.battleCry(); + a.rangedAttack(); + a.meleeAttack(); + AssaultTerminator a2(a); + AssaultTerminator a3; + a3 = a; + std::cout << "################ INTERFACE" << std::endl; + ISpaceMarine *sm = new AssaultTerminator(); + sm->battleCry(); + sm->rangedAttack(); + sm->meleeAttack(); + delete sm; + } + + std::cout << std::endl; + + { + std::cout << "================ SQUAD =====================" << std::endl; + Squad s; + std::cout << "Count: " << s.getCount() << std::endl; + s.push(new TacticalMarine()); + std::cout << "Count: " << s.getCount() << std::endl; + s.push(new TacticalMarine()); + s.push(new AssaultTerminator()); + s.push(new AssaultTerminator()); + std::cout << "Count: " << s.getCount() << std::endl; + s.getUnit(0)->battleCry(); + s.getUnit(1)->battleCry(); + s.getUnit(2)->battleCry(); + s.getUnit(3)->battleCry(); + s.getUnit(3)->rangedAttack(); + s.getUnit(3)->meleeAttack(); + s.getUnit(0)->rangedAttack(); + s.getUnit(0)->meleeAttack(); + std::cout << s.getUnit(4) << std::endl; + std::cout << s.getUnit(-1) << std::endl; + + std::cout << "################ COPY" << std::endl; + Squad s2(s); + std::cout << "Copy Count: " << s2.getCount() << std::endl; + s2.push(new TacticalMarine()); + std::cout << "Copy Count: " << s2.getCount() << std::endl; + std::cout << "Origin Count: " << s.getCount() << std::endl; + + std::cout << "################ ASSIGN" << std::endl; + Squad s3; + s3 = s; + std::cout << "Copy Count: " << s3.getCount() << std::endl; + s3.push(new TacticalMarine()); + std::cout << "Copy Count: " << s3.getCount() << std::endl; + std::cout << "Origin Count: " << s.getCount() << std::endl; + + std::cout << "################ INTERFACE" << std::endl; + ISquad *si = new Squad(); + si->push(new TacticalMarine()); + std::cout << si->getCount() << std::endl; + si->getUnit(0)->battleCry(); + si->push(new AssaultTerminator()); + std::cout << si->getCount() << std::endl; + si->getUnit(1)->battleCry(); + delete si; + + std::cout << "################ DESTROY" << std::endl; } - delete vlc; return 0; } |
