1: // Filename: TESTREM3.CPP 2: // Purpose: When used with REM3.H/REM3.CPP, shows the problems that 3: // arise when you have a class with dynamic data and yet you 4: // *don't* have an overloaded assignment operator = and/or a 5: // copy constructor. When used with REM4.H/REM4.CPP, shows 6: // that those problems go away when you *do* have an overloaded 7: // assignment operator and a copy constructor. 9: #include <iostream> 10: using namespace std; 12: #include "REM?.H" // Note: ? to be replaced first by 3, then by 4 13: #include "PAUSE.H" 15: int main() 16: { 17: cout << endl; 19: // Illustrates that the kind of copy made by assignment depends 20: // on whether the assignment operator has been overloaded. 21: Reminder r1_1(20000101, "First message"); 22: Reminder r1_2; 23: r1_2 = r1_1; // Copy is "shallow" with REM3.*, "deep" with REM4.*. 24: cout << "r1_1: "; r1_1.Display(); Pause(0); 25: cout << "r1_2: "; r1_2.Display(); Pause(0); 26: r1_1.MutilateMessageString(); 27: cout << "r1_1: "; r1_1.Display(); Pause(0); 28: cout << "r1_2: "; r1_2.Display(); Pause(0); 29: cout << endl; 31: // Illustrates that the kind of copy made by initialization 32: // depends on whether a copy constructor has been provided. 33: Reminder r2_1(20000202, "Second message"); 34: Reminder r2_2 = r2_1; // Copy is "shallow" with REM3.*, "deep" with REM4.*. 35: cout << "r2_1: "; r2_1.Display(); Pause(0); 36: cout << "r2_2: "; r2_2.Display(); Pause(0); 37: r2_1.MutilateMessageString(); 38: cout << "r2_1: "; r2_1.Display(); Pause(0); 39: cout << "r2_2: "; r2_2.Display(); Pause(0); 40: cout << endl; 42: // Again illustrates that the kind of copy made by initialization 43: // depends on whether a copy constructor has been provided. 44: // Note the alternate form of initialization used this time around. 45: Reminder r3_1(20000303, "Third message"); 46: Reminder r3_2(r3_1); // Copy is "shallow" with REM3.*, "deep" with REM4.*. 47: cout << "r3_1: "; r3_1.Display(); Pause(0); 48: cout << "r3_2: "; r3_2.Display(); Pause(0); 49: r3_1.MutilateMessageString(); 50: cout << "r3_1: "; r3_1.Display(); Pause(0); 51: cout << "r3_2: "; r3_2.Display(); Pause(0); 52: cout << endl; 54: return 0; 55: }