This commit is contained in:
2026-04-28 17:01:06 +02:00
parent 738a3dc371
commit 1a8ba1c2bf
13 changed files with 595 additions and 116 deletions

View File

@@ -0,0 +1,22 @@
#include <iostream>
class Kontener {
int *dane;
int n;
public:
Kontener(int rozmiar) : n(rozmiar), dane(new int[rozmiar]()) {}
const int &operator[](int i) const { return dane[i]; }
Kontener &operator+=(const Kontener &inny) {
for (int i = 0; i < n; i++)
dane[i] += inny.dane[i];
return *this;
}
operator int() const { return n; }
};
int main() {
Kontener k = Kontener(5);
k += 1;
std::cout << int(k);
}

View File

@@ -1,24 +0,0 @@
#include <iostream>
#include <ostream>
class Liczba {
int wartosc;
public:
Liczba(int w) : wartosc(w) {}
Liczba operator+(Liczba &inny) {
wartosc += inny.wartosc;
return *this;
}
friend std::ostream &operator<<(std::ostream &os, const Liczba &liczba) {
return os << liczba.wartosc;
}
};
int main() {
Liczba l1 = Liczba(5);
Liczba l2 = Liczba(10);
Liczba l3 = l1 + l2;
std::cout << l3;
return 0;
}

View File

@@ -0,0 +1,45 @@
#include <iostream>
#include <ostream>
class Licznik {
int wartosc;
public:
Licznik(int wartosc) : wartosc(wartosc) {}
Licznik() : wartosc(0) {}
Licznik &operator++() {
wartosc++;
return *this;
}
Licznik operator++(int) {
Licznik stary = *this;
++wartosc;
return stary;
}
Licznik &operator--() {
wartosc--;
return *this;
}
Licznik operator--(int) {
Licznik stary = *this;
--wartosc;
return stary;
}
Licznik &operator=(const Licznik &inny_licznik) {
if (this != &inny_licznik) {
wartosc = inny_licznik.wartosc;
}
return *this;
}
friend std::ostream &operator<<(std::ostream &os, Licznik &licznik) {
os << licznik.wartosc;
return os;
}
};
// int main() {
// Licznik a(5);
// std::cout << a++ << std::endl; // 6
// std::cout << a << std::endl; // 6
// std::cout << ++a << std::endl; // 7
// }

View File

@@ -0,0 +1,117 @@
#include <iostream>
#include <ostream>
#define SIZE 2
class Macierz2x2 {
public:
int macierz[SIZE][SIZE];
Macierz2x2(int a, int b, int c, int d) {
macierz[0][0] = a;
macierz[0][1] = b;
macierz[1][0] = c;
macierz[1][1] = d;
}
Macierz2x2() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
macierz[i][j] = 0;
}
}
}
Macierz2x2 &operator+=(const Macierz2x2 &inna_macierz) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
macierz[i][j] += inna_macierz.macierz[i][j];
}
}
return *this;
}
Macierz2x2 operator+(const Macierz2x2 &inna_macierz) const {
Macierz2x2 m = *this;
m += inna_macierz;
return m;
}
Macierz2x2 &operator-=(const Macierz2x2 &inna_macierz) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
macierz[i][j] -= inna_macierz.macierz[i][j];
}
}
return *this;
}
Macierz2x2 operator-(const Macierz2x2 &inna_macierz) const {
Macierz2x2 m = *this;
m -= inna_macierz;
return m;
}
Macierz2x2 operator*(const Macierz2x2 &inna_macierz) const {
Macierz2x2 m;
m.macierz[0][0] = macierz[0][0] * inna_macierz.macierz[0][0] +
macierz[0][1] * inna_macierz.macierz[1][0];
m.macierz[0][1] = macierz[0][0] * inna_macierz.macierz[0][1] +
macierz[0][1] * inna_macierz.macierz[1][1];
m.macierz[1][0] = macierz[1][0] * inna_macierz.macierz[0][0] +
macierz[1][1] * inna_macierz.macierz[1][0];
m.macierz[1][1] = macierz[1][0] * inna_macierz.macierz[0][1] +
macierz[1][1] * inna_macierz.macierz[1][1];
return m;
}
int *operator[](int x) { return macierz[x]; }
const int *operator[](int x) const { return macierz[x]; }
friend std::ostream &operator<<(std::ostream &os, Macierz2x2 &macierz) {
for (int i = 0; i < SIZE; i++) {
os << '[';
for (int j = 0; j < SIZE; j++) {
os << macierz.macierz[i][j];
if (j < SIZE - 1)
os << " ";
}
os << ']';
os << '\n';
}
return os;
}
friend std::istream &operator>>(std::istream &is, Macierz2x2 &macierz) {
char separator;
is >> macierz.macierz[0][0] >> separator >> macierz.macierz[0][1] >>
separator >> macierz.macierz[1][0] >> separator >>
macierz.macierz[1][1];
return is;
}
explicit operator double() const {
return macierz[0][0] * macierz[1][1] - macierz[1][0] * macierz[0][1];
}
const int operator()(int i, int j) const { return macierz[i][j]; }
int &operator()(int i, int j) { return macierz[i][j]; }
};
int main() {
Macierz2x2 m1 = Macierz2x2(1, 2, 3, 4);
std::cout << "Macierz 1:\n";
std::cout << m1;
Macierz2x2 m2;
std::cout << "Podaj swoją macierz: \n";
std::cout << "Macierz 2:\n";
std::cin >> m2;
std::cout << m2;
Macierz2x2 m3 = m1 + m2;
Macierz2x2 m4 = m1 * m2;
std::cout << "Macierz 3(dodawanie):\n";
std::cout << m3 << std::endl;
std::cout << "Macierz 4(mnożenie):\n";
std::cout << m4 << std::endl;
std::cout << "Wyznacznik macierz m1: " << double(m1) << std::endl;
std::cout << "Element (1,1) macierzy m1: " << m1(1, 1) << std::endl;
;
}

View File

@@ -1,62 +0,0 @@
#include <iostream>
class Punkt2d {
public:
double x, y;
Punkt2d(double x, double y) : x(x), y(y) {}
Punkt2d() : x(0), y(0) {}
Punkt2d operator+=(const Punkt2d &inny_pkt) {
this->x += inny_pkt.x;
this->y += inny_pkt.y;
return *this;
}
Punkt2d operator-=(const Punkt2d &inny_pkt) {
this->x -= inny_pkt.x;
this->y -= inny_pkt.y;
return *this;
}
Punkt2d operator*=(const Punkt2d &inny_pkt) {
this->x *= inny_pkt.x;
this->y *= inny_pkt.y;
return *this;
}
Punkt2d operator+(const Punkt2d &inny_pkt) const {
Punkt2d p = *this;
p += inny_pkt;
return p;
}
Punkt2d operator-(const Punkt2d &inny_pkt) const {
Punkt2d pkt;
pkt.x = x - inny_pkt.x;
pkt.y = y - inny_pkt.y;
return pkt;
}
bool operator==(const Punkt2d &inny_pkt) const {
if (x != inny_pkt.x || y != inny_pkt.y) {
return false;
}
return true;
}
bool operator!=(const Punkt2d &inny_pkt) const {
return !(*this == inny_pkt);
}
friend std::ostream &operator<<(std::ostream &os, const Punkt2d &inny_pkt) {
os << '(' << inny_pkt.x << ',' << inny_pkt.y << ')';
return os;
}
};
int main() {
Punkt2d a(6, 7), b(3, 4);
Punkt2d c = a + b;
std::cout << c << std::endl; // (9, 11)
std::cout << (a == b) << std::endl; // false
std::cout << (a != a) << std::endl; // false
}

View File

@@ -0,0 +1,51 @@
#include <cstddef>
#include <iostream>
#include <stdexcept>
#include <string>
class Tablica {
int *arr;
void is_size_correct(size_t index) const {
if (index > size) {
throw std::out_of_range("Zbyt duży index! Maksymalny rozmiar to %d" +
std::to_string(size));
}
if (index < 0) {
throw std::out_of_range("Zbyt mały index! Minimalny rozmiar to 0");
}
}
public:
size_t size;
Tablica(size_t size) : size{size} { arr = new int[size]; }
// po zakomentowaniu tej metody program wciąż się kompiluje
int &operator[](size_t index) const {
is_size_correct(index);
return *(arr + index);
}
int &operator[](size_t index) {
is_size_correct(index);
return *(arr + index);
}
};
void przeczytaj_tablice(Tablica &tablica) {
for (int i = 0; i < tablica.size; i++) {
try {
std::cout << tablica[i] << '\n';
} catch (const std::out_of_range &e) {
std::cout << e.what();
continue;
}
}
}
int main() {
Tablica t = Tablica(10);
przeczytaj_tablice(t);
}

View File

@@ -26,6 +26,10 @@ public:
Ulamek() : licznik(0), mianownik(1) {}
explicit operator double() const {
return static_cast<double>(licznik) / mianownik;
}
Ulamek operator+(const Ulamek &inny_ulamek) const {
return Ulamek(licznik * inny_ulamek.mianownik +
inny_ulamek.licznik * mianownik,
@@ -79,37 +83,28 @@ public:
os << u.licznik << '/' << u.mianownik;
return os;
}
friend std::istream &operator>>(std::istream &is, Ulamek &u) {
char separator;
int licznik, mianownik;
is >> licznik >> separator >> mianownik;
if (mianownik == 0) {
is.setstate(std::ios::failbit); // nadmiarowe, bo w konstruktorze też
// sprawdzamy czy mianownik == 0
}
if (is) {
u = Ulamek(licznik, mianownik);
}
return is;
}
};
Ulamek stworz_ulamek() {
int licznik, mianownik;
std::cout << "Podaj licznik: ";
std::cin >> licznik;
std::cout << "Podaj mianownik: ";
std::cin >> mianownik;
return Ulamek(licznik, mianownik);
}
int main() {
Ulamek u1 = stworz_ulamek();
std::cout << "Ulamek 1: " << u1 << std::endl;
Ulamek u2 = stworz_ulamek();
std::cout << "Ulamek 2: " << u2 << std::endl;
std::cout << "=== OPERACJE ===" << std::endl;
std::cout << "Dodawanie: " << u1 + u2 << std::endl;
std::cout << "Odejmowanie: " << u1 - u2 << std::endl;
std::cout << "Mnożenie: " << u1 * u2 << std::endl;
try {
std::cout << "Dzielenie: " << u1 / u2 << std::endl;
} catch (const std::invalid_argument &e) {
std::cout << "Błąd: " << e.what() << std::endl;
}
std::cout << "=== PORÓWNYWANIE ===" << std::endl;
std::cout << std::boolalpha;
std::cout << u1 << " == " << u2 << ": " << (u1 == u2) << std::endl;
std::cout << u1 << " != " << u2 << ": " << (u1 != u2) << std::endl;
std::cout << u1 << " <= " << u2 << ": " << (u1 <= u2) << std::endl;
std::cout << u1 << " >= " << u2 << ": " << (u1 >= u2) << std::endl;
std::cout << u1 << " < " << u2 << ": " << (u1 < u2) << std::endl;
std::cout << u1 << " > " << u2 << ": " << (u1 > u2) << std::endl;
Ulamek ulamek;
std::cin >> ulamek;
// double ulamek_double = ulamek; ta linia się nie skompiluje, ponieważ k
// onwersja musi być explicit
double ulamek_double = double(ulamek);
std::cout << ulamek_double;
}

View File

@@ -0,0 +1,54 @@
#include <iostream>
class Wektor2D {
public:
double x, y;
Wektor2D(double x, double y) : x(x), y(y) {}
Wektor2D() : x(0), y(0) {}
Wektor2D operator+=(const Wektor2D &inny_pkt) {
this->x += inny_pkt.x;
this->y += inny_pkt.y;
return *this;
}
Wektor2D operator-=(const Wektor2D &inny_pkt) {
this->x -= inny_pkt.x;
this->y -= inny_pkt.y;
return *this;
}
Wektor2D operator*=(const Wektor2D &inny_pkt) {
this->x *= inny_pkt.x;
this->y *= inny_pkt.y;
return *this;
}
Wektor2D operator+(const Wektor2D &inny_pkt) const {
Wektor2D p = *this;
p += inny_pkt;
return p;
}
Wektor2D operator-(const Wektor2D &inny_pkt) const {
Wektor2D pkt;
pkt.x = x - inny_pkt.x;
pkt.y = y - inny_pkt.y;
return pkt;
}
bool operator==(const Wektor2D &inny_pkt) const {
if (x != inny_pkt.x || y != inny_pkt.y) {
return false;
}
return true;
}
bool operator!=(const Wektor2D &inny_pkt) const {
return !(*this == inny_pkt);
}
friend std::ostream &operator<<(std::ostream &os, const Wektor2D &inny_pkt) {
os << '(' << inny_pkt.x << ',' << inny_pkt.y << ')';
return os;
}
};

View File

@@ -0,0 +1,11 @@
#include "licznik.cpp"
#include "wektor.cpp"
int main() {
Wektor2D a(1, 2), b(3, 4);
a += b;
std::cout << a << std::endl; // (4,6)
Licznik c(10);
Licznik d = c++;
std::cout << c << " " << d << std::endl; // 11 10
}