Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class str_plus
  4. {
  5. int len;
  6. char *p;
  7. public:
  8. str_plus(char *str);
  9. str_plus();
  10. str_plus operator+(char *s);
  11. str_plus operator=(str_plus ob);
  12. void show();
  13.  
  14. };
  15. str_plus::str_plus()
  16. {
  17. len=1;
  18. p=(char *)malloc(sizeof(char)*len);
  19. strcpy(p,"*");
  20. }
  21. str_plus::str_plus(char *str)
  22. {
  23. len=strlen(str)+1;
  24. p=(char *)malloc(sizeof(char)*len);
  25. strcpy(p,str);
  26.  
  27. }
  28. str_plus str_plus::operator+(char *s)
  29. {
  30. p=(char *)realloc(p,sizeof(char)*strlen(s));
  31. strcat(p,s);
  32. return *this;
  33. }
  34. str_plus str_plus::operator=(str_plus ob)
  35. {
  36. p=(char *)realloc(p,sizeof(char)*strlen(ob.p));
  37. strcpy(p,ob.p);
  38. return *this;
  39. }
  40. void str_plus::show()
  41. {
  42. cout<<p<<"\n";
  43. }
  44. int main()
  45. {
  46. int n,k;
  47. str_plus ob1("mahmud"),ob2("mushi"),ob3();
  48. //ob2=ob1+"is good ";
  49. ob1=ob1+" is a good";
  50. ob2=ob2+" is a good boy";
  51. // ob3=ob3+"hello world"
  52. ///this shows error
  53. ob2.show();
  54. ob1.show();
  55. ///after "is a good" unknown random chars
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement