add pointer
add "dynamic" heap array add polymorphic
This commit is contained in:
@@ -11,6 +11,10 @@ Entity::Entity(std::string name) : name(name)
|
||||
//this->name = name;
|
||||
}
|
||||
|
||||
Entity::~Entity()
|
||||
{
|
||||
}
|
||||
|
||||
std::string Entity::ToString()
|
||||
{
|
||||
return name;
|
||||
|
||||
@@ -6,6 +6,7 @@ class Entity
|
||||
public:
|
||||
Entity();
|
||||
Entity(std::string name);
|
||||
virtual ~Entity();
|
||||
|
||||
virtual std::string ToString();
|
||||
|
||||
|
||||
@@ -3,5 +3,7 @@
|
||||
class ICompareable
|
||||
{
|
||||
public:
|
||||
virtual ~ICompareable() {}
|
||||
|
||||
virtual int Compare(ICompareable* other) = 0;
|
||||
};
|
||||
@@ -7,6 +7,10 @@ Monster::Monster(int strength) : Entity("My Monster"), _strength(strength)
|
||||
this->name = "My Monster";
|
||||
}
|
||||
|
||||
Monster::~Monster()
|
||||
{
|
||||
}
|
||||
|
||||
//Monster Monster::Create(Monsters type)
|
||||
//{
|
||||
// Monster monster;
|
||||
|
||||
@@ -13,6 +13,7 @@ class Monster : public Entity, public ICompareable
|
||||
{
|
||||
public:
|
||||
Monster(int strength);
|
||||
virtual ~Monster();
|
||||
|
||||
//static Monster Create(Monsters type);
|
||||
|
||||
|
||||
@@ -6,6 +6,16 @@ Ork::Ork() : Monster(10)
|
||||
this->name = "My Ork";
|
||||
}
|
||||
|
||||
Ork::Ork(const Ork& other) : Monster(other._strength)
|
||||
{
|
||||
this->name = other.name;
|
||||
this->_speed = other._speed;
|
||||
}
|
||||
|
||||
Ork::~Ork()
|
||||
{
|
||||
}
|
||||
|
||||
void Ork::Move()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
class Ork : public Monster
|
||||
{
|
||||
public:
|
||||
Ork();
|
||||
Ork(); // default constructor
|
||||
Ork(const Ork&); // copy constructor
|
||||
~Ork(); // destructor
|
||||
|
||||
void Move() override;
|
||||
};
|
||||
|
||||
@@ -24,5 +24,22 @@ int main()
|
||||
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* 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;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user