Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #define _USE_MATH_DEFINES
- #include <cmath>
- #include <vector>
- #define M_PI 3.14159265358979323846
- class Figura {
- public:
- virtual float pole() = 0;
- };
- class Kwadrat : public Figura {
- protected:
- int A; // bok kwadratu
- public:
- Kwadrat(int a) : A(a) {} // konstruktor wraz z listą inicjalizacyjną
- float pole() { return A*A; } // implementacja metody pole() z klasy bazowej
- };
- class Kolo : public Figura {
- protected:
- int A; // promien kola
- public:
- Kolo(int a) : A(a) {} // konstruktor wraz z listą inicjalizacyjną
- float pole() { return M_PI*A*A; } // implementacja metody pole() z klasy bazowej
- };
- class Prostokat : public Figura {
- protected:
- int A, B; // boki prostokąta
- public:
- Prostokat(int a, int b) : A(a), B(b) {} // konstruktor wraz z listą inicjalizacyjną
- float pole() { return A*B; } // implementacja metody pole() z klasy bazowej
- };
- void porownaj(Figura* a, Figura* b)
- {
- if (a->pole() == b->pole())
- {
- std::cout << "Pola sa rowne" << std::endl;
- }
- else if (a->pole() > b->pole())
- {
- std::cout << "Pole pierwszej wylosowanej figury jest wieksze." << std::endl;
- }
- else
- {
- std::cout << "Pole drugiej wylosowanej figury jest wieksze." << std::endl;
- }
- }
- Figura * losujFigure() {
- int figura;
- figura = rand() % 3;
- switch (figura) {
- case 0:
- {
- int a = rand() % 100;
- std::cout << "wylosowano kwadrat o boku: " << a << std::endl;
- return new Kwadrat(a);
- break;
- }
- case 1:
- {
- int a = rand() % 100;
- int b = rand() % 100;
- std::cout << "wylosowano prostokat o bokach: " << a << " i " << b << std::endl;
- return new Prostokat(a,b);
- break;
- }
- case 2:
- {
- int a = rand() % 100;
- std::cout << "wylosowano kolo o promieniu: " << a << std::endl;
- return new Kolo(a);
- break;
- }
- }
- }
- int main(void)
- {
- srand(time(0));
- std::vector<Figura*> figury;
- figury.push_back(losujFigure());
- figury.push_back(losujFigure());
- porownaj(figury[0], figury[1]);
- for (auto v : figury) { delete v; } // usuwa dane z pamieci
- figury.clear(); // usuwa elementy z contenera (zwija kontener)
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement