Advertisement
juan_de99

Untitled

Oct 26th, 2021
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. ///main
  2.  
  3. #include <iostream>
  4. #include "Animal.h"
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     Animal p1;
  11.     Animal p2("Lola",5);
  12.     cout<<p1<<endl;
  13.     cout<<p2<<endl;
  14.  
  15.     Animal p3(p1);
  16.     p3=p2++;
  17.     if(p1.verEdad() == p2.verEdad())
  18.         cout<< "Ambos animales tienen la misma edad"<<endl;
  19.     else cout<<"Los animales tienen edad diferente"<<endl;
  20.     cout<<p1<<p2<<endl;
  21.     return 0;
  22. }
  23. ///animal.h
  24.  
  25. #include <iostream>
  26. #include <string.h>
  27.  
  28. using namespace std;
  29.  
  30. class Animal
  31. {
  32.     private:
  33.         int edad;
  34.         char *nombre;
  35.  
  36.  
  37.     public:
  38.         Animal();
  39.         Animal(const char *nombre,int edad);
  40.         Animal(const Animal& anim);
  41.         Animal& operator ++(int);
  42.         Animal& operator=(const Animal& anim);
  43.         ~Animal();
  44.         int verEdad() const;
  45.         friend ostream& operator <<(ostream &sal,const Animal &anim);
  46. };
  47.  
  48. ///animal.cpp
  49.  
  50. #include "Animal.h"
  51.  
  52. Animal::Animal(const char *nombre,int edad)
  53. {
  54.  
  55.  
  56.  
  57.     this->nombre=new char[strlen(nombre)];
  58.     strcpy(this->nombre,nombre);
  59.  
  60.  
  61.     this->edad=edad;
  62. }
  63. Animal::Animal()
  64. {
  65.     this->nombre=new char[1];
  66.     *this->nombre='\0';
  67.     this->edad=0;
  68. }
  69. Animal::Animal(const Animal &anim)
  70. {
  71.     if(anim.nombre)
  72.     {
  73.         this->nombre=new char[strlen(anim.nombre)];
  74.         strcpy(this->nombre,anim.nombre);
  75.     }
  76.     else
  77.         this->nombre=NULL;
  78.     this->edad=anim.edad;
  79. }
  80.  
  81.  Animal::~Animal()
  82.  {
  83.      delete this->nombre;
  84.  }
  85. Animal& Animal::operator ++(int)
  86. {
  87.     this->edad++;
  88.  
  89.     return *this;
  90. }
  91. Animal& Animal::operator =(const Animal& anim)
  92. {
  93.     if(strlen(this->nombre)!= strlen(anim.nombre))
  94.     {
  95.         delete [] this->nombre;
  96.         this->nombre=new char[strlen(anim.nombre)];
  97.     }
  98.  
  99.     strcpy(this->nombre,anim.nombre);
  100.     this->edad=anim.edad;
  101.  
  102.     return *this;
  103. }
  104. int Animal::verEdad() const
  105. {
  106.     return this->edad;
  107. }
  108.  
  109. ostream& operator <<(ostream &sal,const Animal &anim)
  110. {
  111.     //return sal<<"Nombre: " << *anim.nombre?sal<<"Sin Nombre":sal<<anim.nombre<<"Edad: "<<anim.edad;
  112.     sal<<"Nombre: ";
  113.     if(*anim.nombre == '\0')
  114.     {
  115.         sal<<"Sin nombre";
  116.     }
  117.     else
  118.         sal<<anim.nombre;
  119.     sal<<" edad: "<<anim.edad;
  120.     return sal;
  121. }
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement