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;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -42,7 +43,38 @@ int main()
|
||||
printArray(numbers4);
|
||||
printArray(numbers4, size(numbers4));
|
||||
|
||||
// "dynamic" (...) arrays
|
||||
// "dynamic" heap arrays
|
||||
int count2 = 10;
|
||||
int* pNumbers = new int[count2];
|
||||
pNumbers[0] = 10;
|
||||
int* pNumber = pNumbers; // pNumber points onto the first element
|
||||
//pNumber++; // pNumber points onto the second element
|
||||
//pNumber--;
|
||||
//pNumber += 2;
|
||||
cout << "\"dynamic\" heap arrays" << endl;
|
||||
for (size_t i = 0; i < count2; i++) // CAUTION - size is not working with heap arrays, we have to remember the size of the array
|
||||
{
|
||||
cout << pNumber << ": " << *pNumber << endl;
|
||||
pNumber++;
|
||||
}
|
||||
delete[] pNumbers; // CAUTION - heap arrays have to be deleted with delete[]
|
||||
pNumbers = pNumber = nullptr;
|
||||
|
||||
// 2d arrays
|
||||
int** p2dArray = new int* [10];
|
||||
for (size_t i = 0; i < 10; i++)
|
||||
{
|
||||
p2dArray[i] = new int[10];
|
||||
}
|
||||
|
||||
// for delete -> delete inner arrays first
|
||||
for (size_t i = 0; i < 10; i++)
|
||||
{
|
||||
delete[] p2dArray[i];
|
||||
p2dArray[i] = nullptr;
|
||||
}
|
||||
delete[] p2dArray; // then outer array
|
||||
p2dArray = nullptr;
|
||||
|
||||
// stl collections
|
||||
vector<int> numbers5;
|
||||
@@ -60,6 +92,28 @@ int main()
|
||||
numbersList.push_back(10);
|
||||
numbersList.push_front(0);
|
||||
//numbersList[0] = 100; // random access is not available for lists
|
||||
list<int>::iterator it = numbersList.begin();
|
||||
for (; it != numbersList.end(); it++)
|
||||
{
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
numbersList.insert(--it, 20);
|
||||
|
||||
for (it = numbersList.begin(); it != numbersList.end(); it++)
|
||||
{
|
||||
cout << *it << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
for (int n : numbersList)
|
||||
{ }
|
||||
|
||||
// https://cppreference.com/w/cpp/algorithm.html
|
||||
for_each(numbersList.begin(), numbersList.end(), [](const int& n) {
|
||||
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -65,5 +65,19 @@ int main()
|
||||
shared_ptr<int> pTmpSharedPointer2 = pNumber7.lock();
|
||||
if (pTmpSharedPointer2 != nullptr) *pTmpSharedPointer2 = 10; // smart pointer are no guarantee for a valid object behind the pointer
|
||||
|
||||
// const exercise
|
||||
// int *
|
||||
// const int *
|
||||
// int const *
|
||||
// int * const
|
||||
// const int * const
|
||||
// int const * const
|
||||
// const int const * const
|
||||
// const int const const * const
|
||||
|
||||
// const int* pNumber = new int(0);
|
||||
// *pNumber = 10; // is this working
|
||||
// pNumber = new int(0); // is this working
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user