Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class figura {
- public:
- virtual double pole()const = 0;
- virtual ostream& wyswietl(ostream& out)const = 0;
- virtual ~figura() {}
- };
- class kolo : public figura {
- string* txt;
- double l;
- public:
- kolo() :txt(new string("brak")), l(0) {}
- kolo(const string& a1, const double& a2) :txt(new string(a1)), l(a2) {}
- kolo(const kolo& r) :txt(new string(*r.txt)), l(r.l) {}
- kolo& operator=(const kolo& r) {
- if (this != &r) {
- delete txt;
- txt = new string(*r.txt);
- l = r.l;
- }return *this;
- }
- double pole() const { return 3.14 * l * l; }
- ostream& wyswietl(ostream& out)const { return out << *txt << " " << l; }
- ~kolo() { delete txt; }
- };
- class prost : public figura {
- protected:
- string* txt;
- double a;
- double b;
- public:
- prost() :txt(new string("brak")), a(0), b(0) {}
- prost(const string& a1, const double& a2, const double& a3) :txt(new string(a1)), a(a2), b(a3) {}
- prost(const prost& r) :txt(new string(*r.txt)), a(r.a), b(r.b) {}
- prost& operator=(const prost& r) {
- if (this != &r) {
- delete txt;
- txt = new string(*r.txt);
- a = r.a;
- b = r.b;
- }return *this;
- }
- double pole()const { return a * b; }
- ostream& wyswietl(ostream& out)const { return out << *txt << " " << a << " " << b; }
- ~prost() { delete txt; }
- };
- class prostop : public prost {
- double h;
- public:
- prostop() :prost(), h(0) {}
- prostop(const string& a1, const double& a2, const double& a3, const double& a4) :prost(a1, a2, a3), h(a4) {}
- prostop(const prostop& r) :prost(r), h(r.h) {}
- double pole()const { return 2 * a * b + 2 * a * h + 2 * b * h; }
- ostream& wyswietl(ostream& out)const { return out << *txt << " " << a << " " << b << " " << h; }
- };
- ostream& operator<<(ostream& out, const figura& r) {
- return r.wyswietl(out) << endl;
- }
- int main()
- {
- figura* tab[5];
- const kolo test1("czarny", 100);
- const prost test2("szary", 2.2, 2);
- tab[0] = new kolo("czerwony", 100);
- tab[1] = new kolo;
- tab[2] = new prost("niebieski", 1, 1);
- tab[3] = new prostop("zielony", 1, 1, 1);
- tab[4] = new prostop;
- for (int i = 0; i < 5; ++i) {
- cout << tab[i]->pole() << endl;
- }
- cout << "************* 3 ************" << endl;
- for (int i = 0; i < 5; ++i) {
- cout << *tab[i] << tab[i]->pole() << endl;
- delete tab[i];
- }
- cout << "************* 4 ************" << endl;
- cout << "************* 5 ************" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement