Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. struct Ball {
  6.     int x;          // поля, свойства
  7.     int y;
  8.     char* name;
  9.    
  10. };
  11.  
  12. bool isEqual(Ball b1, Ball b2)
  13. {
  14.     if (b1.x != b2.x)
  15.         return false;
  16.     if (b1.y != b2.y)
  17.         return false;
  18.     if (strcmp(b1.name, b2.name) != 0)
  19.         return false;
  20.        
  21.     return true;
  22. }
  23.  
  24. void printBall(Ball ball)
  25. {
  26.     cout << "X = " << ball.x << "\nY = " << ball.y << endl;
  27.     cout << ball.name << endl;
  28. }
  29.  
  30. void resetBall(Ball* pB)     // ожидается адрес переменной типа Ball
  31. {
  32.     cout << pB << endl;
  33.     pB->x = 0;
  34.     pB->y = 0;
  35. }
  36.  
  37. Ball makeCopy(Ball b)
  38. {
  39.     Ball temp;
  40.     temp.x = b.x;
  41.     temp.y = b.y;
  42.     temp.name = new char[strlen(b.name) + 1];
  43.     strcpy(temp.name, b.name);
  44.    
  45.     return temp;
  46. }
  47.  
  48. int main()
  49. {
  50.     Ball b = {0, 0};
  51.     b.x = 200;
  52.     b.y = 300;
  53.     b.name = new char[100];
  54.     strcpy(b.name, "Ball 1");
  55.     cout << sizeof(b) << endl;
  56.     printBall(b);
  57.    
  58.     Ball d = makeCopy(b);     // копирование без выделения памяти (неглубокое копирование)
  59.     printBall(d);
  60.    
  61.     strcpy(b.name, "New name");
  62.     printBall(b);
  63.     printBall(d);
  64.    
  65.     delete [] b.name;
  66.     delete [] d.name;
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement