Advertisement
fabgonber

Untitled

Sep 30th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // inicio de Declaracion de Clase
  5. class Rectangulo
  6. {
  7.  
  8.   // metodos o funciones internas
  9.   // public: desde main u otros hay acceso
  10.   public:  
  11.     // constructores y sobrecarga de constructor:
  12.     Rectangulo();
  13.     Rectangulo(int x);
  14.     Rectangulo(int x, int y);
  15.     // sobrecarga de metodo:
  16.     void setLados(int lado1, int lado2);
  17.     void setLados(int lado);
  18.  
  19.     // setters y getters || modificadores y consultores
  20.     int getLargo();
  21.     void setLargo(int a);
  22.     int getAncho();
  23.     void setAncho(int a);
  24.     // fin de setters y getters || modificadores y consultores
  25.  
  26.     // EJERCICIO EN CLASES construya y pruebe:
  27.     int area(); // devuelve el area
  28.     int perimetro(); // devuelve el perimetro
  29.     bool esCuadrado(); // si es cuadrado devuelve true
  30.  
  31.     // EJERCICIO EN CLASES 2:
  32.     // modifique setLargo y setAncho para que no se pueda colocar un ancho mas grande que el largo ni un largo mas corto que el ancho
  33.  
  34. // private solo los objetos de la clase tiene acceso
  35. private:
  36. // atributos
  37.   int largo;
  38.   int ancho;
  39.  
  40. };
  41. // fin de Declaracion de Clase
  42.  
  43. int main() {
  44.  
  45.   int valor;
  46.  
  47.   cout << "Mi primera Clase: Rectangulo !\n";
  48.   cout << "creacion de rectangulos" << endl;
  49.  
  50.   Rectangulo azul;
  51.   Rectangulo verde;
  52.   Rectangulo amarillo(7,12);
  53.  
  54.   azul.setLados(3,8);
  55.   verde.setLados(12,4);
  56.  
  57.  cout << "amarillo tiene ancho " << amarillo.getAncho() << endl;
  58.  
  59.   cout << "azul tiene ancho " << azul.getAncho() << endl;
  60.   azul.setLados(9);
  61.   cout << "azul tiene ancho " << azul.getAncho() << endl;
  62.   cout << "azul tiene largo " << azul.getLargo() << endl;
  63.  
  64.   valor = verde.getLargo();
  65.   cout << "verde tiene largo " << valor << endl;
  66.  
  67.   valor = verde.area();
  68.   cout << "verde tiene area " << valor << endl;
  69.   verde.setLargo(15);
  70.   valor = verde.area();
  71.   cout << "verde tiene area " << valor << endl;
  72. }
  73.  
  74. Rectangulo::Rectangulo()
  75. {
  76.   // opcion 1:
  77.     this->setLados(0,0);
  78.  
  79.   /*
  80.   // opcion 2:
  81.   // no es recomendable
  82.     this->largo = 0;
  83.     this->ancho = 0;
  84.   */
  85.  
  86. }
  87.  
  88. Rectangulo::Rectangulo(int x, int y)
  89. {
  90.     this->setLados(x,y);
  91. }
  92.  
  93. Rectangulo::Rectangulo(int x)
  94. {
  95.     this->setLados(x,x);
  96. }
  97.  
  98.  
  99. void Rectangulo::setLados(int lado)
  100. {
  101.     this->setLados(lado,lado);
  102. }
  103.  
  104.  
  105. void Rectangulo::setLados(int lado1, int lado2)
  106. {
  107.     if (lado1>lado2)
  108.     {
  109.         this->largo = lado1;
  110.         this->ancho = lado2;
  111.     } else
  112.     {
  113.         this->largo = lado2;
  114.         this->ancho = lado1;
  115.     }
  116. }
  117.  
  118. int Rectangulo::getAncho()
  119. {
  120.     return this->ancho;
  121. }
  122.  
  123. void Rectangulo::setAncho(int a)
  124. {
  125.     if (a<=this->largo)
  126.     {  this->ancho = a; }
  127.     else
  128.     {
  129.         this->ancho = this->largo;
  130.         this->largo = a;
  131.     }
  132. }
  133.  
  134. int Rectangulo::getLargo()
  135. {
  136.    
  137.     return this->largo;
  138. }
  139.  
  140. void Rectangulo::setLargo(int a)
  141. {
  142.     if (a>=this->ancho)
  143.     {
  144.       this->largo = a;
  145.     }
  146.     else
  147.     {
  148.       this->largo = this->ancho;
  149.       this->ancho = a;
  150.     }
  151. }
  152.  
  153. bool Rectangulo::esCuadrado()
  154. {
  155.   if ((this->largo == this->ancho)&&(largo>0))
  156.   {
  157.     return true;
  158.   }
  159.   else
  160.   {
  161.     return false;
  162.   }
  163. }
  164.  
  165. int Rectangulo::perimetro()
  166. {
  167.     return 2*(this->largo + this->ancho);
  168. }
  169.  
  170. int Rectangulo::area()
  171. {
  172.     return this->largo * this->ancho;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement