Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///main
- #include <iostream>
- #include "Animal.h"
- using namespace std;
- int main()
- {
- Animal p1;
- Animal p2("Lola",5);
- cout<<p1<<endl;
- cout<<p2<<endl;
- Animal p3(p1);
- p3=p2++;
- if(p1.verEdad() == p2.verEdad())
- cout<< "Ambos animales tienen la misma edad"<<endl;
- else cout<<"Los animales tienen edad diferente"<<endl;
- cout<<p1<<p2<<endl;
- return 0;
- }
- ///animal.h
- #include <iostream>
- #include <string.h>
- using namespace std;
- class Animal
- {
- private:
- int edad;
- char *nombre;
- public:
- Animal();
- Animal(const char *nombre,int edad);
- Animal(const Animal& anim);
- Animal& operator ++(int);
- Animal& operator=(const Animal& anim);
- ~Animal();
- int verEdad() const;
- friend ostream& operator <<(ostream &sal,const Animal &anim);
- };
- ///animal.cpp
- #include "Animal.h"
- Animal::Animal(const char *nombre,int edad)
- {
- this->nombre=new char[strlen(nombre)];
- strcpy(this->nombre,nombre);
- this->edad=edad;
- }
- Animal::Animal()
- {
- this->nombre=new char[1];
- *this->nombre='\0';
- this->edad=0;
- }
- Animal::Animal(const Animal &anim)
- {
- if(anim.nombre)
- {
- this->nombre=new char[strlen(anim.nombre)];
- strcpy(this->nombre,anim.nombre);
- }
- else
- this->nombre=NULL;
- this->edad=anim.edad;
- }
- Animal::~Animal()
- {
- delete this->nombre;
- }
- Animal& Animal::operator ++(int)
- {
- this->edad++;
- return *this;
- }
- Animal& Animal::operator =(const Animal& anim)
- {
- if(strlen(this->nombre)!= strlen(anim.nombre))
- {
- delete [] this->nombre;
- this->nombre=new char[strlen(anim.nombre)];
- }
- strcpy(this->nombre,anim.nombre);
- this->edad=anim.edad;
- return *this;
- }
- int Animal::verEdad() const
- {
- return this->edad;
- }
- ostream& operator <<(ostream &sal,const Animal &anim)
- {
- //return sal<<"Nombre: " << *anim.nombre?sal<<"Sin Nombre":sal<<anim.nombre<<"Edad: "<<anim.edad;
- sal<<"Nombre: ";
- if(*anim.nombre == '\0')
- {
- sal<<"Sin nombre";
- }
- else
- sal<<anim.nombre;
- sal<<" edad: "<<anim.edad;
- return sal;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement