Advertisement
borisdexter

copy/=/dinamicko alociranje

Mar 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. class Runner{
  6. private:
  7. char *ime;
  8. int *metri;
  9. int n;
  10. public:
  11.     Runner(){}
  12.     Runner(char *i,int *m,int n){
  13.     ime=new char[strlen(i)+1];
  14.     strcpy(ime,i);
  15.     this->n=n;
  16.     metri = new int[n];
  17.     for(int i=0;i<n;i++){
  18.         this->metri[i]=m[i];
  19.     }
  20.     }
  21.     // Runner koljo;
  22.     // Runner labi;
  23.     // koljo=labi;
  24.    
  25.     Runner& operator=(Runner &ob){
  26.     this->ime=new char[strlen(ob.ime)+1];
  27.     strcpy(this->ime,ob.ime);
  28.     this->n=ob.n;
  29.     metri=new int[ob.metri];
  30.     for(int i=0;i<this->n;i++){
  31.         this->metri[i]=ob.metri[i];
  32.     }
  33.     return *this;
  34.     }
  35.     Runner(const Runner &ob){ // malce tezok copy constructor
  36.     this->ime=new char[strlen(ob.ime)+1];  
  37.     strcpy(this->ime,ob.ime);
  38.     this->n=ob.n;
  39.     metri = new int[ob.n];
  40.     for(int i=0;i<ob.n;i++){
  41.         this->metri[i]=ob.metri[i];
  42.     }
  43.     }
  44.  
  45. void pecati(){
  46. cout<<this->ime<<endl;
  47. }
  48. ~Runner(){
  49. delete []ime;
  50. delete []metri;
  51. }
  52. };
  53. int main(){
  54. Runner Labi("ime",niza,5);
  55. Runner koljo(Labi); // copy constructor
  56. Runner koljo=Labi;// pak copy constructor
  57.  
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement