Advertisement
fabgonber

Untitled

Oct 5th, 2020 (edited)
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 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.     Rectangulo(const Rectangulo & otro);
  16.     ~Rectangulo(); // el destructor
  17.     // sobrecarga de metodo:
  18.     void setLados(int lado1, int lado2);
  19.     void setLados(int lado);
  20.  
  21.     // setters y getters || modificadores y consultores
  22.     int getLargo();
  23.     void setLargo(int a);
  24.     int getAncho();
  25.     void setAncho(int a);
  26.     // fin de setters y getters || modificadores y consultores
  27.  
  28.     // EJERCICIO EN CLASES construya y pruebe:
  29.     int area(); // devuelve el area
  30.     int perimetro(); // devuelve el perimetro
  31.     bool esCuadrado(); // si es cuadrado devuelve true
  32.  
  33. // private solo los objetos de la clase tiene acceso
  34. private:
  35. // atributos
  36.   int largo;
  37.   int ancho;
  38.  
  39. };
  40. // fin de Declaracion de Clase
  41.  
  42. int main() {
  43.  
  44.   int valor;
  45.  
  46.   cout << "Mi primera Clase: Rectangulo !\n";
  47.   cout << "creacion de rectangulos" << endl;
  48.  
  49.   Rectangulo azul;
  50.   Rectangulo verde;
  51.   Rectangulo amarillo(7,12);
  52.   Rectangulo rosado(amarillo);
  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. Rectangulo::Rectangulo(const Rectangulo & otro)
  99. {
  100.   // copiar todos los atributos del objeto:
  101.   this->largo = otro.largo;
  102.   this->ancho = otro.ancho;
  103. }
  104.  
  105. Rectangulo::~Rectangulo()
  106. {
  107.   cout << "soy el destructor ";
  108.   cout << this->largo << " " << this->ancho;
  109.   cout << endl;
  110.  
  111. }
  112.  
  113. void Rectangulo::setLados(int lado)
  114. {
  115.     this->setLados(lado,lado);
  116. }
  117.  
  118. void Rectangulo::setLados(int lado1, int lado2)
  119. {
  120.     if (lado1>lado2)
  121.     {
  122.         this->largo = lado1;
  123.         this->ancho = lado2;
  124.     } else
  125.     {
  126.         this->largo = lado2;
  127.         this->ancho = lado1;
  128.     }
  129. }
  130.  
  131. int Rectangulo::getAncho()
  132. {
  133.     return this->ancho;
  134. }
  135.  
  136. void Rectangulo::setAncho(int a)
  137. {
  138.     if (a<=this->largo)
  139.     {  this->ancho = a; }
  140.     else
  141.     {
  142.         this->ancho = this->largo;
  143.         this->largo = a;
  144.     }
  145. }
  146.  
  147. int Rectangulo::getLargo()
  148. {
  149.    
  150.     return this->largo;
  151. }
  152.  
  153. void Rectangulo::setLargo(int a)
  154. {
  155.     if (a>=this->ancho)
  156.     {
  157.       this->largo = a;
  158.     }
  159.     else
  160.     {
  161.       this->largo = this->ancho;
  162.       this->ancho = a;
  163.     }
  164. }
  165.  
  166. bool Rectangulo::esCuadrado()
  167. {
  168.   if ((this->largo == this->ancho)&&(largo>0))
  169.   {
  170.     return true;
  171.   }
  172.   else
  173.   {
  174.     return false;
  175.   }
  176. }
  177.  
  178. int Rectangulo::perimetro()
  179. {
  180.     return 2*(this->largo + this->ancho);
  181. }
  182.  
  183. int Rectangulo::area()
  184. {
  185.     return this->largo * this->ancho;
  186. }
  187.  
  188.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement