add pointer in classes
add interfaces
This commit is contained in:
@@ -184,6 +184,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Entity.h" />
|
||||
<ClInclude Include="ICompareable.h" />
|
||||
<ClInclude Include="Monster.h" />
|
||||
<ClInclude Include="Ork.h" />
|
||||
<ClInclude Include="Vector3.h" />
|
||||
|
||||
@@ -47,5 +47,8 @@
|
||||
<ClInclude Include="Vector3.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ICompareable.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
// only for demonstration
|
||||
#include <iostream>
|
||||
|
||||
class ICompareable
|
||||
{
|
||||
public:
|
||||
ICompareable() { std::cout << "ICompareable::ICompareable()" << std::endl; } // constructor, only for demonstration
|
||||
virtual ~ICompareable() { std::cout << "ICompareable::~ICompareable()" << std::endl; } // destructor
|
||||
|
||||
virtual bool Compare(const ICompareable* pOther) = 0; // pure virtual - abstract method
|
||||
};
|
||||
@@ -1,6 +1,30 @@
|
||||
#include "Monster.h"
|
||||
#include <iostream>
|
||||
|
||||
Monster::Monster()
|
||||
Monster::Monster() : _pWeapon(new Weapon())
|
||||
{
|
||||
//ToString();
|
||||
|
||||
std::cout << "Monster::Monster()" << std::endl;
|
||||
}
|
||||
|
||||
Monster::Monster(const Monster& other)
|
||||
{
|
||||
// shallow copy
|
||||
this->_name = other._name;
|
||||
//this->_pWeapon = other._pWeapon;
|
||||
|
||||
// deep copy
|
||||
this->_pWeapon = new Weapon(*other._pWeapon);
|
||||
}
|
||||
|
||||
Monster::~Monster()
|
||||
{
|
||||
std::cout << "Monster::~Monster()" << std::endl;
|
||||
|
||||
if (_pWeapon != nullptr)
|
||||
{
|
||||
delete _pWeapon;
|
||||
_pWeapon = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,25 @@
|
||||
#pragma once
|
||||
#include "Entity.h"
|
||||
#include "Vector3.h"
|
||||
#include "ICompareable.h"
|
||||
#include "Weapon.h"
|
||||
|
||||
class Monster : public MyNamespace::Entity
|
||||
class Monster : public MyNamespace::Entity, public ICompareable
|
||||
{
|
||||
public:
|
||||
// constructor, copy-constructor & destructor,
|
||||
// if you need one, you'll need all of them
|
||||
// internal pointers often requires constrcutor, copy-constructor & destructor
|
||||
Monster();
|
||||
//Monster(Weapon* weapon);
|
||||
Monster(const Monster& other);
|
||||
virtual ~Monster();
|
||||
|
||||
std::string ToString() override { return "Monster"; }
|
||||
|
||||
virtual void Move(const Vector3& move) = 0;
|
||||
bool Compare(const ICompareable* pOther) override;
|
||||
|
||||
protected:
|
||||
Weapon* _pWeapon = nullptr;
|
||||
};
|
||||
@@ -1,6 +1,23 @@
|
||||
#include "Ork.h"
|
||||
#include <iostream>
|
||||
|
||||
Ork::Ork()
|
||||
{
|
||||
//ToString();
|
||||
|
||||
std::cout << "Ork::Ork()" << std::endl;
|
||||
}
|
||||
|
||||
Ork::~Ork()
|
||||
{
|
||||
std::cout << "Ork::~Ork()" << std::endl;
|
||||
}
|
||||
|
||||
void Ork::Move(const Vector3& move)
|
||||
{
|
||||
}
|
||||
|
||||
bool Monster::Compare(const ICompareable* pOther)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ class Ork : public Monster
|
||||
{
|
||||
public:
|
||||
Ork();
|
||||
~Ork();
|
||||
|
||||
std::string ToString() override { return "Ork"; }
|
||||
|
||||
void Move(const Vector3& move) override;
|
||||
};
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "Entity.h"
|
||||
#include "Monster.h"
|
||||
#include "Vector3.h"
|
||||
#include "Ork.h"
|
||||
|
||||
using namespace MyNamespace;
|
||||
using namespace std;
|
||||
@@ -11,12 +12,29 @@ int main()
|
||||
Entity entity; // this is an instance
|
||||
cout << entity.ToString() << endl;
|
||||
|
||||
Monster monster;
|
||||
cout << monster.ToString() << endl;
|
||||
// can't be instantiated, because it is abstract
|
||||
//Monster monster;
|
||||
//cout << monster.ToString() << endl;
|
||||
|
||||
Vector3 position(1.0f, 0.0f, 2.0f);
|
||||
Vector3 position2 = Vector3();
|
||||
Vector3 position3{}; // list-initializer
|
||||
|
||||
Ork ork;
|
||||
ork.Move(position);
|
||||
|
||||
Monster* pMonster = new Ork();
|
||||
delete pMonster;
|
||||
pMonster = nullptr;
|
||||
|
||||
ICompareable* pCompareables[] = { new Ork, new Ork, new Ork };
|
||||
for (size_t i = 0; i < size(pCompareables); i++)
|
||||
{
|
||||
delete pCompareables[i];
|
||||
pCompareables[i] = nullptr;
|
||||
}
|
||||
|
||||
Ork ork2 = ork;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user