Coriic

1.2015A

Jun 16th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. template<class T>
  4. class Array{
  5. private:
  6. T* wsk;
  7. int dlugosc;
  8. public:
  9. Array();
  10. ~Array<T>();
  11. void dodaj(T& t);
  12. bool usun(int i);
  13. void wypisz();
  14. Array<T>& operator=(Array<T>&);
  15. };
  16.  
  17. template<class T>
  18. Array<T>::Array() {
  19. wsk=0;
  20. dlugosc=0;
  21. }
  22.  
  23. template<class T>
  24. void Array<T>::dodaj(T& t) {
  25. T* tmp=new T[dlugosc+1];
  26. for(int i=0; i<dlugosc; ++i){
  27. tmp[i]=wsk[i];
  28. }
  29. delete[] wsk;
  30. wsk=tmp;
  31. wsk[dlugosc++]=t;
  32.  
  33. }
  34.  
  35. template<class T>
  36. bool Array<T>::usun(int i){
  37. if(i<0 || i>=dlugosc){
  38. return false;
  39. }
  40. else{
  41. int gdzie_wstawic=0;
  42. T* tmp=new T [dlugosc-1];
  43. for(int m=0; m<dlugosc; m++){
  44. if(m==i) continue;
  45. else{
  46. tmp[gdzie_wstawic]=wsk[m];
  47. gdzie_wstawic++;
  48. }
  49. }
  50. delete[] wsk;
  51. wsk=tmp;
  52. dlugosc--;
  53. return true;
  54. }
  55. }
  56.  
  57. template<class T>
  58. Array<T>::~Array() {
  59. delete[] wsk;
  60. wsk=0;
  61. }
  62.  
  63. template<class T>
  64. void Array<T>::wypisz(){
  65. for(int i=0; i<dlugosc; i++){
  66. std::cout<<wsk[i]<<std::endl;
  67. }
  68. }
  69.  
  70. template<class T>
  71. Array<T>& Array<T>::operator=(Array<T>& copy) {
  72.  
  73. if(copy.wsk!=this->wsk){
  74. delete[] wsk;
  75. wsk=new T[copy.dlugosc];
  76. dlugosc=copy.dlugosc;
  77. for(int i=0; i<dlugosc; ++i){
  78. wsk[i]=copy.wsk[i];
  79. }
  80. }
  81. return (*this);
  82. }
  83.  
  84. int main(){
  85. Array<int> lista;
  86. int i=5;
  87. int p=10;
  88. lista.dodaj(i);
  89. lista.dodaj(p);
  90. lista.wypisz();
  91. lista.usun(0);
  92. lista.wypisz();
  93. Array<int> lista2;
  94. lista2=lista;
  95. lista2.wypisz();
  96. return 0;
  97. }
Add Comment
Please, Sign In to add comment