Advertisement
fabgonber

Untitled

Oct 7th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Fecha {
  5. public:
  6.     Fecha() {
  7.         anho = 0; mes =  0; dia =  0;    }
  8.  
  9.     Fecha (int a, int m, int d){
  10.         anho = a;  mes =  m; dia =  d;
  11.     }
  12.  
  13.     void setFecha(int a, int m, int d) {
  14.         anho = a;   mes  = m; dia  = d;
  15.     }
  16.  
  17.     int getAnyo()   { return anho;    }
  18.  
  19.     int getMes()    {return mes;    }
  20.  
  21.     int getDia()    { return dia;    }
  22.    
  23.     void ver()
  24.     {   // esto no se debe hacer, pero funciona:
  25.         cout << dia << "/" << mes << "/" << anho << endl;
  26.     }
  27.  
  28. private:
  29.     int anho;
  30.     int mes;
  31.     int dia;
  32. };
  33.  
  34.  
  35. class Persona {
  36. public:
  37.  
  38.     Persona();
  39.     Persona(string nombre, int edad);
  40.     Persona(const Persona& otro);
  41.     ~Persona();
  42.    
  43.     void setNombre(string nombre);
  44.     string getNombre();
  45.  
  46.     void setEdad(int una_edad);
  47.     int getEdad();
  48.  
  49.     void setNacimiento(Fecha cuando);
  50.     Fecha getNacimiento();
  51.    
  52.     void ver();
  53.    
  54.     // alerta !!!! nacimiento es atributo public
  55.     // Fecha nacimiento; // no se hace !!!!
  56.  
  57. protected:
  58. private:
  59.     //atributos
  60.     string nombre;
  61.     int edad;
  62.     Fecha nacimiento;
  63. };
  64.  
  65. int main()
  66. {
  67.     cout << "primero las personas" << endl;
  68.     cout << "====================" << endl;
  69.     Persona uno;
  70.     uno.setNombre("Cristobal");
  71.     uno.setEdad(24);
  72.  
  73.     Persona dos("Pedro", 19); //instanciar  
  74.     Persona cuatro(dos); //constructor copia
  75.  
  76.     uno.ver();
  77.     dos.ver();
  78.     cuatro.ver();
  79.  
  80.     cout << endl << endl;
  81.     cout << "ahora una fecha" << endl;
  82.     cout << "====================" << endl;
  83.     Fecha enero152020(2020,1,15);
  84.     enero152020.ver();
  85.  
  86.     cout << endl << endl;
  87.     cout << "ahora una persona con fecha" << endl;
  88.     cout << "====================" << endl;    
  89.     uno.setNacimiento(enero152020);
  90.     uno.getNacimiento().ver();
  91.  
  92.     cout << endl << endl;
  93.     cout << "ahora una persona con fecha 2" << endl;
  94.     cout << "====================" << endl;    
  95.     Fecha mayo152020(2020,5,15);
  96.     Fecha temporal;
  97.     dos.setNacimiento(mayo152020);
  98.     temporal = dos.getNacimiento();
  99.     temporal.ver();
  100.  
  101.     return 0;
  102.  
  103. }
  104.  
  105. Persona::Persona() {
  106.         this->nombre = "";
  107.         this->edad = 0;
  108.     }
  109.  
  110. void Persona::setNacimiento(Fecha cuando) {
  111.         this->nacimiento.setFecha(
  112.             cuando.getAnyo(),
  113.             cuando.getMes(),
  114.             cuando.getDia()
  115.         );
  116. }
  117.  
  118. Persona::Persona(string nombre, int edad)
  119. {
  120.         this->nombre = nombre;
  121.         this->edad = edad;
  122. }
  123.     //constructor copia
  124. Persona::Persona(const Persona& otro){
  125.         this->nombre = otro.nombre;
  126.         this->edad = otro.edad;
  127.     }
  128.     //setters
  129.     void Persona::setNombre(string nombre){
  130.         this->nombre = nombre;
  131.     }
  132.  
  133.     void Persona::setEdad(int una_edad){
  134.         edad = una_edad;
  135.     }
  136.  
  137.     //getters
  138.     string Persona::getNombre(){
  139.         return nombre;
  140.     }
  141.  
  142.     int Persona::getEdad(){
  143.         return this->edad;
  144.     }
  145.  
  146.     Fecha Persona::getNacimiento(){
  147.         return this->nacimiento;
  148.     }
  149.  
  150.     //destructor
  151.     Persona::~Persona() {
  152.     }
  153.  
  154.     //métodos de la clase (internos)
  155.     void Persona::ver(){
  156.         cout <<"nombre:"<<this->nombre<<", edad:"<<getEdad() <<endl ;
  157.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement