StefiIOE

Shapes lab OOP polimorfizam

May 13th, 2020
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. class Shape
  5. {
  6.     protected:
  7.     int a;
  8.     public:
  9.     Shape(int a = 0)
  10.     {
  11.     this->a=a;
  12.     }
  13.     virtual double ploshtina ()=0;
  14.     virtual void pecati()=0;
  15.     virtual int getType()=0;
  16. };
  17. class Square :public Shape
  18. {
  19.     public:
  20.     Square(int a = 0) :Shape(a){}
  21.    
  22.    
  23.     double ploshtina()
  24.     {
  25.     return a*a;
  26.     }
  27.    
  28.     void pecati()
  29.     {
  30.     cout<<"Kvadrat so plostina = "<<ploshtina()<<endl;
  31.     }
  32.     int getType()
  33.     {
  34.         return 1;
  35.     }
  36.    
  37. };
  38. class Circle :public Shape
  39. {
  40.     public:
  41.     Circle(int a=0): Shape(a){}
  42.    
  43.     double ploshtina()
  44.     {
  45.     return 3.14*a *a;
  46.     }
  47.     void pecati()
  48.     {
  49.     cout<<"Krug so plostina = "<<ploshtina()<<endl;
  50.     }
  51.     int getType()
  52.     {
  53.     return 2;
  54.     }
  55.    
  56.    
  57. };
  58. class Triangle:public Shape
  59. {
  60.     public:
  61.     Triangle(int a=0) :Shape(a){}
  62.    
  63.     double ploshtina()
  64.     {
  65.     return (sqrt(3)/4) * a * a;
  66.     }
  67.     void pecati()
  68.     {
  69.     cout<<"Triagolnik so plostina = "<<ploshtina()<<endl;
  70.     }
  71.     int getType(){
  72.     return 3;
  73.     }
  74. };
  75. void checkNumTypes(Shape** niza, int n)
  76. {
  77.     int brojac=0 , brojac1=0, brojac2=0;
  78.     for(int i = 0 ; i < n ; i ++ )
  79.     {
  80.     Square *s= dynamic_cast<Square*>(niza[i]);
  81.         if(s!=0){
  82.             brojac++;
  83.     }
  84.     Circle*c=dynamic_cast<Circle*>(niza[i]);
  85.         if(c!=0)
  86.         {
  87.         brojac1++;
  88.         }
  89.    Triangle*t=dynamic_cast<Triangle*>(niza[i]);
  90.         if(t!=0)
  91.         {
  92.         brojac2++;
  93.         }
  94. }
  95.     cout<<"Broj na kvadrati vo nizata = "<<brojac<<endl;
  96.     cout<<"Broj na krugovi vo nizata = "<<brojac1<<endl;
  97.     cout<<"Broj na triagolnici vo nizata = "<<brojac2<<endl;
  98. }
  99.  
  100.  
  101. int main(){
  102.  
  103.  
  104.     int n;
  105.     cin >> n;
  106.         Shape**niza=new Shape*[n];
  107.  
  108.         int classType;
  109.         int side;
  110.  
  111.     for(int i = 0; i < n; ++i){
  112.  
  113.         cin >> classType;
  114.         cin >> side;
  115.         if(classType==1)
  116.         {
  117.         niza[i]=new Square(side);
  118.         }
  119.         else if(classType==2)
  120.         {
  121.         niza[i]=new Circle(side);
  122.         }
  123.         else if(classType==3)
  124.         {
  125.         niza[i]=new Triangle(side);
  126.         }
  127.     }
  128.    
  129.    
  130.     for(int i = 0; i < n; ++i){
  131.  
  132.         niza[i]->pecati();
  133.     }
  134.  
  135.     checkNumTypes(niza, n);
  136.  
  137.  
  138.     return 0;
  139. }
Add Comment
Please, Sign In to add comment