Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4.  
  5. class Dog {
  6. private:
  7. char* name;
  8. char* breed;
  9. int age;
  10. public:
  11. Dog(const char* dName = "", const char* dBreed = "", int dAge = 0);
  12. Dog(const Dog&);
  13. Dog& operator=(const Dog&);
  14. ~Dog();
  15. };
  16.  
  17.  
  18. int main() {
  19.  
  20.  
  21. return 0;
  22. }
  23.  
  24. Dog::Dog(const char * dName, const char * dBreed, int dAge)
  25. {
  26. name = new char(strlen(dName) + 1);
  27. strcpy(name, dName);
  28. breed = new char(strlen(dBreed) + 1);
  29. strcpy(breed, dBreed);
  30. age = dAge;
  31. }
  32.  
  33. Dog::Dog(const Dog &other)
  34. {
  35. name = new char(strlen(other.name) + 1);
  36. strcpy(name, other.name);
  37. breed = new char(strlen(other.breed) + 1);
  38. strcpy(breed, other.breed);
  39. age = other.age;
  40. }
  41.  
  42. Dog & Dog::operator=(const Dog &other)
  43. {
  44. if (this != &other) {
  45. delete[] name;
  46. delete[] breed;
  47. name = new char(strlen(other.name) + 1);
  48. strcpy(name, other.name);
  49. breed = new char(strlen(other.breed) + 1);
  50. strcpy(breed, other.breed);
  51. age = other.age;
  52. }
  53. return *this;
  54. }
  55.  
  56. Dog::~Dog()
  57. {
  58. delete[] name;
  59. delete[] breed;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement