#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); friend ostream & operator<<(ostream & os, const SL &); friend istream & operator>>(istream &, SL &); 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); } ostream & operator<<(ostream & os, const SL & fr) { if (fr.ptr == 0) {os << "data " << " 0 value none int " << fr.val << endl; return os;} os << "data " << fr.ptr << " value " << *(fr.ptr) << " int " << fr.val << endl; return os; } istream & operator>>(istream & is, SL & fr) { int contents; cout << "Input two values" << endl; fr.ptr = new int; is >> *(fr.ptr) >> fr.val; return is; } void SL::Erase() { delete(ptr); ptr=0; } int main() { cout << endl; SL l; // Ab0 cin >> l; cout << "SL l" << " Located at " << &l << endl; cout << l; cout << endl; // Ab0 A08 4 3 SL m; // Aa0 cin >> m; cout << "SL m" << " Located at " << &m<< endl; cout << m; cout << endl; // Aa0 A18 2 1 l.Erase(); m.Erase(); cout << "SL l" << " Located at " << &l<< endl; cout << l; // Ab0 A0 --- 3 cout << "SL m" << " Located at " << &m<< endl; cout << m; // Aa0 A0 --- 1 cout << endl; return 0; }