Advertisement
bazmikel

2K4

Jan 15th, 2020
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. class figura{
  7. private:
  8.   string * color;
  9. public:
  10.   figura(): color(new string("Brak coloru")){};
  11.   figura(const string & clr): color(new string(clr)){};
  12.   virtual double pole() = 0;
  13.   string getColor()const {return *color;}
  14.   virtual ~figura(){delete color;}
  15.  
  16.   virtual void print(ostream & out)const{
  17.       out << *color << " ";
  18.   }
  19.  
  20.   friend ostream & operator <<(ostream & out, const figura & obj);
  21. };
  22.  
  23. ostream & operator <<(ostream & out, const figura & obj){
  24.   obj.print(out);
  25.   return out;
  26. }
  27.  
  28.  
  29. class kolo: public figura{
  30. int r;
  31. public:
  32. kolo(): figura(), r(0){};
  33. kolo(const string & clr, const int & _r): figura(clr), r(_r){};
  34.  
  35.  double pole() {
  36.   return 3.14 * r * r;
  37. }
  38.  
  39. int getR()const {return r;}
  40.  
  41. virtual void print(ostream & out)const{
  42.       out << "Kolo o kolorze: \"" << getColor() << "\" i promieniu: " << getR();
  43. }
  44.  
  45. friend ostream & operator <<(ostream & out, const kolo & obj);
  46. };
  47.  
  48. ostream & operator <<(ostream & out, const kolo & obj){
  49.   obj.print(out);
  50.   return out;
  51. }
  52.  
  53. class prost: public figura{
  54. protected:
  55. int a, b;
  56. public:
  57. prost(): figura(), a(0), b(0){};
  58. prost(const string & clr, const int & _a, const int & _b): figura(clr), a(_a), b(_b){};
  59. double pole() {
  60.  return a * b;
  61. }
  62. int getA()const {return a;}
  63. int getB()const {return b;}
  64.  
  65. virtual void print(ostream & out)const{
  66.       out << "Prostokat o kolorze: \"" << getColor() << "\" i polach : " << getA() << " " << getB();
  67. }
  68.  
  69. friend ostream & operator <<(ostream & out, const prost & obj);
  70. };
  71.  
  72. ostream & operator <<(ostream & out, const prost & obj){
  73.   obj.print(out);
  74.   return out;
  75. }
  76.  
  77. class prostop: public prost{
  78.   int h;
  79. public:
  80.   prostop():  prost(), h(0){};
  81.   prostop(const string & clr, const int & _a, const int & _b, const int & _h): prost(clr, _a, _b), h(_h){};
  82.   double pole() {
  83.    return a * b * h;
  84.   }
  85.  
  86.   int getH() const{
  87.     return h;
  88.   }
  89.  
  90.   virtual void print(ostream & out)const{
  91.       out << "Prostopadloscian o kolorze: \"" << getColor() << "\" i polach : " << getA() << " " << getB() << getH();
  92. }
  93.  
  94.   friend ostream & operator <<(ostream & out, const prostop & obj);
  95. };
  96.  
  97. ostream & operator <<(ostream & out, const prostop & obj){
  98.   obj.print(out);
  99.   return out;
  100. }
  101.  
  102. int main(){
  103.   figura * tab[5];
  104.   const kolo test1("czarny", 100);
  105.   const prostop test2("szary", 2, 2, 2);
  106.   tab[0] = new kolo("czerwony", 1);
  107.   tab[1] = new kolo;
  108.   tab[2] = new prost("niebieski", 1, 1);
  109.   tab[3] = new prostop("zielony", 1, 1, 1);
  110.   tab[4] = new prostop;
  111.   for(int i = 0; i < 5; ++i)
  112.      cout << tab[i]->pole() << endl;
  113.   cout << "***********3*********" << endl;
  114.  
  115.  
  116.   for(int i = 0; i < 5; ++i)
  117.    cout << * tab[i] << tab[i]->pole() << endl;
  118.    cout << "***********4*********" << endl;
  119.  
  120.  
  121. for(int i = 0; i < 5; ++i)
  122.   delete tab[i];
  123.  
  124.  
  125.   return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement