Shallow Copy and Deep Copy Example
Define string class with dynamic memory allocation for string. Define default constructor, parameterized constructors, copy constructor, destructor, Overload +, [], =, <<, >> operators. Observe the behavior of shallow copying and deep copying. #include<iostream> using namespace std; #include<string.h> class cstring { char *sptr; int length; public: cstring() { sptr=NULL; length=0; } cstring(const char *s) { sptr=new char[strlen(s)+1]; strcpy(sptr,s); length=strlen(s); } cstring(const cstring &s) { sptr=new char[s.length+1]; strcpy(sptr,s.sptr); length=s.length; } void print_cstring() { cout<<sptr<<endl; } void scan_cstring() { sptr=new char[10]; cin>>sptr; } ~cstring() { if(sptr!=NULL) delete []sptr; } cstring operator=(cstring c) { delete []sptr; sptr=new char[strlen(c.sptr)+1]; strcpy(sptr,c.sptr); length=c.length; retu...