Hyluss

Przeciazanie operatorow

Jul 2nd, 2016
545
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. // przeciazanie operatorow, konstruktor parametryczny, kopiujacy
  2. // }{
  3.  
  4. #include "stdafx.h"
  5. #include <conio.h>
  6. #include <malloc.h>
  7.  
  8. class DynArray
  9. {
  10. int* tab;
  11. int N;
  12.  
  13.  
  14. public:
  15. DynArray () : N (0)
  16. {
  17. }
  18.  
  19. DynArray (int n, int m) : N (n)
  20. {
  21.     tab = (int*) malloc (N*sizeof (int));
  22.     for (int i=0; i<N; i++)
  23.     tab[i] = m;
  24. }
  25.  
  26. DynArray ( DynArray &dyn)
  27. {
  28.     N= dyn.N;
  29.     tab = (int*) malloc (N*sizeof (int));
  30.     for (int i =0; i<N; i++)
  31.     {
  32.         tab[i] = dyn.tab[i];
  33.     }
  34. }
  35.  
  36.  
  37. int Size ()
  38. {
  39.     return N;
  40. }
  41.  
  42.  
  43. void Display ()
  44. {
  45.     printf ("[");
  46.     for (int i=0; i<N;i++)
  47.     {
  48.         printf ("%d",tab[i]);
  49.         if (i!=N-1)
  50.         {
  51.             printf (", ");
  52.         }
  53.     }
  54.     printf ("]");
  55. }
  56.  
  57.  
  58. void operator += (int liczba)
  59. {
  60. int i = N; // to ten uj!
  61.     N++;
  62.    
  63. //tab = (int*) realloc (tab, N*sizeof (*tab));
  64. tab = (int*) realloc ( tab, ( (N)*(sizeof (int)) ) );
  65. //tab = (int*) realloc (tab, sizeof (int)*(N) );
  66. //  realloc (tab, sizeof (int)* (N) ); 
  67.  // tab = (int*)realloc(tab, N*sizeof(int));
  68.  tab[i] = liczba;
  69. //  Display ();
  70. }
  71.  
  72. void operator ! ()
  73. {
  74.     for (int i=0;i<N;i++)
  75.     {
  76.         tab[i] *= -1;
  77.     }
  78. }
  79.  
  80.  
  81. void Koniec()
  82. {
  83.     /*for (int i=0;i<N;i++)
  84.     {
  85.     free (tab);
  86.     }
  87. */
  88. }
  89.  
  90. };
  91.  
  92.  
  93. int _tmain(int argc, _TCHAR* argv[])
  94. {
  95.  
  96.     DynArray arr1;
  97.     DynArray arr2(5, 2);
  98.    
  99.     printf("arr1 {%2d}  = ", arr1.Size());
  100.     arr1.Display();
  101.  
  102.     printf("\narr2 {%2d}  = ", arr2.Size());
  103.     arr2.Display();
  104.  
  105.     DynArray arr3 = arr2;
  106.    
  107.     arr3 += -10;
  108.     arr3 += -20;
  109.     arr3 += -30;
  110.     printf("\narr3 {%2d}  = ", arr3.Size());
  111.     arr3.Display();
  112.  
  113.     !arr3;
  114.     printf("\n!arr3 {%2d} = ", arr3.Size());
  115.     arr3.Display();
  116.  
  117.     printf("\narr2 {%2d}  = ", arr2.Size());
  118.     arr2.Display();
  119.  
  120.     arr1.Koniec(); // wydaje mis ei ze tablica jest juz automatycznie wyczyszczona, ale nie wiem jak i czemu xD
  121.     arr2.Koniec();
  122.     arr3.Koniec();
  123.     printf("\n\nKoniec.");
  124.  
  125.     _getch();
  126.     return 0;
  127. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment