152 lines
4.4 KiB
C++
152 lines
4.4 KiB
C++
#include <iostream>
|
|
|
|
#include "AttackType.h"
|
|
#include "Entity.h"
|
|
#include "Monster.h"
|
|
#include "Ork.h"
|
|
#include "Vector3.h"
|
|
#include "Weapon.h"
|
|
|
|
using namespace std;
|
|
|
|
int main()
|
|
{
|
|
Entity e("John"); // this is an entity instance
|
|
cout << e.toString() << endl;
|
|
e.setName("Fred");
|
|
|
|
Monster m("Doe");
|
|
cout << m.toString() << endl;
|
|
m.setName("Jane");
|
|
|
|
Ork o("Alex");
|
|
|
|
Monster m2 = m; // m2 is a copy of m
|
|
|
|
Vector3 v;
|
|
v.x = 1.0f;
|
|
v.y = 2.0f;
|
|
v.z = 3.0f;
|
|
|
|
Vector3 v2(1.0f, 2.0f, 3.0f);
|
|
Vector3 v3 = Vector3(1.0f, 2.0f, 3.0f);
|
|
Vector3 v4 = {1.0f, 2.0f, 3.0f}; // list-initialization assigned
|
|
Vector3 v5 { 1.0f, 2.0f, 3.0f }; // list-initialization
|
|
|
|
Weapon w;
|
|
w.setAmmo(100);
|
|
// int ammo = w.getAmmo();
|
|
cout << w.getAmmo() << endl;
|
|
w.setDamage(9999);
|
|
int damage = w.getDamage();
|
|
|
|
Weapon* pWeapon = new Weapon();
|
|
pWeapon->setAmmo(100);
|
|
int ammo2 = pWeapon->getAmmo();
|
|
|
|
AttackType at = Melee;
|
|
at = AttackType::Ranged;
|
|
|
|
// interfaces & abstract classes
|
|
IComparable* pComparable = nullptr;
|
|
pComparable = &m;
|
|
pComparable = &o;
|
|
pComparable->compareTo(o);
|
|
(*pComparable).compareTo(o); // same as pComparable->compareTo(o)
|
|
|
|
IComparable* comparables[] = { new Ork("Ork 1"), new Ork("Ork 2"), new Monster };
|
|
for (int i = 0; i < size(comparables); ++i)
|
|
{
|
|
comparables[i]->compareTo(o);
|
|
delete comparables[i];
|
|
comparables[i] = nullptr;
|
|
}
|
|
|
|
// create a heap pointer array
|
|
IComparable** heapComparables = new IComparable*[3];
|
|
heapComparables[0] = new Ork("Ork 1");
|
|
heapComparables[1] = new Ork("Ork 2");
|
|
heapComparables[2] = new Monster;
|
|
|
|
for (size_t i = 0; i < 3; ++i)
|
|
{
|
|
// delete the elements first
|
|
delete heapComparables[i];
|
|
heapComparables[i] = nullptr;
|
|
}
|
|
delete[] heapComparables;
|
|
heapComparables = nullptr;
|
|
|
|
// casts
|
|
// c cast
|
|
int number = -100000000;
|
|
unsigned short smallPositiveNumber = (unsigned short)number;
|
|
int *pNumber = &number;
|
|
unsigned short* pSmallPositiveNumber = (unsigned short*)pNumber;
|
|
float fNumber = (float)number;
|
|
float* pFNumber = (float*)pNumber;
|
|
int* pNumbers = new int[3];
|
|
void** pVoidNumbers = (void**)&pNumbers;
|
|
Monster* pMonster = (Monster*)pNumbers;
|
|
Monster* pMonster2 = new Ork("Bla bla");
|
|
Ork* pOrk = (Ork*)pMonster2;
|
|
string toString = pOrk->toString();
|
|
pOrk->testMethod();
|
|
Monster* pMonster3 = new Monster();
|
|
Ork* pOrk2 = (Ork*)pMonster3;
|
|
string toString2 = pOrk2->toString();
|
|
|
|
// runtime error, because pMonster3 is a Monster, not an Ork,
|
|
// virtual table lookup is in the Monster and not the Ork class
|
|
// pOrk2->testMethod();
|
|
|
|
// c++ cast
|
|
// static_cast
|
|
smallPositiveNumber = static_cast<unsigned short>(number);
|
|
|
|
// compiler error, use reinterpret_cast with caution instead
|
|
// pSmallPositiveNumber = static_cast<unsigned short*>(pNumber);
|
|
|
|
fNumber = static_cast<float>(number);
|
|
// pFNumber = static_cast<float*>(pNumber);
|
|
// pVoidNumbers = static_cast<void**>(pNumbers);
|
|
pOrk = static_cast<Ork*>(pMonster2); // possible, but dynamic_cast is better
|
|
// Weapon* pWeapon = static_cast<Weapon*>(pMonster2); // compiler error, because Monster and Weapon are unrelated
|
|
// pOrk2 = static_cast<Ork*>(pMonster3); // possible, but dynamic_cast is better and this example will not work at all
|
|
|
|
// dynamic_cast, do runtime checks, same as 'as' in C#
|
|
pOrk = dynamic_cast<Ork*>(pMonster2);
|
|
pOrk2 = dynamic_cast<Ork*>(pMonster3);
|
|
|
|
// reinterpret_cast
|
|
// smallPositiveNumber = reinterpret_cast<unsigned short>(number); // compiler error, use static_cast instead
|
|
pSmallPositiveNumber = reinterpret_cast<unsigned short*>(pNumber);
|
|
pFNumber = reinterpret_cast<float*>(pNumber);
|
|
|
|
// const_cast
|
|
const char* sz = "Test";
|
|
// sz[0] = 'H'; // compiler error, use const_cast instead
|
|
// char* nonConstSz = sz;
|
|
char* nonConstSz = const_cast<char*>(sz);
|
|
nonConstSz[0] = 'H';
|
|
|
|
const string str = "Test";
|
|
string str2 = str;
|
|
str.c_str(); // if you need a zero terminated string, use c_str()
|
|
string str3 = to_string(number);
|
|
|
|
delete[] pNumbers;
|
|
pNumbers = nullptr;
|
|
pVoidNumbers = nullptr;
|
|
pNumber = nullptr;
|
|
pSmallPositiveNumber = nullptr;
|
|
pFNumber = nullptr;
|
|
pMonster = nullptr;
|
|
delete pMonster2;
|
|
pMonster2 = nullptr;
|
|
delete pMonster3;
|
|
pMonster3 = nullptr;
|
|
|
|
return 0;
|
|
}
|