Files
hwr2023_a/CPlusPlus/intro/main.cpp
T
david dc3cfc8fa3 add pure virtual
add interfaces like classes
add template example
2025-02-12 13:03:15 +01:00

161 lines
3.9 KiB
C++

#include <iostream>
#include <Windows.h>
#include <memory>
#include "functions.h"
#include "ClassA.h"
#include "ClassB.h"
int number = 0;
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 not defined
//functionB(); // no error
////functionC(); // linker error - function declared, but not defined
//functionD(); // no error
////functionE(); // linker error - function is defined twice, because of two includes in two source files
//functionF(); // no error
//short s = 0;
//unsigned short us = 0;
//int i = 0;
//long l = 0;
//cout << "size of short: " << sizeof(s) << endl;
//cout << "size of int: " << sizeof(i) << endl;
//cout << "size of long: " << sizeof(l) << endl;
//cout << "size of long long: " << sizeof(long long) << endl;
//cout << "size of bool: " << sizeof(bool) << endl;
//BYTE b;
//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[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
//int* pNumber = new int(0);
//cout << "local array print" << endl;
//for (size_t i = 0; i < sizeof(numbers) / sizeof(int); i++)
//{
// std::cout << numbers[i] << " ";
//}
//std::cout << std::endl;
//cout << "function array print" << endl;
//printArray(numbers, size(numbers));
//int n = 1;
//changeInt(n);
//cout << "n (" << &n << "): " << n << endl;
//changeIntPointer(&n);
//cout << "n (" << &n << "): " << n << endl;
//int* pN = nullptr;
//int* pN2 = new int;
//*pN2 = 1234;
//delete pN2;
//pN2 = nullptr;
//if (pN2 != nullptr)
//{
// *pN2 = 1234;
//}
//pN = new int;
//*pN += 10;
//pN = new int; // BAD - Memory Leak
//changeIntReference(n);
//cout << "n (" << &n << "): " << n << endl;
//unique_ptr<int> un = make_unique<int>(0);
//*un = 10;
////unique_ptr<int> un2 = un; // not possible, because it is unique
//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;
//cout << "sn2 refs: " << sn2.use_count() << endl;
//if (!wn.expired())
//{
// shared_ptr<int> ln = wn.lock();
// *ln *= 23;
// cout << "sn2 refs: " << sn2.use_count() << endl;
//}
//cout << "sn2 refs: " << sn2.use_count() << endl;
//ClassA classA;
//ClassA classA2 = classA;
//ClassB classB;
//int number = classA.GetNumber();
//cout << number << endl;
//classB.GetNumber();
//IComparable* pComparable = nullptr;
//pComparable = new ClassB();
////ClassB* pClassB2 = (ClassB*)pComparable; // C-Cast, CAUTION: DANGER!!!
//ClassB* pClassB2 = dynamic_cast<ClassB*>(pComparable); // C++ polymorph cast
//float f = 1.0f;
//int i = static_cast<int>(f); // explicit c++ cast between two static data types
////int* pNonsense = reinterpret_cast<int*>(pComparable); // c++ cast like C-Cast
//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 cipc;
//int const* const icpc;
//const int const* const cicpc;
IComparable* pComparable = nullptr;
IComparable* pComparable2 = new ClassA();
pComparable = new ClassB();
(*pComparable).Compare(pComparable2);
pComparable->Compare(pComparable2); // identical to line above, but simpler
delete pComparable;
pComparable = nullptr;
//delete pComparable2;
//pComparable2 = nullptr;
safeDelete(pComparable2);
return 0;
}
void functionA() {}
void functionD() {}