fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class Person {
  6. char* name;
  7. friend class Couple;
  8. void setName( const char * newName ) {
  9. if( name != nullptr )
  10. delete( name );
  11.  
  12. int nameLen = strlen( newName );
  13. name = new char[nameLen + 1];
  14. strcpy( name, newName );
  15. name[nameLen] = '\0';
  16. }
  17. public:
  18. friend ostream& operator<<( ostream& str, const Person& os );
  19. explicit Person( const char* n );
  20. Person( const Person& os );
  21. Person& operator=( const Person& os );
  22. ~Person();
  23. };
  24. // implementacja zadeklarowanych funkcji
  25. ostream& operator<<( ostream& str, const Person& os ) {
  26. str << os.name;
  27. return str;
  28. }
  29.  
  30. Person::Person( const char* n ) : name( nullptr ) {
  31. setName( n );
  32. }
  33. Person::Person( const Person& os ) : Person(os.name) {}
  34. Person& Person::operator=( const Person& os ) { setName( os.name ); return *this; }
  35.  
  36. Person::~Person() {
  37. cout << "DEL " << *this << endl;
  38. if( name != nullptr ) delete( name );
  39. };
  40.  
  41. class Couple {
  42. Person *wife, *husband;
  43. public:
  44. friend ostream& operator<<( ostream& str, const Couple& p );
  45. Couple( const Person& she, const Person& he );
  46. Couple( const Couple& other );
  47. Couple& operator=( const Couple& other );
  48. ~Couple();
  49. };
  50. // implementacja zadeklarowanych funkcji
  51. ostream& operator<<( ostream& str, const Couple& c ) {
  52. str << "Couple: he " << *(c.husband) << ", she " << *(c.wife) << endl;
  53. return str;
  54. }
  55.  
  56. Couple::Couple( const Person& she, const Person& he ) { wife = new Person( she ); husband = new Person( he ); }
  57. Couple::Couple( const Couple& other ) : Couple( *( other.wife ), *( other.husband) ) {}
  58. Couple& Couple::operator=( const Couple& other ) {
  59. if( wife != nullptr )
  60. delete wife;
  61.  
  62. wife = new Person( *( other.wife ) );
  63.  
  64. if( husband != nullptr )
  65. delete husband;
  66.  
  67. husband = new Person( *( other.husband ) );
  68.  
  69. return *this;
  70. }
  71.  
  72. Couple::~Couple() {
  73. //cout << "DEL " << *this << endl;
  74. if( wife != nullptr )
  75. delete wife;
  76.  
  77. if( husband != nullptr )
  78. delete husband;
  79. }
  80.  
  81.  
  82. int main( void ) {
  83. Person *pjohn = new Person( "John" ),
  84. *pjane = new Person( "Jane" );
  85.  
  86. Person mary( "Mary" ),
  87. mark( "Mark" );
  88.  
  89. Couple *pcouple1 = new Couple( mary, *pjohn );
  90.  
  91. Couple couple2( *pjane, mark );
  92.  
  93. delete pjohn; // DEL John
  94. delete pjane; // DEL Jane
  95.  
  96. cout << *pcouple1 << endl; // Couple : he John, she Mary
  97. cout << couple2 << endl; // Couple : he Mark, she Jane
  98. couple2 = *pcouple1;
  99. /*
  100. DEL Jane
  101. DEL Mark
  102. */
  103.  
  104. delete pcouple1;
  105.  
  106. /*
  107. DEL Mary
  108. DEL John
  109. */
  110.  
  111. cout << couple2; // Couple : he John, she Mary
  112.  
  113. return 0;
  114. /*
  115. DEL Mary
  116. DEL John
  117. DEL Mark
  118. DEL Mary
  119. */
  120. }
Success #stdin #stdout 0s 5628KB
stdin
Standard input is empty
stdout
DEL John
DEL Jane
Couple: he John, she Mary

Couple: he Mark, she Jane

DEL Jane
DEL Mark
DEL Mary
DEL John
Couple: he John, she Mary
DEL Mary
DEL John
DEL Mark
DEL Mary