#include<iostream.h>
#include<conio.h>
#include<string.h>
class String
{
char *name;
int length;
public:
String()
{
length=0;
name=new char[length+1];
}
String(char *s)
{
length=strlen(s);
name=new char[length+1];
strcpy(name,s);
}
void display()
{
cout<<name;
}
void join(String &a,String &b);
};
void String::join(String &a,String &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
}
void main()
{
char *c1,*c2,*c3;
clrscr();
cout<<"\t \t DYNAMIC CONSTRUCTOR \n";
cout<<"\t \t ------------------------ \n";
cout<<"\n Enter the string1:";
cin>>c1;
cout<<"\n Enter the string2:";
cin>>c2;
cout<<"\n Enter the string3:";
cin>>c3;
String s1(c1),s2(c2),s3(c3),s4,s5;
cout<<"\n Before Concatenation \n";
cout<<"\n ------------------ \n";
cout<<"\n\n String1:";
s1.display();
cout<<"\n\n String2:";
s2.display();
cout<<"\n\n String3:";
s3.display();
cout<<"\n";
cout<<"\n After Concatenation";
cout<<"\n -----------------";
s4.join(s1,s2);
s5.join(s4,s3);
cout<<"\n\n String4 concatenation of (string1 & string2):";
s4.display();
cout<<"\n\n String5 concatenation of (string4 & string3):";
s5.display();
getch();
}
-------------
OUTPUT:
-------------
Dynamic Constructor
--------------------------
Enter the string1: Welcome
Enter the string2: To
Enter the string3: All
Before concatenation
--------------------------
String1 : Welcome
String2: To
String3: All
After concatenation
------------------------
String4 concatenation of (string1 & string2): Welcome To
String5 concatenation of (string4 & string3): Welcome To All
#include<conio.h>
#include<string.h>
class String
{
char *name;
int length;
public:
String()
{
length=0;
name=new char[length+1];
}
String(char *s)
{
length=strlen(s);
name=new char[length+1];
strcpy(name,s);
}
void display()
{
cout<<name;
}
void join(String &a,String &b);
};
void String::join(String &a,String &b)
{
length=a.length+b.length;
delete name;
name=new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
}
void main()
{
char *c1,*c2,*c3;
clrscr();
cout<<"\t \t DYNAMIC CONSTRUCTOR \n";
cout<<"\t \t ------------------------ \n";
cout<<"\n Enter the string1:";
cin>>c1;
cout<<"\n Enter the string2:";
cin>>c2;
cout<<"\n Enter the string3:";
cin>>c3;
String s1(c1),s2(c2),s3(c3),s4,s5;
cout<<"\n Before Concatenation \n";
cout<<"\n ------------------ \n";
cout<<"\n\n String1:";
s1.display();
cout<<"\n\n String2:";
s2.display();
cout<<"\n\n String3:";
s3.display();
cout<<"\n";
cout<<"\n After Concatenation";
cout<<"\n -----------------";
s4.join(s1,s2);
s5.join(s4,s3);
cout<<"\n\n String4 concatenation of (string1 & string2):";
s4.display();
cout<<"\n\n String5 concatenation of (string4 & string3):";
s5.display();
getch();
}
-------------
OUTPUT:
-------------
Dynamic Constructor
--------------------------
Enter the string1: Welcome
Enter the string2: To
Enter the string3: All
Before concatenation
--------------------------
String1 : Welcome
String2: To
String3: All
After concatenation
------------------------
String4 concatenation of (string1 & string2): Welcome To
String5 concatenation of (string4 & string3): Welcome To All
0 comments:
Post a Comment