Advertisement
Guest User

Untitled

a guest
Jun 28th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. En el Unit.cpp:
  2. #include <vcl.h>
  3.  
  4. #include "Unit1.h"
  5. #include "ClasePrueba.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma resource "*.dfm"
  9. TForm1 *Form1;
  10. TClase *C; //Puntero a la clase
  11. //---------------------------------------------------------------------------
  12. __fastcall TForm1::TForm1(TComponent* Owner)
  13.         : TForm(Owner)
  14. {
  15. }
  16. //---------------------------------------------------------------------------
  17.  
  18. //Creo la clase
  19. void __fastcall TForm1::FormCreate(TObject *Sender)
  20. {
  21.         C = new TClase();
  22. }
  23. //---------------------------------------------------------------------------
  24.  
  25. //Destruyo la clase
  26. void __fastcall TForm1::FormDestroy(TObject *Sender)
  27. {
  28.         delete C;
  29. }
  30. //---------------------------------------------------------------------------
  31.  
  32. void __fastcall TForm1::Button1Click(TObject *Sender)
  33. {
  34.         //Comprobamos que haya algo en el TEdit que debe contener el entero
  35.         if(Edit1->Text != "")
  36.         {
  37.                 Edit2->Text = C->Int->ToHex(Edit1->Text.ToInt());
  38.         }else{
  39.                 Edit2->Text = "";
  40.         }
  41. }
  42. //---------------------------------------------------------------------------
  43.  
  44. //Filtramos para que no se pueda introducir algo diferente a numeros
  45. void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
  46. {
  47.         if(Key < '0' || Key > '9')
  48.                 if(Key != '\b')
  49.                         Key = 0;
  50. }
  51. //---------------------------------------------------------------------------
  52.  
  53. En el ClasePrueba.cpp:
  54. #include "ClasePrueba.h"
  55.  
  56. //Constructor
  57. TClase::TClase()
  58. {
  59.         Int = new TInt;
  60.         Int->ToHex = IntToHex;
  61. }
  62.  
  63. //Destructor
  64. TClase::~TClase()
  65. {
  66.         delete Int;
  67.         Int = NULL;
  68. }
  69.  
  70. //Metodo de la clase
  71. AnsiString __fastcall TClase::IntToHex(int value)
  72. {
  73.         I = value;
  74.         return AnsiString().sprintf("0x%s",AnsiString().IntToHex(+I,8));
  75. }
  76.  
  77. En el ClasePrueba.h:
  78. #include <vcl.h>
  79.  
  80. #ifndef ClasePruebaH
  81. #define ClasePruebaH
  82. //---------------------------------------------------------------------------
  83. class TClase
  84. {
  85.         private:
  86.                 double I; //Variable privada
  87.                 AnsiString __fastcall IntToHex(int value); //Metodo privado
  88.  
  89.         public:
  90.                 TClase();
  91.                 ~TClase();
  92.                
  93.                 //Declaracion del tipo de funcion
  94.                 typedef AnsiString __fastcall(__closure* POINT_FUNC_RET_STRING)(int value);
  95.  
  96.                 //Declaracion de la estructura que contendrĂ¡ los punteros a funciones
  97.                 typedef struct
  98.                 {
  99.                         POINT_FUNC_RET_STRING ToHex;
  100.                 }TInt;
  101.  
  102.                 //Puntero a estructura que definimos
  103.                 TInt *Int;
  104. };
  105. //---------------------------------------------------------------------------
  106. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement