Ver Mensaje Individual
  #4 (permalink)  
Antiguo 26/01/2014, 09:58
Avatar de iFuSiiOnzZ
iFuSiiOnzZ
 
Fecha de Ingreso: junio-2012
Mensajes: 8
Antigüedad: 11 años, 10 meses
Puntos: 0
Respuesta: Creación de objetos y paso como parámetro

A ver si esto te ayuda, en todo caso, cuando hay que tratar con Objetos lo mejor es usar punteros.

Código C++:
Ver original
  1. #include <cstdio>
  2. #include <string>
  3. #include <cstdlib>
  4.  
  5. class A {
  6.     private:
  7.         std::string str;
  8.  
  9.     public:
  10.         A(void)
  11.         {
  12.         }
  13.  
  14.         A(std::string str)
  15.         {
  16.             this->str.assign(str);
  17.         }
  18.         ~A()
  19.         {
  20.             this->str.clear();
  21.         }
  22.  
  23.         void mostrarString(void)
  24.         {
  25.             std::printf("%s\n", this->str.c_str());
  26.         }
  27. };
  28.  
  29. class B {
  30.     private:
  31.         A objA;
  32.  
  33.     public:
  34.         B(void)
  35.         {
  36.         }
  37.  
  38.         ~B()
  39.         {
  40.         }
  41.  
  42.         void anadirA(A obj)
  43.         {
  44.             objA = obj;
  45.         }
  46.  
  47.         void mostrarStringA()
  48.         {
  49.             objA.mostrarString();
  50.         }
  51. };
  52.  
  53.  
  54. int main(int argc, char *argv[])
  55. {
  56.     A objA("Hola Mundo Desde A");
  57.     B objB;
  58.  
  59.     objB.anadirA(objA);
  60.     objB.mostrarStringA();
  61.  
  62.     getchar();
  63.     return(EXIT_SUCCESS);
  64. }