This commit is contained in:
2026-04-20 11:19:07 +02:00
parent d38af389b6
commit ccacf0e97a
36 changed files with 786 additions and 1 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,20 @@
#include <stdio.h>
class Prostokat {
public:
double szerokosc;
double wysokosc;
double pole() { return szerokosc * wysokosc; }
double obwod() { return 2 * szerokosc + 2 * wysokosc; }
};
int main() {
Prostokat p1;
p1.szerokosc = 2;
p1.wysokosc = 2;
double pole = p1.pole();
double obwod = p1.obwod();
printf(
"Pole o szerokości %0.2f i wysokości %0.2f ma pole %0.2f i obwód %0.2f",
p1.szerokosc, p1.wysokosc, pole, obwod);
}

View File

@@ -0,0 +1,35 @@
#include <stdexcept>
#include <stdio.h>
class Konto {
static int liczbaKont;
float saldo;
public:
Konto(int saldoPoczatkowe) : saldo(saldoPoczatkowe) { Konto::liczbaKont++; }
void wplac(float pieniadze) { saldo += pieniadze; }
void wyplac(float pieniadze) {
if (saldo - pieniadze <= 0) {
throw std::invalid_argument(
"Saldo po wypłacie nie może być mniejsze niż 0!!!\n");
}
saldo -= pieniadze;
}
void pokazSaldo() { printf("Aktualne saldo to: %0.2f\n", saldo); }
static void ileKont() {
printf("Aktualnie jest %d kont\n", Konto::liczbaKont);
}
};
int Konto::liczbaKont = 0;
int main() {
Konto::ileKont();
Konto k1 = Konto(20);
Konto::ileKont();
Konto k2 = Konto(20);
Konto::ileKont();
Konto k3 = Konto(20);
Konto::ileKont();
}

View File

@@ -0,0 +1,69 @@
#include <stdio.h>
#include <string>
class Produkt {
public:
std::string nazwa;
float cena;
Produkt(std::string nazwa, float cena) : nazwa(nazwa), cena(cena) {}
Produkt() : nazwa(""), cena(0) {}
const Produkt &operator=(const Produkt *p) {
if (this != p) {
nazwa = p->nazwa;
cena = p->cena;
}
return *this;
}
};
class Magazyn {
public:
Produkt *produkty;
size_t max_pojemnosc;
size_t aktualna_pojemnosc;
Magazyn(size_t pojemnosc) {
this->max_pojemnosc = pojemnosc;
this->aktualna_pojemnosc = 0;
produkty = new Produkt[pojemnosc];
}
~Magazyn() { delete[] produkty; }
void dodaj(const Produkt &p) {
if (aktualna_pojemnosc >= max_pojemnosc) {
printf("Nie można dołożyć elementu, za mało miejsca\n");
return;
}
produkty[aktualna_pojemnosc++] = p;
}
Produkt *znajdz(std::string nazwa) {
for (size_t i = 0; i < aktualna_pojemnosc; i++) {
if (produkty[i].nazwa == nazwa) {
return &produkty[i];
}
}
return nullptr;
}
void wypiszWszystkie() const {
for (size_t i = 0; i < aktualna_pojemnosc; i++) {
printf("Nazwa: %s | Cena: %0.2f\n", produkty[i].nazwa.c_str(),
produkty[i].cena);
}
}
};
int main() {
Magazyn m(5);
m.dodaj(Produkt("Laptop", 3500.0));
m.dodaj(Produkt("Mysz", 49.99));
m.dodaj(Produkt("Klawiatura", 129.0));
Produkt *p = m.znajdz("Mysz");
if (p != nullptr) {
p->cena = 39.99; // zmiana przez wskaźnik
}
m.wypiszWszystkie();
}

View File

@@ -0,0 +1,60 @@
#include <stdio.h>
#include <string>
#include <vector>
class Student {
std::string imie;
std::vector<double> oceny;
public:
Student(std::string imie) : imie(imie) {}
void dodajOcene(double ocena) { oceny.push_back(ocena); }
double srednia() const {
double sum = 0;
for (double ocena : oceny) {
sum += ocena;
}
double avg = sum / oceny.size();
return avg;
}
void wypisz() const {
printf("Imie %s\n", imie.c_str());
printf("Oceny:\n");
for (double ocena : oceny) {
printf("- %0.1f\n", ocena);
}
printf("Średnia: %0.2f\n", srednia());
}
};
class Dziennik {
std::vector<std::reference_wrapper<Student>> studenci;
public:
void zarejestruj(Student &s) { studenci.push_back(s); }
void dodajOceneWszystkim(double ocena) {
for (Student &student : studenci) {
student.dodajOcene(ocena);
}
}
void raport() const {
printf("--- Wszyscy studenci ---\n");
for (const Student student : studenci) {
student.wypisz();
}
}
};
int main() {
Student s1 = Student("Bogdan");
Student s2 = Student("Wojciech");
Dziennik dziennik = Dziennik();
dziennik.zarejestruj(s1);
dziennik.zarejestruj(s2);
dziennik.dodajOceneWszystkim(5);
dziennik.raport();
}

View File

@@ -0,0 +1,22 @@
class Prostokat {
private:
double szerokosc;
double wysokosc;
public:
Prostokat(double szerokosc, double wysokosc)
: szerokosc(szerokosc), wysokosc(wysokosc) {}
double pole() { return szerokosc * wysokosc; }
double obwod() { return 2 * szerokosc + 2 * wysokosc; }
double getSzerokosc() { return szerokosc; }
double getWysokosc() { return wysokosc; }
};
int main() {
Prostokat p1 = Prostokat(2, 2);
// p1.szerokosc = 4; taka operacja jest nieakceptowalna albowiem skrajnie
// nierozsądnym i można powiedzieć __nieakceptowalnym__ jest próba przypisania
// wartości do pola oznaczonego mianem prywatnego. Na szczęście jaśnie
// oświecony kompilator języka c++ powstrzymuje nas przed takim godnym
// potępienia występkiem.
}

View File

@@ -0,0 +1,20 @@
#include <iostream>
class Licznik {
int wartosc;
public:
Licznik() : wartosc(0) {}
void dodaj() { wartosc++; }
int ile() const { return wartosc; }
};
int main() {
Licznik a; // 0
Licznik b; // 0
a.dodaj(); // 1
a.dodaj(); // 2
a.dodaj(); // 3
b.dodaj(); // 1
std::cout << a.ile() << " " << b.ile()
<< "\n"; // a.ile() == 2 && b.ile() == 1
}

View File

@@ -0,0 +1,22 @@
#include <iostream>
class Kalkulator {
public:
void podwoj(int x) { x *= 2; }
void podwojRef(int &x) { x *= 2; }
void zamien(int &a, int &b) {
int tmp = a;
a = b;
b = tmp;
}
};
int main() {
Kalkulator k;
int a = 5, b = 10;
k.podwoj(a); // a = 5
std::cout << a << std::endl;
k.podwojRef(a); // a = 10
std::cout << a << std::endl;
k.zamien(a, b); // a = 10, b = 10
std::cout << a << ' ' << b << std::endl;
}

View File

@@ -0,0 +1,52 @@
#include <iostream>
#include <vector>
class Tablica {
std::vector<int> tablica;
public:
void dodaj(int x) { tablica.push_back(x); }
void wypelnij(std::vector<int> &cel) {
cel.insert(cel.end(), tablica.begin(), tablica.end());
}
int suma() const {
int sum = 0;
for (int element : tablica) {
sum += element;
}
return sum;
}
void wyczysc() { tablica.clear(); }
void wyswietl() {
for (int element : tablica) {
std::cout << element << ' ';
}
std::cout << std::endl;
}
};
int main() {
Tablica t;
t.dodaj(3);
t.wyswietl();
t.dodaj(7);
t.wyswietl();
t.dodaj(1);
t.wyswietl();
std::vector<int> kopia;
t.wypelnij(kopia);
std::cout << "Kopia: " << std::endl;
for (int element : kopia) {
std::cout << element << ' ';
}
std::cout << std::endl;
// kopia zawiera: 3, 7, 1
int suma = t.suma();
std::cout << "Suma wynosi: " << suma << std::endl;
t.wyczysc();
t.wyswietl();
}

View File

@@ -0,0 +1,25 @@
#include <iostream>
class Licznik {
int wartosc;
public:
Licznik(int w) : wartosc(w) {}
void zwieksz() { wartosc++; }
int pobierz() const { return wartosc; }
void resetuj() { wartosc = 0; }
};
int main() {
Licznik a(10);
Licznik *wsk = &a;
wsk->zwieksz();
std::cout << a.pobierz() << '\n'; // 11 (linia A)
std::cout << wsk->pobierz() << '\n'; // 11 (linia B)
Licznik b(100);
wsk = &b;
wsk->zwieksz();
std::cout << a.pobierz() << '\n'; // 11 (linia C)
std::cout << b.pobierz() << '\n'; // 101 (linia D)
a.resetuj();
std::cout << a.pobierz(); // 0
}

View File

@@ -0,0 +1,48 @@
#include <iostream>
class Punkt {
public:
int x, y;
Punkt(int x, int y) : x(x), y(y) {}
void wyswietl() { std::cout << '(' << x << ',' << y << ')' << std::endl; }
};
void zamienRef(Punkt &a, Punkt &b) {
int x = a.x;
int y = a.y;
int i = b.x;
int j = b.y;
a.x = i;
a.y = j;
b.x = x;
b.y = y;
}
void zamienWsk(Punkt *a, Punkt *b) {
int x = a->x;
int y = a->y;
int i = b->x;
int j = b->y;
a->x = i;
a->y = j;
b->x = x;
b->y = y;
}
int main() {
std::cout << "Punkty: " << std::endl;
Punkt p1(1, 2), p2(3, 4);
p1.wyswietl();
p2.wyswietl();
std::cout << "Zamiana referencją: " << std::endl;
zamienRef(p1, p2); // wywołanie przez referencję
//
p1.wyswietl();
p2.wyswietl();
std::cout << "Zamiana wskaźnikiem: " << std::endl;
zamienWsk(&p1, &p2); // wywołanie przez wskaźnik
p1.wyswietl();
p2.wyswietl();
}

View File

@@ -0,0 +1,11 @@
#include <string>
class Samochod {
std::string marka;
int predkosc;
Samochod(std::string marka, int predkosc) {
this->marka = marka;
this->predkosc = predkosc;
}
};

View File

@@ -0,0 +1,19 @@
#include <stdio.h>
class Pies {
public:
static int liczba;
Pies() { Pies::liczba++; }
};
int Pies::liczba = 0;
int main() {
printf("Aktualnie jest %d psów\n", Pies::liczba);
Pies p1 = Pies();
printf("Aktualnie jest %d psów\n", Pies::liczba);
Pies p2 = Pies();
printf("Aktualnie jest %d psów\n", Pies::liczba);
Pies p3 = Pies();
printf("Aktualnie jest %d psów\n", Pies::liczba);
}

View File

@@ -0,0 +1,40 @@
#include <stdexcept>
#include <stdio.h>
class Konto {
float saldo;
public:
Konto(int saldoPoczatkowe) : saldo(saldoPoczatkowe) {}
void wplac(float pieniadze) { saldo += pieniadze; }
void wyplac(float pieniadze) {
if (saldo - pieniadze <= 0) {
throw std::invalid_argument(
"Saldo po wypłacie nie może być mniejsze niż 0!!!\n");
}
saldo -= pieniadze;
}
void pokazSaldo() { printf("Aktualne saldo to: %0.2f\n", saldo); }
};
int main() {
float n = 200;
printf("Tworzymy konto z saldem początkowym %0.2f\n", n);
Konto k = Konto(n);
k.pokazSaldo();
printf("Wypłacamy 100 zł\n");
k.wyplac(100);
k.pokazSaldo();
printf("Wpłacamy 50 zł\n");
k.pokazSaldo();
printf("Próbujemy wypłacić 6769 złoty\n");
try {
k.wyplac(6769);
} catch (std::invalid_argument &ex) {
printf("Nie wolno wypłacić aż tyle pieniędzy! %s", ex.what());
}
}