Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Figura
- {
- public:
- Figura()
- { this->clase = 0; }
- ~Figura()
- { }
- void virtual setLado(int a)
- { }
- void virtual setLado(int a, int b)
- { }
- int getClase()
- { return this->clase; }
- static const int CUADRADO = 1;
- static const int RECTANGULO = 2;
- protected:
- int clase;
- };
- class Rectangulo : public Figura
- {
- public:
- Rectangulo()
- {
- this->clase = this->RECTANGULO;
- this->largo = 0;
- this->ancho = 0;
- }
- ~Rectangulo()
- { cout << largo << ", " << ancho << endl; }
- void setLado(int a, int b)
- { this->largo = a;
- this->ancho = b;
- }
- private:
- int largo;
- int ancho;
- };
- class Cuadrado : public Figura
- {
- public:
- Cuadrado()
- { this->clase = this->CUADRADO;
- this->lado = 0; }
- void setLado(int a)
- { this->lado = a; }
- ~Cuadrado()
- { cout << lado << endl; }
- private:
- int lado;
- };
- int main() {
- std::cout << "Hello World!\n";
- Cuadrado azul;
- Rectangulo verde;
- /* Como hacer un array de diferentes clases? no se puede... puesto que todos los elementos deben tener el mismo tamaño (en bytes)
- idea: hacer un array de direcciones de memoria. debido a que todas las direcciones de memoria tienen el mismo tamaño (sizeof(dword))
- */
- Figura * arreglo[2];
- arreglo[0] = &azul;
- arreglo[1] = &verde;
- if ( (arreglo[0]->getClase()) == arreglo[0]->CUADRADO )
- {
- arreglo[0]->setLado(3);
- }
- if ( (arreglo[0]->getClase()) == arreglo[0]->RECTANGULO )
- {
- arreglo[0]->setLado(4,5);
- }
- if ( (arreglo[1]->getClase()) == arreglo[1]->CUADRADO )
- {
- arreglo[1]->setLado(3);
- }
- if ( (arreglo[1]->getClase()) == arreglo[1]->RECTANGULO )
- {
- arreglo[1]->setLado(4,5);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement