metalni

OOP Labs 7 Shapes

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