#include "RefCountedString.h" #include using namespace std; int main() { // Create a brand-new RefCountedString. RefCountedString fruit1("apple"); cout << "Fruit1: " << fruit1 << endl; // Create another RefCountedString from an existing one. This calls the copy constructor. RefCountedString fruit2(fruit1); cout << "Fruit2: " << fruit2 << endl; // Create yet another RefCountedString from an existing one. This also calls the copy constructor. RefCountedString fruit3 = fruit2; cout << "Fruit3: " << fruit3 << endl << endl; // Change fruit1. This should snap off a separate representation object. fruit1.ToUpperCase(); cout << "After modifying fruit1... " << endl; cout << "Fruit1: " << fruit1 << endl; cout << "Fruit2: " << fruit2 << endl; cout << "Fruit3: " << fruit3 << endl << endl; // Change fruit1 again. It should just keep its existing (sole) representation object this time. cout << "Apples are sooooooo dull. Gimme a more exotic fruit: "; cin >> fruit1; cout << "Fruit1: " << fruit1 << endl; cout << "Fruit2: " << fruit2 << endl; cout << "Fruit3: " << fruit3 << endl << endl; // Change fruit2 now, to be the same as fruit1. cout << fruit1 << " - what a great fruit. I'm gonna use it again!" << endl; fruit2 = fruit1; cout << "Fruit1: " << fruit1 << endl; cout << "Fruit2: " << fruit2 << endl; cout << "Fruit3: " << fruit3 << endl << endl; // Let's see what's equal. cout << "fruit1==fruit2? " << (fruit1 == fruit2) << endl; cout << "fruit2==fruit3? " << (fruit2 == fruit3) << endl; cout << "fruit1==fruit3? " << (fruit1 == fruit3) << endl << endl; // Etc :-) return 0; }