Advertisement
fabgonber

Untitled

Nov 1st, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Figura
  5. {
  6.   public:
  7.     Figura()
  8.     { this->clase = 0; }
  9.  
  10.     ~Figura()
  11.     {  }
  12.  
  13.     void virtual setLado(int a)
  14.     { }
  15.     void virtual setLado(int a, int b)
  16.     { }
  17.     int getClase()
  18.     { return this->clase; }
  19.  
  20.     static const int CUADRADO = 1;
  21.     static const int RECTANGULO = 2;
  22.  
  23.   protected:
  24.     int clase;
  25. };
  26.  
  27. class Rectangulo : public Figura
  28. {
  29.   public:
  30.     Rectangulo()
  31.     {
  32.       this->clase = this->RECTANGULO;
  33.       this->largo = 0;
  34.       this->ancho = 0;
  35.     }
  36.     ~Rectangulo()
  37.     { cout << largo << ", " << ancho << endl; }
  38.     void setLado(int a, int b)
  39.     { this->largo = a;
  40.       this->ancho = b;
  41.     }
  42.   private:
  43.     int largo;
  44.     int ancho;
  45. };
  46.  
  47. class Cuadrado : public Figura
  48. {
  49.   public:
  50.     Cuadrado()
  51.     { this->clase = this->CUADRADO;
  52.       this->lado = 0; }
  53.     void setLado(int a)
  54.     { this->lado = a; }
  55.     ~Cuadrado()
  56.     { cout << lado <<  endl; }
  57.   private:
  58.     int lado;
  59. };
  60.  
  61. int main() {
  62.   std::cout << "Hello World!\n";
  63.   Cuadrado azul;
  64.   Rectangulo verde;
  65.  
  66.   /* Como hacer un array de diferentes clases? no se puede... puesto que todos los elementos deben tener el mismo tamaño (en bytes)
  67.  
  68.   idea: hacer un array de direcciones de memoria. debido a que todas las direcciones de memoria tienen el mismo tamaño (sizeof(dword))
  69.   */
  70.  
  71.   Figura * arreglo[2];
  72.   arreglo[0] = &azul;
  73.   arreglo[1] = &verde;
  74.  
  75.  
  76.   if ( (arreglo[0]->getClase()) == arreglo[0]->CUADRADO )
  77.   {
  78.       arreglo[0]->setLado(3);
  79.   }
  80.   if ( (arreglo[0]->getClase()) == arreglo[0]->RECTANGULO )
  81.   {
  82.       arreglo[0]->setLado(4,5);
  83.   }
  84.  
  85.   if ( (arreglo[1]->getClase()) == arreglo[1]->CUADRADO )
  86.   {
  87.       arreglo[1]->setLado(3);
  88.   }
  89.   if ( (arreglo[1]->getClase()) == arreglo[1]->RECTANGULO )
  90.   {
  91.       arreglo[1]->setLado(4,5);
  92.   }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement