CB100

Notas, resueltos y tps de la materia Algoritmos y Estructuras de Datos
Index Commits Files Refs README
guias/4/ej16.cpp (1899B)
   1 #include <iostream>
   2 
   3 class Rectangle {
   4 private:
   5     float base;
   6     float height;
   7 
   8 public:
   9     /**
  10      * post: inicializa el rectangulo con sus atributos a `0.0f`
  11      */
  12     Rectangle() {
  13         this->base = 0.0f;
  14         this->height = 0.0f;
  15     }
  16 
  17     /**
  18      * post: inicializa el rectangulo con `base` y `altura`
  19      */
  20     Rectangle(float base, float height) {
  21         this->base = base;
  22         this->height = height;
  23     }
  24 
  25     /**
  26      * post: libera la memoria
  27      */
  28     virtual ~Rectangle() {}
  29 
  30     /**
  31      * pre: el rectangulo debe estar inicializado
  32      * post: cambia la base del rectangulo por `base`
  33      */
  34     void setBase(float base) {
  35         this->base = base;
  36     }
  37 
  38     /**
  39      * pre: el rectangulo debe estar inicializado
  40      * post: cambia la altura del rectangulo por `height`
  41      */
  42     void setHeight(float height) {
  43         this->height = height;
  44     }
  45 
  46     /**
  47      * pre: el rectangulo debe estar inicializado
  48      * post: devuelve la base del rectangulo
  49      */
  50     float getBase() {
  51         return this->base;
  52     }
  53 
  54     /**
  55      * pre: el rectangulo debe estar inicializado
  56      * post: devuelve la altura del rectangulo
  57      */
  58     float getHeight() {
  59         return this->height;
  60     }
  61 
  62     /**
  63      * pre: el rectangulo debe estar inicializado
  64      * post: devuelve el perimetro del rectangulo
  65      */
  66     float getPerimeter() {
  67         return 2*(this->base + this->height);
  68     }
  69 
  70     /**
  71      * pre: el rectangulo debe estar inicializado
  72      * post: devuelve el area del rectangulo
  73      */
  74     float getArea() {
  75         return this->base * this->height;
  76     }
  77 
  78     /**
  79      * pre:  el rectangulo debe estar inicializada
  80      * post: imprime la base y altura del rectangulo en formato (b,h)
  81      */
  82     void print() {
  83         std::cout << "(" << this->base << ", " << this->height << ")\n";
  84     }
  85 };
  86 
  87 int main (void) {
  88     Rectangle r(2,2);
  89     r.print();
  90     std::cout << "Perimetro = " << r.getPerimeter() << std::endl;
  91     std::cout << "Area = " << r.getArea() << std::endl;
  92     std::cout << "Base = " << r.getBase() << std::endl;
  93     std::cout << "Altura = " << r.getHeight() << std::endl;
  94     return 0;
  95 }