Ejemplo de Polimorfismo-Herencia en C++ con POO

C++:
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <vector>
  4.  
  5. class Padre
  6. {
  7.     protected:
  8.       char* _nombre;
  9.       int _edad;
  10.  
  11.     public:
  12.       Padre():_edad(-1)
  13.       {
  14.         _nombre = new char[20];
  15.         strcpy(_nombre, "Default");
  16.       }
  17.       Padre(char* n, int a = 1):_edad(a)
  18.       {
  19.         _nombre = new char[20];
  20.         strcpy(_nombre, n);
  21.       }
  22.  
  23.       inline int edad()const { return _edad; }
  24.       char* nombre() const
  25.       {
  26.         char* temp = new char[20];
  27.         strcpy(temp, _nombre);
  28.         return temp;
  29.       }                                                                             
  30.  
  31.       virtual char* saluda()
  32.       {
  33.          char* t = new char[30];
  34.          strcpy(t,"Esta es una clase Padre");
  35.          return t;
  36.       }
  37.  
  38.       friend ostream& operator <<(ostream& out, const Padre& rhs)
  39.       {
  40.          out <<"nombre: " <<rhs.nombre() <<" edad: " <<rhs.edad();
  41.          return out;
  42.       }
  43. };
  44.  
  45. class Hijo : public Padre
  46. {
  47.       protected:
  48.             int _matricula;
  49.             char* _escuela;
  50.      public:
  51.  
  52.             Hijo(char* nombrePadre, int edadPadre, char* nEscuela, int nMatricula): Padre(nombrePadre, edadPadre)
  53.             {
  54.                _matricula = nMatricula;
  55.                _escuela = new char[20];
  56.                strcpy(_escuela, nEscuela);
  57.             }
  58.  
  59.             inline int matricula(){ return _matricula; }
  60.  
  61.            char* saluda()
  62.            {
  63.                  char* t = "Esta es una clase Hijo";
  64.                  return t;
  65.            }
  66.  
  67.  
  68.             char* escuela()
  69.             {
  70.                char* temp = new char[20];
  71.                strcpy(temp, _escuela);
  72.                return temp;
  73.             }
  74.  
  75.             friend ostream& operator <<(ostream& out, const Hijo &rhs)
  76.             {
  77.                 out <<static_cast<Padre> (rhs) <<" escuela: " <<rhs._escuela <<" matricula : " <<rhs._matricula;
  78.                 return out;
  79.             }
  80. };
  81.  
  82. int main(int args, char** argv)
  83. {
  84.        bool cond = false;
  85.        if(args == 2)
  86.              cond = !strcmp(argv[1], "-debug");
  87.  
  88.       if(cond) cout <<"Creando objeto Padre " <<endl;
  89.       Padre p1("Alejandro", 24);
  90.  
  91.       if(cond) cout <<"Creando objeto Hijo " <<endl;
  92.       Hijo h1("Junior", 15, "Tec", 922382);
  93.  
  94.  
  95.       // Un contenedor general para los dos tipos de objetos
  96.  
  97.       vector<Padre*>arreglo;
  98.       arreglo.push_back(new Padre("Alejandro", 24));
  99.       arreglo.push_back(new Hijo("Junior", 15, "Tec", 922382));
  100.  
  101.       cout <<arreglo[0]->saluda() <<endl;
  102.       cout <<arreglo[1]->saluda() <<endl;
  103.  
  104.  
  105.       //cout <<p1 <<endl;
  106.       //cout <<h1 <<endl;
  107.       system("PAUSE");
  108.       return 0;
  109. }

Popularidad: 15%