fork download
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4. class base
  5. {
  6. char* p;
  7. int len;
  8. public:
  9. base(char *str){
  10. len = strlen(str);
  11. p = new char[len];
  12. strcpy(p,str);
  13. }
  14. ~base(){
  15. delete [] p;
  16. }
  17. base &operator=(base &obj){
  18. if(obj.len>len){
  19. delete [] p;
  20. p = new char[obj.len];
  21. strcpy(p,obj.p);
  22. len = obj.len;
  23. }
  24. return *this;
  25. }
  26. char *display(){
  27. return p;
  28. }
  29. };
  30. int main ()
  31. {
  32. base ob1("go"), ob2("school");
  33.  
  34. ob1 = ob2;
  35. cout<<ob1.display();
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
school