Guest User

Untitled

a guest
Dec 12th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. // Copy Assignment Operator
  2. #include <iostream>
  3. #include <cstring>
  4.  
  5. using namespace std;
  6.  
  7. class Name {
  8. int m_len;
  9. char *m_p;
  10. public:
  11. Name(const char *p)
  12. {
  13. m_len = strlen(p);
  14. m_p = (char *)malloc(m_len + 1);
  15. ////
  16. strcpy(m_p, p);
  17. }
  18.  
  19. Name(const Name &r)
  20. {
  21. m_len = r.m_len;
  22. m_p = (char *)malloc(m_len + 1);
  23. ///
  24. strcpy(m_p, r.m_p);
  25. }
  26.  
  27. Name &operator=(const Name &r)
  28. {
  29. if (this == &r)
  30. return *this;
  31.  
  32. free(m_p);
  33.  
  34. m_len = r.m_len;
  35. m_p = (char *)malloc(m_len + 1);
  36. ///
  37. strcpy(m_p, r.m_p);
  38.  
  39. return *this;
  40. }
  41.  
  42. ~Name()
  43. {
  44. free(m_p);
  45.  
  46. }
  47. void display()const
  48. {
  49. cout << "(" << m_p << ")" << endl;
  50. }
  51. ////
  52. };
  53.  
  54.  
  55. int main()
  56. {
  57. Name x{"Kerem Vatandas"};
  58.  
  59. x = x;
  60.  
  61.  
  62. x.display();
  63.  
  64.  
  65. return 0;
  66. }
Add Comment
Please, Sign In to add comment