Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- No.9: Use of dynamic constructors
- Created By GSC,CSE,IIUC.
- Date: 17-02-15
- Time: 2:18:01 am
- Facebook: www.facebook.com/gsc.cse
- E-mail: sarwar.pustcse1@gmail.com
- Skype: sarwar_pust_cse
- Mobile: 01861500457
- */
- # include <iostream>
- # include <cstring>
- using namespace std;
- class dycon
- {
- char *st;
- int len;
- public:
- dycon()
- {
- //len=0;
- //st=new char[len+1];
- }
- dycon(char *s)
- {
- len=strlen(s);
- st=new char[len+1];// one additional space for \0 (Null)
- strcpy(st,s);
- }
- void show()
- {
- cout<<"NAME IS: "<<st<<endl;
- }
- void join(dycon &a,dycon &b);// copy constructor
- };
- void dycon::join(dycon &a,dycon &b)
- {
- len=a.len+b.len;
- delete st;// free merory
- st=new char[len+1];// dynamic allocation
- strcpy(st,a.st);
- strcat(st,b.st);
- };
- main()
- {
- char *first="Just ";
- dycon n1(first),n2("Start "),n3("Now"),n4,n5;
- n4.join(n1,n2);
- n5.join(n4,n3);
- n1.show();
- n2.show();
- n3.show();
- n4.show();
- n5.show();
- }
Add Comment
Please, Sign In to add comment