Files
2025-02-19 16:44:16 +01:00

182 lines
4.5 KiB
C++

#include <iostream>
#include <Windows.h>
#include <memory>
#include <vector>
#include <list>
#include "functions.h"
#include "ClassA.h"
#include "ClassB.h"
using namespace std;
void functionB() {}
void functionC();
void functionD(); // forward declaration
int main()
{
//std::cout << "Hello World!" << std::endl;
//cout << "Hallo" << endl;
////functionA(); // compiler error, function is not declared, but defined afterwards
//functionB(); // no error
////functionC(); // linker error, function is declared, but not defined
//functionD(); // no error
////functionE(); // linker error, function is defined in header and included in two different source files
//functionF(); // no error
//short s = 0;
//unsigned short us = 0;
//int i = 0;
//long l = 0;
//cout << "sizeof short: " << sizeof(s) << endl;
//cout << "sizeof int: " << sizeof(i) << endl;
//cout << "sizeof long: " << sizeof(l) << endl;
//cout << "sizeof long long: " << sizeof(long long) << endl;
//cout << "sizeof bool: " << sizeof(bool) << endl;
//BOOL b;
//int number;
//cin >> number;
//cout << "number: " << number << endl;
//char c;
//cin >> c;
//cout << "c: " << c << endl;
//string str;
//cin >> str;
//cout << "str: " << str << endl;
//char sz[128];
//cin >> sz;
//cout << "sz: " << sz << endl;
//cin >> sz;
//cout << "sz: " << sz << endl;
//int numbers[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
//cout << "print local array" << endl;
//for (size_t i = 0; i < sizeof(numbers) / sizeof(int); i++)
//{
// cout << numbers[i] << " ";
//}
//cout << endl;
//cout << "print remote array" << endl;
//printArray(numbers, size(numbers));
//int n = 1;
//changeInt(n);
//cout << "n (" << &n << "): " << n << endl;
//changeIntPointer(&n);
//cout << "n (" << &n << "): " << n << endl;
//changeIntReference(n);
//cout << "n (" << &n << "): " << n << endl;
//int* pNumber = nullptr;
//int* pNumber2 = new int;
//
//if (pNumber2 != nullptr)
//{
// delete pNumber2;
// pNumber2 = nullptr;
//}
//pNumber = new int;
//*pNumber += 10;
//pNumber = new int; // BAD - Memory Leak
//unique_ptr<int> un = make_unique<int>(0);
//*un = 100;
//
//shared_ptr<int> sn = make_shared<int>(0);
//*sn = 10;
//cout << "sn refs: " << sn.use_count() << endl;
//shared_ptr<int> sn2 = sn;
//cout << "sn refs: " << sn.use_count() << endl;
//sn.reset();
//cout << "sn refs: " << sn.use_count() << endl;
//cout << "sn2 refs: " << sn2.use_count() << endl;
//weak_ptr<int> wn = sn2;
//if (!wn.expired())
//{
// shared_ptr<int> ln = wn.lock();
// *ln = 1234;
// cout << "sn2 refs: " << sn2.use_count() << endl;
//}
//cout << "sn2 refs: " << sn2.use_count() << endl;
//ClassA classA;
//ClassA classA2 = classA;
//ClassB classB;
//int n = classA.GetNumber();
//cout << n << endl;
//classB.GetNumber();
//IPrintable* pPrintable = nullptr;
//pPrintable = new ClassB();
//(*pPrintable).Print();
//pPrintable->Print(); // same line as above, but simpler
////ClassB* pClassB2 = (ClassB*)pPrintable; // C-Cast, CAUTION: DANGER!!!
//ClassB* pClassB2 = dynamic_cast<ClassB*>(pPrintable); // c++ cast for polymorhpic cast
//float f = 1.0f;
//int i = static_cast<int>(f); // c++ cast, convert one static data type into another
////int* nonsense = reinterpret_cast<int*>(pPrintable); // c++ cast like C-Cast, CAUTION: DANGER!!!
//const char csz[13] = "Hello World";
//char* sz = const_cast<char*>(csz);
// What's the difference?
//const int* cip;
//int const* icp;
//int* const ipc;
//const int const* cicp;
//const int* const cipc;
//int const* const icpc;
//const int const* const cicpc;
//delete pPrintable;
//pPrintable = nullptr;
//safeDelete(pPrintable);
//vector<int> numbers = { 0, 1, 2, 30, 5, 456 };
//cout << "numbers size/capacity: " << numbers.size() << " / " << numbers.capacity() << endl;
//numbers.push_back(999);
//cout << "numbers size/capacity: " << numbers.size() << " / " << numbers.capacity() << endl;
//numbers[3] = 67;
//
//list<int> numbers2 = { 0, 1, 2, 30, 5, 456 };
//list<int>::iterator it = numbers2.begin();
//for (; it != numbers2.end(); it++)
//{
// //*it *= 2;
// cout << *it << " ";
//}
//cout << endl;
//numbers2.insert(--it, 999);
//for (it = numbers2.begin(); it != numbers2.end(); it++)
//{
// //*it *= 2;
// cout << *it << " ";
//}
//cout << endl;
const size_t count = 5;
int numbers[count]; // const size -> stack array
size_t count2 = 10;
int* numbers2 = new int[count2]; // dynamic size -> heap array
delete[] numbers2;
numbers2 = nullptr;
return 0;
}
void functionA() {}
void functionD() {}