p32929

UVA - 123

Apr 20th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1.     /*
  2.     No.9: Use of dynamic constructors
  3.  
  4.     Created By GSC,CSE,IIUC.
  5.     Date: 17-02-15
  6.     Time: 2:18:01 am
  7.     Facebook: www.facebook.com/gsc.cse
  8.     E-mail: sarwar.pustcse1@gmail.com
  9.     Skype: sarwar_pust_cse
  10.     Mobile: 01861500457
  11.     */
  12. # include <iostream>
  13. # include <cstring>
  14. using namespace std;
  15. class dycon
  16. {
  17.     char *st;
  18.     int len;
  19.     public:
  20.         dycon()
  21.         {
  22.             //len=0;
  23.             //st=new char[len+1];
  24.         }
  25.         dycon(char *s)
  26.         {
  27.             len=strlen(s);
  28.             st=new char[len+1];// one additional space for \0 (Null)
  29.             strcpy(st,s);
  30.         }
  31.         void show()
  32.         {
  33.             cout<<"NAME IS: "<<st<<endl;
  34.         }
  35.         void join(dycon &a,dycon &b);// copy constructor
  36. };
  37.  
  38. void dycon::join(dycon &a,dycon &b)
  39. {
  40.     len=a.len+b.len;
  41.     delete st;// free merory
  42.     st=new char[len+1];// dynamic allocation
  43.     strcpy(st,a.st);
  44.     strcat(st,b.st);
  45. };
  46.  
  47. main()
  48. {
  49.     char *first="Just ";
  50.     dycon n1(first),n2("Start "),n3("Now"),n4,n5;
  51.  
  52.     n4.join(n1,n2);
  53.     n5.join(n4,n3);
  54.     n1.show();
  55.     n2.show();
  56.     n3.show();
  57.     n4.show();
  58.     n5.show();
  59. }
Add Comment
Please, Sign In to add comment