Added: C++ projects

This commit is contained in:
2026-09-29 22:09:02 +02:00
parent 11929bad68
commit 0af79b3686
32 changed files with 1397 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Ignored default folder with query files
/queries/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>
+13
View File
@@ -0,0 +1,13 @@
//
// Created by David Hackbarth on 21.09.26.
//
#ifndef CLASSES_ATTACKTYPE_H
#define CLASSES_ATTACKTYPE_H
enum AttackType {
Melee,
Ranged
};
#endif //CLASSES_ATTACKTYPE_H
+17
View File
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 4.3)
project(Classes)
set(CMAKE_CXX_STANDARD 20)
add_executable(Classes main.cpp
Entity.cpp
Entity.h
Monster.cpp
Monster.h
Ork.cpp
Ork.h
Vector3.h
AttackType.h
IComparable.h
Weapon.h
)
+36
View File
@@ -0,0 +1,36 @@
//
// Created by David Hackbarth on 21.09.26.
//
#include "Entity.h"
#include <iostream>
using namespace std;
Entity::Entity() : _name("") //defined
{
cout << "Entity constructor called" << endl;
}
Entity::Entity(string name) : _name(name)
{
// _name = name;
cout << "Entity constructor with name called" << endl;
}
Entity::~Entity()
{
cout << "Entity destructor called" << endl;
}
string Entity::toString()
{
return "Entity: " + _name;
}
void Entity::setName(string name)
{
_name = name;
}
+29
View File
@@ -0,0 +1,29 @@
//
// Created by David Hackbarth on 21.09.26.
//
#ifndef CLASSES_ENTITY_H
#define CLASSES_ENTITY_H
#include <string>
using namespace std;
class Entity
{
public:
Entity(); // declared
Entity(string name); // declared
virtual ~Entity();
virtual string toString();
void setName(string name);
protected:
string _name = "";
private:
}; // IMPORTANT: don't forget the semicolon
#endif //CLASSES_ENTITY_H
+22
View File
@@ -0,0 +1,22 @@
//
// Created by David Hackbarth on 22.09.26.
//
#ifndef CLASSES_ICOMPARABLE_H
#define CLASSES_ICOMPARABLE_H
#include <iostream>
using namespace std;
class IComparable
{
public:
// only for demonstration purposes
IComparable() { cout << "IComparable constructor called" << endl; }
virtual ~IComparable() { cout << "IComparable destructor called" << endl; }
virtual int compareTo(const IComparable& other) const = 0;
};
#endif //CLASSES_ICOMPARABLE_H
+50
View File
@@ -0,0 +1,50 @@
//
// Created by David Hackbarth on 21.09.26.
//
#include "Monster.h"
#include <iostream>
using namespace std;
Monster::Monster() : Entity(""), _pWeapon(new Weapon())
{
cout << "Monster constructor called" << endl;
}
Monster::Monster(string name) : Entity(name), _pWeapon(new Weapon())
{
cout << "Monster constructor with name called" << 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()
{
cout << "Monster destructor called" << endl;
if (_pWeapon != nullptr)
{
delete _pWeapon;
_pWeapon = nullptr;
}
}
string Monster::toString()
{
return "Monster: " + _name;
}
int Monster::compareTo(const IComparable& other) const
{
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
//
// Created by David Hackbarth on 21.09.26.
//
#ifndef CLASSES_MONSTER_H
#define CLASSES_MONSTER_H
#include "Entity.h"
#include "IComparable.h"
#include "Weapon.h"
class Monster : public Entity, public IComparable
{
public:
// default constructor, copy constructor & destructor, if you need one you'll need all of them
// internal pointers often requires a deep copy and so a default constructor & a copy constructor & a destructor
Monster();
Monster(string name);
Monster(const Monster& other);
~Monster() override;
string toString() override;
int compareTo(const IComparable& other) const override;
// void assignWeapon(Weapon* weapon);
protected:
Weapon* _pWeapon = nullptr;
};
#endif //CLASSES_MONSTER_H
+29
View File
@@ -0,0 +1,29 @@
//
// Created by David Hackbarth on 21.09.26.
//
#include "Ork.h"
#include <iostream>
using namespace std;
Ork::Ork(string name) : Monster(name)
{
cout << "Ork constructor with name called" << endl;
}
Ork::~Ork()
{
cout << "Ork destructor called" << endl;
}
string Ork::toString()
{
return "Ork: " + _name;
}
void Ork::testMethod()
{
cout << "testMethod called" << endl;
}
+22
View File
@@ -0,0 +1,22 @@
//
// Created by David Hackbarth on 21.09.26.
//
#ifndef CLASSES_ORK_H
#define CLASSES_ORK_H
#include "Monster.h"
class Ork : public Monster
{
public:
Ork(string name);
~Ork() override;
string toString() override;
virtual void testMethod();
};
#endif //CLASSES_ORK_H
+18
View File
@@ -0,0 +1,18 @@
//
// Created by David Hackbarth on 21.09.26.
//
#ifndef CLASSES_VECTOR3_H
#define CLASSES_VECTOR3_H
struct Vector3
{
float x;
float y;
float z;
Vector3() : x(0.0f), y(0.0f), z(0.0f) {}
Vector3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
};
#endif //CLASSES_VECTOR3_H
+27
View File
@@ -0,0 +1,27 @@
//
// Created by David Hackbarth on 23.09.26.
//
#ifndef CLASSES_WEAPON_H
#define CLASSES_WEAPON_H
class Weapon {
public:
// implicit inlining
const int& getAmmo() const { return _ammo; }
void setAmmo(int ammo) { _ammo = ammo; }
// explicit inlining
const int& getDamage() const;
void setDamage(int damage);
private:
int _ammo = 0;
int _damage = 0;
};
// explicit inlining needs the method definition to be always in the header file
inline const int& Weapon::getDamage() const { return _damage; }
inline void Weapon::setDamage(int damage) { _damage = damage; }
#endif //CLASSES_WEAPON_H
+151
View File
@@ -0,0 +1,151 @@
#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;
}