Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- class Figura
- {
- protected:
- double obwod{0};
- public:
- virtual void oblicz_obwod()=0;
- virtual void wypisz() const=0;
- virtual ~Figura(){}
- };
- class Prostokat : public Figura
- {
- protected:
- double bok1;
- double bok2;
- public:
- Prostokat(double, double);
- void wypisz() const override;
- virtual void ustaw_bok1(double);
- virtual void ustaw_bok2(double);
- void oblicz_obwod() override;
- };
- class Kwadrat : public Prostokat
- {
- public:
- Kwadrat(double);
- void wypisz() const override;
- void ustaw_bok1(double) override;
- void ustaw_bok2(double) override;
- };
- //================================================================
- int main()
- {
- srand(time(nullptr));
- Prostokat *tab[]=
- {
- new Prostokat(rand()%30-5,rand()%30-5),
- new Kwadrat(rand()%30-5),
- new Kwadrat(rand()%30-5),
- new Prostokat(rand()%30-5,rand()%30-5)
- };
- int rozmiar=sizeof(tab)/sizeof(*tab);
- for(int i=0; i<rozmiar; ++i)
- {
- cout<<"\n---------------------------------------------------------";
- cout<<"\n"<<i+1<<".\n";
- tab[i]->wypisz();
- cout<<"\nZmiany\n";
- tab[i]->ustaw_bok1((rand()%100-20)/10.0);
- tab[i]->ustaw_bok2((rand()%100-20)/10.0);
- tab[i]->wypisz();
- }
- cout<<"\n---------------------------------------------------------\n";
- for(int i=0; i<rozmiar; ++i)
- delete tab[i];
- return 0;
- }
- //================================================================
- void Prostokat::wypisz() const
- {
- if(this->bok1!=0 && this->bok2!=0)
- {
- cout<<"Prostokat o bokach: "<<this->bok1<<", "<<this->bok2<<"; ";
- }
- else
- cout<<"Blad!!! Boki prostokata: "<<this->bok1<<", "<<this->bok2<<"; ";
- }
- void Prostokat::ustaw_bok1(double x)
- {
- if (x>0)
- {
- this->bok1=x;
- }
- }
- void Prostokat::ustaw_bok2(double x)
- {
- if (x>0)
- {
- this->bok2=x;
- }
- }
- void Kwadrat::ustaw_bok1(double a)
- {
- if(a>0)
- {
- bok1=a;
- bok2=a;
- }
- }
- void Kwadrat::ustaw_bok2(double a)
- {
- if(a>0)
- {
- bok1=a;
- bok2=a;
- }
- }
- Prostokat::Prostokat(double a,double b)
- :bok1(a>0?a:0),bok2(b>0?b:0)
- { }
- //skladowe Klasy Kwadrat
- Kwadrat::Kwadrat(double arg)
- :Prostokat(arg,arg)
- {}
- void Kwadrat::wypisz() const
- {
- if(this->bok1!=0 && this->bok1==this->bok2)
- {
- cout<<"Kwadrat o boku: "<<this->bok1<<"; ";
- }
- else
- cout<<"Blad!!! Boki kwadratu: "<<this->bok1<<", "<<this->bok2<<"; ";
- }
- void Prostokat::oblicz_obwod()
- {
- obwod=bok1*bok2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement