add pointer

add "dynamic" heap array
add polymorphic
This commit is contained in:
2025-03-26 12:44:05 +01:00
parent abb1ea4a38
commit 4b6ac6a484
13 changed files with 339 additions and 1 deletions
+4
View File
@@ -11,6 +11,10 @@ Entity::Entity(std::string name) : name(name)
//this->name = name;
}
Entity::~Entity()
{
}
std::string Entity::ToString()
{
return name;
+1
View File
@@ -6,6 +6,7 @@ class Entity
public:
Entity();
Entity(std::string name);
virtual ~Entity();
virtual std::string ToString();
+2
View File
@@ -3,5 +3,7 @@
class ICompareable
{
public:
virtual ~ICompareable() {}
virtual int Compare(ICompareable* other) = 0;
};
+4
View File
@@ -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;
+1
View File
@@ -13,6 +13,7 @@ class Monster : public Entity, public ICompareable
{
public:
Monster(int strength);
virtual ~Monster();
//static Monster Create(Monsters type);
+10
View File
@@ -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()
{
}
+3 -1
View File
@@ -4,7 +4,9 @@
class Ork : public Monster
{
public:
Ork();
Ork(); // default constructor
Ork(const Ork&); // copy constructor
~Ork(); // destructor
void Move() override;
};
+17
View File
@@ -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;
}