add example for "default constructor, copy constructor, destructor, if you need one you'll need all"
add smart pointer add pointer pointer
This commit is contained in:
@@ -187,6 +187,7 @@
|
||||
<ClCompile Include="main.cpp" />
|
||||
<ClCompile Include="Monster.cpp" />
|
||||
<ClCompile Include="Ork.cpp" />
|
||||
<ClCompile Include="Weapon.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Entity.h" />
|
||||
@@ -194,6 +195,7 @@
|
||||
<ClInclude Include="Monster.h" />
|
||||
<ClInclude Include="Ork.h" />
|
||||
<ClInclude Include="Vector3.h" />
|
||||
<ClInclude Include="Weapon.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -27,6 +27,9 @@
|
||||
<ClCompile Include="Ork.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Weapon.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Entity.h">
|
||||
@@ -44,5 +47,8 @@
|
||||
<ClInclude Include="Vector3.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Weapon.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -3,16 +3,19 @@
|
||||
|
||||
Entity::Entity()
|
||||
{
|
||||
|
||||
std::cout << "Create Entity" << std::endl;
|
||||
}
|
||||
|
||||
Entity::Entity(std::string name) : name(name)
|
||||
{
|
||||
std::cout << "Create Entity with name" << std::endl;
|
||||
|
||||
//this->name = name;
|
||||
}
|
||||
|
||||
Entity::~Entity()
|
||||
{
|
||||
std::cout << "Destroy Entity" << std::endl;
|
||||
}
|
||||
|
||||
std::string Entity::ToString()
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
// only for presentation
|
||||
#include <iostream>
|
||||
|
||||
class ICompareable
|
||||
{
|
||||
public:
|
||||
virtual ~ICompareable() {}
|
||||
// only for presentation
|
||||
ICompareable() { std::cout << "Create IComparable" << std::endl; }
|
||||
|
||||
virtual ~ICompareable() { std::cout << "Destroy IComparable" << std::endl; }
|
||||
|
||||
virtual int Compare(ICompareable* other) = 0;
|
||||
};
|
||||
@@ -2,13 +2,40 @@
|
||||
|
||||
#include "Monster.h"
|
||||
|
||||
Monster::Monster(int strength) : Entity("My Monster"), _strength(strength)
|
||||
Monster::Monster() : Entity("My Monster"), _pWeapon(new Weapon())
|
||||
{
|
||||
std::cout << "Create Monster" << std::endl;
|
||||
}
|
||||
|
||||
Monster::Monster(const Monster& other) : Entity(other), ICompareable(other)
|
||||
{
|
||||
std::cout << "Create Copy of a Monster" << std::endl;
|
||||
|
||||
// shallow copy
|
||||
//this->_pWeapon = other._pWeapon;
|
||||
this->_strength = other._strength;
|
||||
this->_speed = other._speed;
|
||||
|
||||
// deep copy of internal pointers
|
||||
this->_pWeapon = new Weapon(*other._pWeapon);
|
||||
}
|
||||
|
||||
Monster::Monster(int strength) : Entity("My Monster"), _strength(strength), _pWeapon(new Weapon())
|
||||
{
|
||||
std::cout << "Create Monster with strength" << std::endl;
|
||||
|
||||
this->name = "My Monster";
|
||||
}
|
||||
|
||||
Monster::~Monster()
|
||||
{
|
||||
std::cout << "Destroy Monster" << std::endl;
|
||||
|
||||
if (_pWeapon != nullptr)
|
||||
{
|
||||
delete _pWeapon;
|
||||
_pWeapon = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
//Monster Monster::Create(Monsters type)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "Entity.h"
|
||||
#include "ICompareable.h"
|
||||
#include "Weapon.h"
|
||||
|
||||
//enum Monsters
|
||||
//{
|
||||
@@ -12,6 +14,9 @@
|
||||
class Monster : public Entity, public ICompareable
|
||||
{
|
||||
public:
|
||||
// internal pointers often requires a default & copy constructor and a destructor
|
||||
Monster();
|
||||
Monster(const Monster& other);
|
||||
Monster(int strength);
|
||||
virtual ~Monster();
|
||||
|
||||
@@ -22,6 +27,8 @@ public:
|
||||
int Compare(ICompareable* other) override;
|
||||
|
||||
protected:
|
||||
Weapon* _pWeapon = nullptr;
|
||||
|
||||
int _strength = 0;
|
||||
float _speed = 0.0f;
|
||||
};
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
#include "Ork.h"
|
||||
#include <iostream>
|
||||
|
||||
Ork::Ork() : Monster(10)
|
||||
{
|
||||
std::cout << "Create Ork" << std::endl;
|
||||
|
||||
//this->_strength = 10;
|
||||
this->name = "My Ork";
|
||||
}
|
||||
|
||||
Ork::Ork(const Ork& other) : Monster(other._strength)
|
||||
Ork::Ork(const Ork& other) : Monster(other)
|
||||
{
|
||||
this->name = other.name;
|
||||
this->_speed = other._speed;
|
||||
std::cout << "Create a copy of an Ork" << std::endl;
|
||||
//this->name = other.name;
|
||||
//this->_speed = other._speed;
|
||||
}
|
||||
|
||||
Ork::~Ork()
|
||||
{
|
||||
std::cout << "Destroy Ork" << std::endl;
|
||||
}
|
||||
|
||||
void Ork::Move()
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
#include "Weapon.h"
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
class Weapon
|
||||
{
|
||||
};
|
||||
|
||||
+25
-21
@@ -8,38 +8,42 @@ using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
Entity entity; // this is an entity instance
|
||||
entity.name = "My Entity";
|
||||
//Entity entity; // this is an entity instance
|
||||
//entity.name = "My Entity";
|
||||
|
||||
//Monster monster; // can't be instantiated, because Monster is abstract
|
||||
//Monster monster2 = Monster::Create(Monsters::Ork);
|
||||
|
||||
Ork ork;
|
||||
ork.name = "My Ork";
|
||||
ork.name = "Ork1";
|
||||
ork.Move();
|
||||
|
||||
cout << entity.ToString() << endl;
|
||||
cout << ork.ToString() << endl;
|
||||
//cout << entity.ToString() << endl;
|
||||
//cout << ork.ToString() << endl;
|
||||
|
||||
Vector3 position(1.0f, 3.0f, -5.0f);
|
||||
position.y = 0.0f;
|
||||
//Vector3 position(1.0f, 3.0f, -5.0f);
|
||||
//position.y = 0.0f;
|
||||
|
||||
ICompareable* pComparable = nullptr;
|
||||
pComparable = &ork;
|
||||
pComparable->Compare(&ork);
|
||||
(*pComparable).Compare(&ork); // same as line above, but more complicated to write
|
||||
//ICompareable* pComparable = nullptr;
|
||||
//pComparable = &ork;
|
||||
//pComparable->Compare(&ork);
|
||||
//(*pComparable).Compare(&ork); // same as line above, but more complicated to write
|
||||
|
||||
ICompareable* pComparables[] = { new Ork, new Ork, new Ork };
|
||||
for (size_t i = 0; i < size(pComparables); i++)
|
||||
{
|
||||
//pComparables[i]->Compare(nullptr);
|
||||
delete pComparables[i];
|
||||
pComparables[i] = nullptr;
|
||||
}
|
||||
//ICompareable* pComparables[] = { new Ork, new Ork, new Ork };
|
||||
//for (size_t i = 0; i < size(pComparables); i++)
|
||||
//{
|
||||
// //pComparables[i]->Compare(nullptr);
|
||||
// delete pComparables[i];
|
||||
// pComparables[i] = nullptr;
|
||||
//}
|
||||
|
||||
ICompareable** pHeapComparables = new ICompareable*[10];
|
||||
delete[] pHeapComparables;
|
||||
pHeapComparables = nullptr;
|
||||
//ICompareable** pHeapComparables = new ICompareable*[10];
|
||||
//delete[] pHeapComparables;
|
||||
//pHeapComparables = nullptr;
|
||||
|
||||
//Ork ork2(ork);
|
||||
//ork2.name = "Ork2";
|
||||
//ork2.Move();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user