#include using namespace std; // Definition for Class SL class SL { private: // Each object contains two values int *ptr; // Pointer to int int val; // Just an int public: // Member functions SL(); ~SL(); SL(const SL &); SL SL::operator=(const SL & fr); void Readin(); void Printout(); void Erase(); }; // Constructor for Class SL SL::SL() {ptr = 0; cout << "allocated SL " << this << endl; } SL::~SL() {cout << "deallocated SL " << this << endl; this->Erase();} // Copy Constructor for Class SL SL::SL(const SL & fr) {cout << "Copy Constructructor from SL " << &fr << " to SL " << this << endl; this->val = fr.val; this->ptr = 0; if (fr.ptr) {ptr = new int; *ptr = *(fr.ptr); } return; } SL SL::operator=(const SL & fr){ cout << "Assignment Operator from SL " << &fr << " to SL " << this << endl; // Avoiding potential Memory Leak here. if (this == &fr) return (fr); this->val = fr.val; this->ptr = 0; if (fr.ptr) {ptr = new int; *ptr = *(fr.ptr); } return (*this); } void SL::Printout() // Printing out the contents of a SL { if (ptr == 0) {cout << "data " << " 0 value none int " << val << endl; return;} cout << "data " << ptr << " value " << *ptr << " int " << val << endl; return; } void SL::Readin() // Reading in the contents of a SL { cout << "Input two values" << endl; ptr = new int; cin >> *ptr >> val; } void SL::Erase() { delete(ptr); ptr=0; } #include "listNode.h" int main() { cout << endl; SL l; // A80 l.Readin(); cout << "SL l" << " Located at " << &l << endl; l.Printout(); cout << endl; // A80 A08 4 3 SL m; // A70 m.Readin(); cout << "SL m" << " Located at " << &m<< endl; m.Printout(); cout << endl; // A70 A18 2 1 SL o = m; // A60 // There is a correct copy constructor so it works cout << "SL o" << " Located at " << &o<< endl; o.Printout(); cout << endl; // Copy from A70 A60 // A60 A28 2 1 SL q; // A50 // Works just fine q = m; cout << "SL q" << " Located at " << &q<< endl; q.Printout(); cout << endl; // Assign from A70 to A50 // Copy from A50 to A40 // Destroy A40 // A50 A38 2 1 l.Erase(); m.Erase(); cout << "SL l" << " Located at " << &l<< endl; l.Printout(); // A80 A0 -- 3 cout << "SL m" << " Located at " << &m<< endl; m.Printout(); // A70 A0 -- 1 cout << "SL o" << " Located at " << &o<< endl; o.Printout(); // A60 A28 2 1 cout << "SL q" << " Located at " << &q<< endl; q.Printout(); // A50 A38 2 1 cout << endl; SL r,s; //A40 A30 // Will also work r = s = q; // Assignment from A50 to A30 // Copy from A30 to A10 // Assignment from A10 to A40 // Copy from A40 to A20 // Dealloc A20 // Dealloc A10 cout << "SL s" << " Located at " << &s<< endl; s.Printout(); // A30 A18 2 1 cout << "SL r" << " Located at " << &r<< endl; r.Printout(); // A40 A58 2 1 cout << "SL q" << " Located at " << &q<< endl; q.Printout(); // A50 A38 2 1 cout << endl; return 0; }