Skip to content

Instantly share code, notes, and snippets.

@sunmeat
sunmeat / main.cpp
Last active July 11, 2025 18:24
console cpp app logger example with singleton patterns
#include <iostream>
#include <string>
using namespace std;
class Logger
{
static Logger* instance; // приватний статичний вказівник на єдиний екземпляр класу
int log_count = 0; // скільки разів відбувся запис рядка у файл
Logger() // конструктор - приватний (забороняє створювати об'єкти за межами класу)
@sunmeat
sunmeat / main.cpp
Last active July 12, 2025 09:45
C++ diamond problem (rhombus inheritance)
#include <iostream>
#include <string>
using namespace std;
#define PI 3.14159265358979323846
// точка
class Point
{
protected:
@sunmeat
sunmeat / snake.cpp
Last active June 29, 2025 13:43
snake game cpp console application code example
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main() {
srand(time(0)); // запуск генератора случайных чисел
system("title Snake Game");
system("mode con cols=70 lines=31"); // установка размеров окна консоли
MoveWindow(GetConsoleWindow(), 50, 50, 2000, 2000, true); // установка стартовой позиции окна консоли (50 и 50 - это пиксели
@sunmeat
sunmeat / horse.cpp
Last active April 29, 2025 17:43
horse moving C++
#include <iostream>
#include <windows.h>
using namespace std;
const int k = 5; // chess board size
int ar[k][k];
const int shift_count = 8;
@sunmeat
sunmeat / towers.cpp
Last active April 29, 2025 17:56
hanoj towers example C++
#include <iostream>
#include <windows.h>
using namespace std;
const int quantity = 5;
int sterjen1[quantity]{}, sterjen2[quantity]{}, sterjen3[quantity]{};
void moveDisc(int from[], int to[])
{
@sunmeat
sunmeat / 8queens.cpp
Last active April 29, 2025 18:01
8 queens C++
#include <conio.h>
#include <iostream>
using namespace std;
int z = 0; // кількість варіантів розміщення
char check(int A[], int n)
{
int i, j;
for (i = 0; i < n; i++)
@sunmeat
sunmeat / voidstar.cpp
Last active April 30, 2025 18:47
вказівник на невизначений тип void*
#include <iostream>
using namespace std;
void swap(void *a, void *b, size_t size)
{
char *tmp = (char*) malloc(size);
memcpy(tmp, a, size);
memcpy(a, b, size);
memcpy(b, tmp, size);
free(tmp);
@sunmeat
sunmeat / memory_allocation.cpp
Last active May 1, 2025 20:19
malloc calloc realloc free example C++
#include <iostream>
using namespace std;
// функція для демонстрації malloc
void malloc_example() {
system("title malloc example");
int k = 5;
// malloc повертає вказівник на виділену пам'ять, сама пам'ять не ініціалізується
int* ptr = (int*)malloc(k * sizeof(int)); // виділення пам'яті за допомогою malloc
@sunmeat
sunmeat / references.cpp
Last active May 1, 2025 17:46
reference example C++
#include <iostream>
using namespace std;
void vegas(int& a, int& b)
{
// обмін значеннями двох змінних
int temp = a;
a = b;
b = temp;
}
@sunmeat
sunmeat / msize.cpp
Last active May 1, 2025 20:16
_msize example C++
#include <iostream>
using namespace std;
int main() {
setlocale(LC_ALL, "Ukrainian");
int staticArr[10]; // статичний масив
int* dynamicArr = new int[10]; // динамічний масив