mirror of
https://github.com/szymon-jozef/ZUT-notatki.git
synced 2026-10-27 19:20:39 +01:00
bla bla
This commit is contained in:
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad1
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad1
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad10
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad10
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad11
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad11
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad12
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad12
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad3
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad3
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad4
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad4
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad5
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad5
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad6
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad6
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad7
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad7
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad8
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad8
Executable file
Binary file not shown.
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad9
Executable file
BIN
uczelnia/2_semestr/programowanie/laby/lab11/out/zad9
Executable file
Binary file not shown.
64
uczelnia/2_semestr/programowanie/laby/lab11/zad1.cpp
Normal file
64
uczelnia/2_semestr/programowanie/laby/lab11/zad1.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Ksztalt {
|
||||
public:
|
||||
virtual ~Ksztalt() = default;
|
||||
virtual double pole() = 0;
|
||||
virtual std::string opis() = 0;
|
||||
};
|
||||
|
||||
class Kolo : public Ksztalt {
|
||||
int r;
|
||||
|
||||
public:
|
||||
Kolo(int r) : r(r) {}
|
||||
|
||||
double pole() override { return r * r * M_PI; }
|
||||
std::string opis() override { return "To jest koło";}
|
||||
|
||||
~Kolo() override {}
|
||||
};
|
||||
|
||||
class Trojkat : public Ksztalt {
|
||||
int a, h;
|
||||
|
||||
public:
|
||||
Trojkat(int a, int h) : a(a), h(h) {}
|
||||
|
||||
double pole() override { return a * h * 0.5; }
|
||||
std::string opis() override { return "To jest trójkąt";}
|
||||
|
||||
~Trojkat() override {}
|
||||
};
|
||||
|
||||
class Kwadrat : public Ksztalt {
|
||||
int a;
|
||||
|
||||
public:
|
||||
Kwadrat(int a) : a(a) {}
|
||||
|
||||
double pole() override { return a * a; }
|
||||
std::string opis() override { return " To jest kwadrat"; }
|
||||
|
||||
~Kwadrat() override {}
|
||||
};
|
||||
|
||||
double sum_pole(std::vector<std::unique_ptr<Ksztalt>> &vec) {
|
||||
double sum = 0;
|
||||
for (auto &ksztalt : vec) {
|
||||
sum += ksztalt->pole();
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::vector<std::unique_ptr<Ksztalt>> vec;
|
||||
vec.push_back(std::make_unique<Trojkat>(2, 4));
|
||||
vec.push_back(std::make_unique<Kolo>(2));
|
||||
vec.push_back(std::make_unique<Kwadrat>(4));
|
||||
std::cout << "Suma pól wszystkich figur to: " << sum_pole(vec) << std::endl;
|
||||
}
|
||||
51
uczelnia/2_semestr/programowanie/laby/lab11/zad10.cpp
Normal file
51
uczelnia/2_semestr/programowanie/laby/lab11/zad10.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
class StrategiaPodatkowa {
|
||||
public:
|
||||
virtual double oblicz(double kwota) const = 0;
|
||||
virtual ~StrategiaPodatkowa() = default;
|
||||
};
|
||||
|
||||
class PodatekLiniowy : public StrategiaPodatkowa {
|
||||
const double procent = 0.19;
|
||||
|
||||
public:
|
||||
double oblicz(double kwota) const override { return kwota * procent; }
|
||||
};
|
||||
|
||||
class PodatekProgresywny : public StrategiaPodatkowa {
|
||||
const double prog_1 = 0.19;
|
||||
const double prog_2 = 0.8;
|
||||
|
||||
public:
|
||||
double oblicz(double kwota) const override {
|
||||
return (kwota < 4000) ? (kwota * prog_1) : (kwota * prog_2);
|
||||
}
|
||||
};
|
||||
|
||||
class BrakPodatku : public StrategiaPodatkowa {
|
||||
public:
|
||||
double oblicz(double) const override { return 0.0; }
|
||||
};
|
||||
|
||||
class Faktura {
|
||||
std::unique_ptr<StrategiaPodatkowa> strategia;
|
||||
double kwota;
|
||||
|
||||
public:
|
||||
Faktura(std::unique_ptr<StrategiaPodatkowa> s, double kwota)
|
||||
: strategia(std::move(s)), kwota(kwota) {}
|
||||
|
||||
double kwotaDoZaplaty() const { return strategia->oblicz(kwota) + kwota; }
|
||||
};
|
||||
|
||||
int main() {
|
||||
Faktura faktura_socjalistyczna(std::make_unique<PodatekProgresywny>(), 5000);
|
||||
Faktura faktura_wolnorynkowa(std::make_unique<PodatekLiniowy>(), 5000);
|
||||
Faktura faktura_libertalinistyczna(std::make_unique<BrakPodatku>(), 5000);
|
||||
|
||||
std::cout << faktura_socjalistyczna.kwotaDoZaplaty() << std::endl;
|
||||
std::cout << faktura_wolnorynkowa.kwotaDoZaplaty() << std::endl;
|
||||
std::cout << faktura_libertalinistyczna.kwotaDoZaplaty() << std::endl;
|
||||
}
|
||||
111
uczelnia/2_semestr/programowanie/laby/lab11/zad11.cpp
Normal file
111
uczelnia/2_semestr/programowanie/laby/lab11/zad11.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Zwierze {
|
||||
public:
|
||||
virtual std::string odglos() = 0;
|
||||
virtual std::string jedzenie() = 0;
|
||||
virtual double ileKalorii() = 0;
|
||||
virtual ~Zwierze() = default;
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, Zwierze &z) {
|
||||
os << "To zwierze wydaje dźwięk: " << z.odglos() << ".\nJe " << z.jedzenie()
|
||||
<< " i potrzebuje " << z.ileKalorii() << " kalorii dziennie\n";
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
class Lew : public Zwierze {
|
||||
public:
|
||||
std::string odglos() override { return "Odgłos lwa"; }
|
||||
std::string jedzenie() override { return "Mięso";}
|
||||
double ileKalorii() override { return 2348; }
|
||||
};
|
||||
|
||||
class Slon : public Zwierze {
|
||||
public:
|
||||
std::string odglos() override { return "Odgłos słonia"; }
|
||||
std::string jedzenie() override { return "Liście";}
|
||||
double ileKalorii() override { return 23; }
|
||||
};
|
||||
|
||||
class Papuga : public Zwierze {
|
||||
public:
|
||||
std::string odglos() override { return "Odgłos papugii"; }
|
||||
std::string jedzenie() override { return "Ziarna"; }
|
||||
double ileKalorii() override { return 2; }
|
||||
};
|
||||
|
||||
class Waz : public Zwierze {
|
||||
public:
|
||||
std::string odglos() override { return "Odgłos węża"; }
|
||||
std::string jedzenie() override { return "Mięso"; }
|
||||
double ileKalorii() override { return 387; }
|
||||
};
|
||||
|
||||
class Opiekun {
|
||||
public:
|
||||
void nakarm() { std::cout << "Nakarmione" << std::endl; }
|
||||
};
|
||||
|
||||
class Wybieg {
|
||||
Opiekun opiekun_wybiegu;
|
||||
|
||||
public:
|
||||
std::vector<std::unique_ptr<Zwierze>> zwierzeta;
|
||||
|
||||
public:
|
||||
Wybieg(std::vector<std::unique_ptr<Zwierze>> zwierzeta,
|
||||
Opiekun opiekun_wybiegu)
|
||||
: zwierzeta(std::move(zwierzeta)), opiekun_wybiegu(opiekun_wybiegu) {}
|
||||
};
|
||||
|
||||
class Zoo {
|
||||
std::vector<std::unique_ptr<Wybieg>> wybiegi;
|
||||
|
||||
public:
|
||||
Zoo(std::vector<std::unique_ptr<Wybieg>> wybiegi)
|
||||
: wybiegi(std::move(wybiegi)) {}
|
||||
|
||||
double dziennaPorcjaKalorii() {
|
||||
double sum = 0;
|
||||
for (auto &wybieg : wybiegi) {
|
||||
for (const auto &zwierze : wybieg->zwierzeta) {
|
||||
sum += zwierze->ileKalorii();
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, Zoo &z) {
|
||||
os << '[';
|
||||
for (auto &wybieg : z.wybiegi) {
|
||||
for (const auto &zwierze : wybieg->zwierzeta) {
|
||||
os << *zwierze << ',';
|
||||
}
|
||||
}
|
||||
os << ']';
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::vector<std::unique_ptr<Zwierze>> zwierzeta;
|
||||
zwierzeta.push_back(std::make_unique<Waz>());
|
||||
zwierzeta.push_back(std::make_unique<Slon>());
|
||||
zwierzeta.push_back(std::make_unique<Lew>());
|
||||
|
||||
Opiekun opiekun;
|
||||
std::vector<std::unique_ptr<Wybieg>> wybiegi;
|
||||
|
||||
wybiegi.push_back(std::make_unique<Wybieg>(std::move(zwierzeta), opiekun));
|
||||
|
||||
Zoo zoo = Zoo(std::move(wybiegi));
|
||||
std::cout << "Dzienna porcja kalorii: " << zoo.dziennaPorcjaKalorii()
|
||||
<< std::endl;
|
||||
|
||||
std::cout << zoo;
|
||||
}
|
||||
179
uczelnia/2_semestr/programowanie/laby/lab11/zad12.cpp
Normal file
179
uczelnia/2_semestr/programowanie/laby/lab11/zad12.cpp
Normal file
@@ -0,0 +1,179 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class StrategiaZywienia {
|
||||
public:
|
||||
virtual std::string pobierzOpisStrategii() const = 0;
|
||||
virtual ~StrategiaZywienia() = default;
|
||||
};
|
||||
|
||||
class Drapieznik : public StrategiaZywienia {
|
||||
public:
|
||||
std::string pobierzOpisStrategii() const override {
|
||||
return "Drapieznik (wymaga miesa)";
|
||||
}
|
||||
};
|
||||
|
||||
class Roslinozerca : public StrategiaZywienia {
|
||||
public:
|
||||
std::string pobierzOpisStrategii() const override {
|
||||
return "Roslinozerca (wymaga roslin)";
|
||||
}
|
||||
};
|
||||
|
||||
class Wszystkozerca : public StrategiaZywienia {
|
||||
public:
|
||||
std::string pobierzOpisStrategii() const override {
|
||||
return "Wszystkozerca (je wszystko)";
|
||||
}
|
||||
};
|
||||
|
||||
class Zwierze {
|
||||
protected:
|
||||
std::unique_ptr<StrategiaZywienia> strategia;
|
||||
|
||||
public:
|
||||
Zwierze(std::unique_ptr<StrategiaZywienia> strat)
|
||||
: strategia(std::move(strat)) {}
|
||||
|
||||
virtual std::string odglos() = 0;
|
||||
virtual std::string jedzenie() = 0;
|
||||
virtual double ileKalorii() = 0;
|
||||
virtual ~Zwierze() = default;
|
||||
|
||||
void ustawStrategie(std::unique_ptr<StrategiaZywienia> nowaStrategia) {
|
||||
strategia = std::move(nowaStrategia);
|
||||
}
|
||||
|
||||
std::string pokazJakJe() const {
|
||||
return strategia ? strategia->pobierzOpisStrategii() : "Brak strategii";
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, Zwierze &z) {
|
||||
os << "To zwierze wydaje dzwiek: " << z.odglos() << ".\nJe " << z.jedzenie()
|
||||
<< ", potrzebuje " << z.ileKalorii()
|
||||
<< " kcal. Styl zycia: " << z.pokazJakJe() << "\n";
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
class Lew : public Zwierze {
|
||||
public:
|
||||
Lew() : Zwierze(std::make_unique<Drapieznik>()) {}
|
||||
std::string odglos() override { return "Odgłos lwa"; }
|
||||
std::string jedzenie() override { return "Mięso";}
|
||||
double ileKalorii() override { return 2348; }
|
||||
};
|
||||
|
||||
class Slon : public Zwierze {
|
||||
public:
|
||||
Slon() : Zwierze(std::make_unique<Roslinozerca>()) {}
|
||||
std::string odglos() override { return "Odgłos słonia"; }
|
||||
std::string jedzenie() override { return "Liście";}
|
||||
double ileKalorii() override { return 20000; }
|
||||
};
|
||||
|
||||
class Papuga : public Zwierze {
|
||||
public:
|
||||
Papuga() : Zwierze(std::make_unique<Roslinozerca>()) {}
|
||||
std::string odglos() override { return "Odgłos papugii"; }
|
||||
std::string jedzenie() override { return "Ziarna"; }
|
||||
double ileKalorii() override { return 2; }
|
||||
};
|
||||
|
||||
class Waz : public Zwierze {
|
||||
public:
|
||||
Waz() : Zwierze(std::make_unique<Drapieznik>()) {}
|
||||
std::string odglos() override { return "Odgłos węża"; }
|
||||
std::string jedzenie() override { return "Mięso"; }
|
||||
double ileKalorii() override { return 387; }
|
||||
};
|
||||
|
||||
std::unique_ptr<Zwierze> stworzZwierze(const std::string &gatunek) {
|
||||
if (gatunek == "Lew" || gatunek == "lew") {
|
||||
return std::make_unique<Lew>();
|
||||
} else if (gatunek == "Slon" || gatunek == "slon") {
|
||||
return std::make_unique<Slon>();
|
||||
} else if (gatunek == "Papuga" || gatunek == "papuga") {
|
||||
return std::make_unique<Papuga>();
|
||||
} else if (gatunek == "Waz" || gatunek == "waz") {
|
||||
return std::make_unique<Waz>();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
class Opiekun {
|
||||
public:
|
||||
void nakarm() { std::cout << "Nakarmione" << std::endl; }
|
||||
};
|
||||
|
||||
class Wybieg {
|
||||
Opiekun opiekun_wybiegu;
|
||||
|
||||
public:
|
||||
std::vector<std::unique_ptr<Zwierze>> zwierzeta;
|
||||
Wybieg(std::vector<std::unique_ptr<Zwierze>> zwierzeta,
|
||||
Opiekun opiekun_wybiegu)
|
||||
: zwierzeta(std::move(zwierzeta)), opiekun_wybiegu(opiekun_wybiegu) {}
|
||||
};
|
||||
|
||||
class Zoo {
|
||||
std::vector<std::unique_ptr<Wybieg>> wybiegi;
|
||||
|
||||
public:
|
||||
Zoo(std::vector<std::unique_ptr<Wybieg>> wybiegi)
|
||||
: wybiegi(std::move(wybiegi)) {}
|
||||
|
||||
double dziennaPorcjaKalorii() {
|
||||
double sum = 0;
|
||||
for (auto &wybieg : wybiegi) {
|
||||
for (const auto &zwierze : wybieg->zwierzeta) {
|
||||
sum += zwierze->ileKalorii();
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, Zoo &z) {
|
||||
os << "[\n";
|
||||
for (auto &wybieg : z.wybiegi) {
|
||||
for (const auto &zwierze : wybieg->zwierzeta) {
|
||||
os << " " << *zwierze;
|
||||
}
|
||||
}
|
||||
os << ']';
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::vector<std::unique_ptr<Zwierze>> zwierzeta;
|
||||
|
||||
zwierzeta.push_back(stworzZwierze("Waz"));
|
||||
zwierzeta.push_back(stworzZwierze("Slon"));
|
||||
zwierzeta.push_back(stworzZwierze("Lew"));
|
||||
|
||||
std::cout << "Przed zmianą strategii" << std::endl;
|
||||
std::cout << *zwierzeta[2];
|
||||
|
||||
std::cout << "\n Dynamiczna podmiana strategii dla Lwa..." << std::endl;
|
||||
zwierzeta[2]->ustawStrategie(std::make_unique<Wszystkozerca>());
|
||||
|
||||
std::cout << *zwierzeta[2];
|
||||
std::cout << "-------------------------------------------\n" << std::endl;
|
||||
|
||||
Opiekun opiekun;
|
||||
std::vector<std::unique_ptr<Wybieg>> wybiegi;
|
||||
wybiegi.push_back(std::make_unique<Wybieg>(std::move(zwierzeta), opiekun));
|
||||
|
||||
Zoo zoo = Zoo(std::move(wybiegi));
|
||||
std::cout << "Dzienna porcja kalorii w calym zoo: "
|
||||
<< zoo.dziennaPorcjaKalorii() << " kcal\n"
|
||||
<< std::endl;
|
||||
std::cout << zoo << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
21
uczelnia/2_semestr/programowanie/laby/lab11/zad2.cpp
Normal file
21
uczelnia/2_semestr/programowanie/laby/lab11/zad2.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
class A {
|
||||
public:
|
||||
virtual void f() = 0;
|
||||
virtual void g() = 0;
|
||||
virtual void h() = 0;
|
||||
virtual ~A() = default;
|
||||
};
|
||||
|
||||
class B : public A {
|
||||
public:
|
||||
void f() override { return; }
|
||||
void g() override final { return; }
|
||||
void h() override { return; }
|
||||
~B() { return; }
|
||||
};
|
||||
|
||||
class C final : public B {
|
||||
void f() override final { return; }
|
||||
void g() override final { return; } // błąd
|
||||
void h() override final { return; }
|
||||
};
|
||||
34
uczelnia/2_semestr/programowanie/laby/lab11/zad3.cpp
Normal file
34
uczelnia/2_semestr/programowanie/laby/lab11/zad3.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
class Czlowiek {
|
||||
public:
|
||||
virtual void mowa() const { std::cout << "..." << std::endl; }
|
||||
virtual ~Czlowiek() = default;
|
||||
};
|
||||
|
||||
class Polak : public Czlowiek {
|
||||
public:
|
||||
void mowa() const override { std::cout << "Czesc!" << std::endl; }
|
||||
};
|
||||
|
||||
class Anglik : public Czlowiek {
|
||||
public:
|
||||
void mowa() const override { std::cout << "Hello!" << std::endl; }
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::vector<std::unique_ptr<Czlowiek>> ludzie;
|
||||
ludzie.push_back(std::make_unique<Polak>());
|
||||
ludzie.push_back(std::make_unique<Anglik>());
|
||||
ludzie.push_back(std::make_unique<Czlowiek>());
|
||||
for (const auto &c : ludzie)
|
||||
c->mowa();
|
||||
// Cześć!
|
||||
// Hello!
|
||||
// ...
|
||||
|
||||
// Przy metodzie niewirtualnej pojawi się błąd kompilacji – nadpisujemy meto
|
||||
// dę, która nie jest wirtualna
|
||||
}
|
||||
45
uczelnia/2_semestr/programowanie/laby/lab11/zad4.cpp
Normal file
45
uczelnia/2_semestr/programowanie/laby/lab11/zad4.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <iostream>
|
||||
|
||||
class Pojazd {
|
||||
public:
|
||||
virtual ~Pojazd() = default;
|
||||
virtual void wydajDzwiekWirtualnie() {} // metoda wirtualna
|
||||
};
|
||||
|
||||
class Samochod : public Pojazd {
|
||||
public:
|
||||
void sygnal() { std::cout << "Sygnał!"; }
|
||||
void wydajDzwiekWirtualnie() override { std::cout << "Sygnał!"; }
|
||||
};
|
||||
|
||||
class Rower : public Pojazd {
|
||||
public:
|
||||
void Dzwonek() { std::cout << "Dzyń dzyń!"; }
|
||||
void wydajDzwiekWirtualnie() override { std::cout << "Dzyń dzyń!"; }
|
||||
};
|
||||
|
||||
class Motocykl : public Pojazd {
|
||||
public:
|
||||
void Warkot() { std::cout << "Wrum wrum!"; }
|
||||
void wydajDzwiekWirtualnie() override { std::cout << "Wrum wrum!"; }
|
||||
};
|
||||
|
||||
void wydajDzwiek(Pojazd *p) {
|
||||
if (Samochod *s = dynamic_cast<Samochod *>(p)) {
|
||||
s->sygnal();
|
||||
} else if (Rower *r = dynamic_cast<Rower *>(p)) {
|
||||
r->Dzwonek();
|
||||
} else if (Motocykl *m = dynamic_cast<Motocykl *>(p)) {
|
||||
m->Warkot();
|
||||
}
|
||||
}
|
||||
|
||||
void wydajDzwiek2(Pojazd &p) { p.wydajDzwiekWirtualnie(); }
|
||||
|
||||
int main() {
|
||||
Rower r2;
|
||||
Pojazd *r = &r2;
|
||||
|
||||
wydajDzwiek(r);
|
||||
wydajDzwiek2(r2);
|
||||
}
|
||||
48
uczelnia/2_semestr/programowanie/laby/lab11/zad5.cpp
Normal file
48
uczelnia/2_semestr/programowanie/laby/lab11/zad5.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <typeinfo>
|
||||
|
||||
class Pojazd {
|
||||
public:
|
||||
virtual ~Pojazd() = default;
|
||||
virtual void wydajDzwiekWirtualnie() {} // metoda wirtualna
|
||||
};
|
||||
|
||||
class Samochod : public Pojazd {
|
||||
public:
|
||||
void sygnal() { std::cout << "Sygnał!"; }
|
||||
void wydajDzwiekWirtualnie() override { std::cout << "Sygnał!"; }
|
||||
};
|
||||
|
||||
class Rower : public Pojazd {
|
||||
public:
|
||||
void Dzwonek() { std::cout << "Dzyń dzyń!"; }
|
||||
void wydajDzwiekWirtualnie() override { std::cout << "Dzyń dzyń!"; }
|
||||
};
|
||||
|
||||
class Motocykl : public Pojazd {
|
||||
public:
|
||||
void Warkot() { std::cout << "Wrum wrum!"; }
|
||||
void wydajDzwiekWirtualnie() override { std::cout << "Wrum wrum!"; }
|
||||
};
|
||||
|
||||
class NiepolimorficznyTyp {};
|
||||
class NiepolimorficznyTyp2 : public NiepolimorficznyTyp {};
|
||||
|
||||
void pokazTyp(const Pojazd *p) {
|
||||
const std::type_info &ti = typeid(*p);
|
||||
std::cout << "Rzeczywisty typ: " << ti.name() << std::endl;
|
||||
}
|
||||
|
||||
void pokazTyp2(const NiepolimorficznyTyp *n) {
|
||||
const std::type_info &ti = typeid(*n);
|
||||
std::cout << "Rzeczywisty typ: " << ti.name() << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Pojazd p;
|
||||
Samochod s;
|
||||
NiepolimorficznyTyp2 npol;
|
||||
pokazTyp(&p);
|
||||
pokazTyp(&s);
|
||||
pokazTyp2(&npol);
|
||||
}
|
||||
42
uczelnia/2_semestr/programowanie/laby/lab11/zad6.cpp
Normal file
42
uczelnia/2_semestr/programowanie/laby/lab11/zad6.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
#include <typeinfo>
|
||||
|
||||
class Pojazd {
|
||||
public:
|
||||
virtual ~Pojazd() = default;
|
||||
virtual void wydajDzwiekWirtualnie() {} // metoda wirtualna
|
||||
};
|
||||
|
||||
class Samochod : public Pojazd {
|
||||
public:
|
||||
void sygnal() { std::cout << "Sygnał!"; }
|
||||
void wydajDzwiekWirtualnie() override { std::cout << "Sygnał!"; }
|
||||
};
|
||||
|
||||
class Rower : public Pojazd {
|
||||
public:
|
||||
void Dzwonek() { std::cout << "Dzyń dzyń!"; }
|
||||
void wydajDzwiekWirtualnie() override { std::cout << "Dzyń dzyń!"; }
|
||||
};
|
||||
|
||||
class Motocykl : public Pojazd {
|
||||
public:
|
||||
void Warkot() { std::cout << "Wrum wrum!"; }
|
||||
void wydajDzwiekWirtualnie() override { std::cout << "Wrum wrum!"; }
|
||||
};
|
||||
|
||||
int main() {
|
||||
Rower r;
|
||||
Pojazd *p = &r;
|
||||
|
||||
Samochod *s_static = static_cast<Samochod *>(p);
|
||||
|
||||
std::cout
|
||||
<< "Wynik static_cast: " << s_static
|
||||
<< "\n"; // miejsce w pamięci. dowolne wywołanie metody zwróci teraz błąd
|
||||
|
||||
Samochod *s_dynamic = dynamic_cast<Samochod *>(p);
|
||||
|
||||
std::cout << "Wynik dynamic_cast: " << s_dynamic
|
||||
<< "\n"; // nullptr, bo sprawdził w runtime, że typ się nie zgadza
|
||||
}
|
||||
48
uczelnia/2_semestr/programowanie/laby/lab11/zad7.cpp
Normal file
48
uczelnia/2_semestr/programowanie/laby/lab11/zad7.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
template <typename T>
|
||||
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
|
||||
os << "[";
|
||||
for (size_t i = 0; i < v.size(); ++i) {
|
||||
os << v[i];
|
||||
if (i != v.size() - 1)
|
||||
os << ", ";
|
||||
}
|
||||
os << "]";
|
||||
return os;
|
||||
}
|
||||
|
||||
template <typename T1, typename T2> class Para {
|
||||
T1 pierwszy;
|
||||
T2 drugi;
|
||||
|
||||
public:
|
||||
Para(const T1 &p, const T2 &d) : pierwszy(p), drugi(d) {}
|
||||
|
||||
const T1 &pobierzPierwszy() const { return pierwszy; }
|
||||
const T2 &pobierzDrugi() const { return drugi; }
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const Para<T1, T2> ¶) {
|
||||
os << "(" << para.pierwszy << ", " << para.drugi << ")";
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Para<int, std::string> p1(42, "Tralala");
|
||||
std::cout << "Para 1: " << p1 << "\n";
|
||||
std::cout << "Pierwszy element p1: " << p1.pobierzPierwszy() << "\n\n";
|
||||
|
||||
Para<double, double> p2(3.1415, 2.7182);
|
||||
std::cout << "Para 2: " << p2 << "\n";
|
||||
std::cout << "Drugi element p2: " << p2.pobierzDrugi() << "\n\n";
|
||||
|
||||
std::vector<int> liczby = {1, 1, 2, 3, 5, 8};
|
||||
Para<std::string, std::vector<int>> p3("Fibonacci", liczby);
|
||||
|
||||
std::cout << "Para 3: " << p3 << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
33
uczelnia/2_semestr/programowanie/laby/lab11/zad8.cpp
Normal file
33
uczelnia/2_semestr/programowanie/laby/lab11/zad8.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <bitset>
|
||||
#include <iostream>
|
||||
|
||||
template <typename T> class Liczalna {
|
||||
public:
|
||||
void zliczDoN(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
static_cast<T *>(this)->wypisz(i);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class Dziesietna : public Liczalna<Dziesietna> {
|
||||
public:
|
||||
void wypisz(int liczba) { std::cout << "Dziesietnie: " << liczba << "\n"; }
|
||||
};
|
||||
|
||||
class Binarna : public Liczalna<Binarna> {
|
||||
public:
|
||||
void wypisz(int liczba) {
|
||||
std::cout << "Binarnie: " << std::bitset<8>(liczba) << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Dziesietna d;
|
||||
d.zliczDoN(4);
|
||||
|
||||
Binarna b;
|
||||
b.zliczDoN(4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
30
uczelnia/2_semestr/programowanie/laby/lab11/zad9.cpp
Normal file
30
uczelnia/2_semestr/programowanie/laby/lab11/zad9.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class Pracownik {
|
||||
std::string imie;
|
||||
|
||||
public:
|
||||
Pracownik(const std::string &i) : imie(i) {}
|
||||
virtual double pensja() const { return 0; }
|
||||
virtual void wypisz() const {
|
||||
std::cout << imie << ", pensja: " << pensja() << std::endl;
|
||||
}
|
||||
virtual ~Pracownik() {}
|
||||
};
|
||||
|
||||
class Etatowy : public Pracownik {
|
||||
double stawka;
|
||||
|
||||
public:
|
||||
Etatowy(const std::string &i, double s) : Pracownik(i), stawka(s) {}
|
||||
double pensja() const override { return stawka; }
|
||||
};
|
||||
|
||||
void przetworz(Pracownik &p) { p.wypisz(); }
|
||||
|
||||
int main() {
|
||||
Pracownik *tab[] = {new Etatowy("Ala", 5000)};
|
||||
przetworz(*tab[0]);
|
||||
delete tab[0];
|
||||
}
|
||||
Reference in New Issue
Block a user