Advertisement
Guest User

Shapes

a guest
May 22nd, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <math.h>
  4. using namespace std;
  5. class Shape{
  6. protected:
  7.     int strana;
  8. public:
  9.     Shape(){
  10.     strana=0;
  11.     }
  12.     Shape(int strana1){
  13.      strana=strana1;
  14.     }
  15.     virtual double plostina()=0;
  16.     virtual void pecati()=0;
  17.     virtual int getType()=0;
  18. };
  19.  
  20. class Square:public Shape{
  21.  public:
  22.      Square(){
  23.      }
  24.      Square(int strana1):Shape(strana1){
  25.      }
  26.    double plostina(){
  27.     return strana*strana;
  28.    }
  29.  
  30.    void pecati(){
  31.     cout<<"Square so strana "<<strana<<" i polostina: "<<plostina()<<endl;
  32.    }
  33.    int getType(){
  34.    return 1;
  35.    }
  36. };
  37.  
  38. class Circle:public Shape{
  39.  public:
  40.      Circle(){
  41.      }
  42.      Circle(int strana1):Shape(strana1){
  43.  
  44.      }
  45.  double plostina(){
  46.     return 3.14*strana*strana;
  47.    }
  48.  
  49.  
  50.    void pecati(){
  51.     cout<<"Circle so strana "<<strana<<" i polostina: "<<plostina()<<endl;
  52.    }
  53.  
  54.     int getType(){
  55.    return 2;
  56.    }
  57. };
  58.  
  59. class Triangle:public Shape{
  60.  public:
  61.      Triangle(){
  62.      }
  63.      Triangle(int strana1):Shape(strana1){
  64.  
  65.      }
  66.  double plostina(){
  67.     return (sqrt(3)/4)*strana*strana;
  68.    }
  69.  
  70.     void pecati(){
  71.     cout<<"Triangle so strana "<<strana<<" i polostina: "<<plostina()<<endl;
  72.    }
  73.  
  74.     int getType(){
  75.    return 3;
  76.    }
  77. };
  78. void checkNumTypes(Shape **niza,int n){
  79. int brojacSquare=0;
  80. int brojacCircle=0;
  81. int brojacTriangle=0;
  82. for(int i=0;i<n;i++){
  83.     if(dynamic_cast<Square*>(niza[i])!=0){
  84.         brojacSquare++;
  85.     }else if(dynamic_cast<Circle*>(niza[i])!=0){
  86.       brojacCircle++;
  87.     }else if(dynamic_cast<Triangle*>(niza[i])!=0){
  88.      brojacTriangle++;
  89.     }
  90. }
  91.  
  92. cout<<"Kvadrati: "<<brojacSquare<<endl;
  93. cout<<"Krugovi: "<<brojacCircle<<endl;
  94. cout<<"Triagolnici: "<<brojacTriangle<<endl;
  95. }
  96.  
  97. int main(){
  98.  
  99.  
  100.     int n;
  101.     cin >> n;
  102.  
  103.     //TODO: inicijaliziraj niza od pokazuvachi kon klasata Shape
  104.     Shape **niza;
  105.  
  106.  
  107.     //TODO: alociraj memorija so golemina n za prethodno navedenata niza
  108.     niza=new Shape*[50];
  109.  
  110.     int classType;
  111.     int side;
  112.  
  113.     //TODO: konstruiraj for ciklus so chija pomosh ke ja popolnish nizata
  114.     for(int i = 0; i < n; ++i){
  115.  
  116.         cin >> classType;
  117.         cin >> side;
  118.         if(classType==1)
  119.         niza[i]=new Square(side);
  120.         if(classType==2)
  121.            niza[i]=new Circle(side);
  122.         if(classType==3)
  123.             niza[i]=new Triangle(side);
  124.     }
  125.  
  126. for(int i = 0; i < n; ++i){
  127.  
  128.         niza[i]->pecati();
  129.     }
  130.  
  131.     checkNumTypes(niza, n);
  132.  
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement