Advertisement
Sooldierr

wektory - konstruktory i przeciazenia

Mar 30th, 2015
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <malloc.h>
  5. #include <memory.h>
  6.  
  7. class Wektor
  8. {
  9. private:
  10.     double *tab;
  11.     int N;
  12. public:
  13.     Wektor();                           //konstruktor domyślny
  14.     ~Wektor();
  15.     Wektor::Wektor(Wektor &kopia);  //konstruktor kopiujący
  16.     Wektor(const int N, const double M);    //konstruktor parametryczny
  17.     const int Size() const;             //metoda zwracająca liczbę elementów tablicy
  18.     void Display();                     //wyświetla tablicę [el1, el2, ..., elN]
  19.     void Koniec();
  20.     Wektor& operator ~ ()               //operator dokonujący odwracania kolejności
  21.     {
  22.         Wektor sort(N, 0);
  23.         //sort.tab = (double*)malloc(N * sizeof(double));
  24.         int size = N-1;
  25.         for(int i = 0; i < N; i++)
  26.         {
  27.             sort.tab[i] = tab[size];
  28.             size--;
  29.         }
  30.         return sort;
  31.     }
  32.     Wektor& operator += (double x)          //operator pozwalający na dodawanie do tablicy kolejnych elementów
  33.     {
  34.         N++;
  35.         double *buff;
  36.         if (double *buff = (double*)realloc(tab, N * sizeof(double)) != NULL)
  37.             tab = buff;
  38.         tab[N-1] = x;
  39.  
  40.         return *this;
  41.     }
  42. };
  43. Wektor::Wektor()                        //konstruktor domyślny
  44. {
  45.     tab = NULL;
  46.     N = 0;
  47. }
  48. Wektor::Wektor(Wektor &kopia)
  49. {
  50.     this->N = kopia.N;
  51.     this->tab = (double*)malloc(N * sizeof(double));
  52.     memcpy(this->tab, kopia.tab, N * sizeof(double));
  53. }
  54. Wektor::Wektor(const int N, const double M)
  55. {
  56.     this->N = N;
  57.  
  58.     tab  = (double*)malloc(N * sizeof(double));
  59.    
  60.         for( int i = 0; i < N; i++ )
  61.             tab[i] = M;
  62. }
  63. const int Wektor::Size() const
  64. {
  65.     return N;
  66. }
  67. void Wektor::Display()
  68. {
  69.     printf("[");
  70.     for(int i = 0; i < N; i++)
  71.     {
  72.         printf("%.2f", tab[i]);
  73.         if(i!=N-1) printf(", ");
  74.     }
  75.     printf("]");
  76. }
  77. Wektor::~Wektor()
  78. {
  79.     free(tab);
  80. }
  81.  
  82. int main()
  83. {
  84.     Wektor arr1;
  85.     Wektor arr2(5, 2.00);
  86.    
  87.     printf("arr1 {%2d}  = ", arr1.Size());
  88.     arr1.Display();
  89.  
  90.     printf("\narr2 {%2d}  = ", arr2.Size());
  91.     arr2.Display();
  92.  
  93.     Wektor arr3 = arr2;
  94.     arr3 += 11.1;
  95.     arr3 += 22.2;
  96.     arr3 += 33.3;
  97.     printf("\narr3 {%2d}  = ", arr3.Size());
  98.     arr3.Display();
  99.  
  100.     arr3;
  101.     printf("\n~arr3 {%2d} = ", arr3.Size());
  102.     (~arr3).Display();
  103.  
  104.     printf("\narr2 {%2d}  = ", arr2.Size());
  105.     arr2.Display();
  106.  
  107.     arr1.~Wektor();
  108.     arr2.~Wektor();
  109.     arr3.~Wektor();
  110.     printf("\n\nKoniec.");
  111.  
  112.     _getch();
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement