#include #include #include // for move using namespace std; class Student { char* name; public: Student(); // default constructor Student(const char* n); Student(const Student& obj); // copy constructor Student(Student&& obj); // move constructor ~Student(); // destructor Student& operator=(const Student& obj); // assignment operator Student& operator=(Student&& obj); // move assignment operator const char* getName() const { return name ? name : ""; } }; ostream& operator<<(ostream& out, const Student& obj) { return out << "object=" << &obj << " name=" << obj.getName(); } Student::Student() : name(nullptr) { cout << "> In default constructor: " << *this << endl; } Student::Student(const char* n) : name(new char[strlen(n)+1]) { strcpy(name,n); cout << "> In Student(const char* n) ctor: " << *this << endl; } Student::Student(const Student& obj) : name(new char[strlen(obj.name+1)]) { strcpy(name,obj.name); cout << "> In copy constructor: " << *this << endl; } Student::Student(Student&& obj) : name(new char[strlen(obj.name+1)]) { strcpy(name,obj.name); cout << "> In move constructor: " << *this << endl; delete [] obj.name; obj.name = nullptr; } Student::~Student() { cout << "~ Student destructor " << *this << endl; if (name) delete [] name; name = nullptr; } Student& Student::operator=(const Student& obj) { delete [] name; name = new char[strlen(obj.name+1)]; strcpy(name,obj.name); cout << "= In assignment operator: " << *this << endl; return *this; } Student& Student::operator=(Student&& obj) { delete [] name; name = obj.name; cout << "= In move assignment operator: " << *this << endl; obj.name = nullptr; return *this; } Student create() { cout << "In create()\n"; return Student("Temporary");; } int main() { cout << "Executing line => Student j(\"Joe\");" << endl; Student j("Joe"); cout << "j = " << j << endl; cout << "\nExecuting line => Student h(j);" << endl; Student h(j); cout << "\nExecuting line => h = j;" << endl; h = j; cout << "\nExecuting line => j = create();" << endl; j = create(); cout << "j = " << j << endl; cout << "\nExecuting line => Student k(move(j));" << endl; Student k(move(j)); cout << "k = " << k << endl; cout << "j = " << j << endl; cout << "\nThat's all folks!!!" << endl; }