Copy and Addition (Concatenation) of string class based strings
A demonstration program to see how string objects of string class are copied or concatenated one after the another.
Learning Objectives
- Learning to copy and concatenation of string objects.
Source Code
|
Related (?) : |
Run Output
Code Understanding
string s1,s2,s3;
Three string class objects are declared;
s1=”Welcome”;
s1 instance is assigned a string literal.
s2=s1;
The contents of s1 are copied to s2
s3=s2+” to the world of c++”;
contents of s2 are concatenated with another string literal
cout<<s3<<endl;
Final concatenated string is printed
Notes
- While concatenation string class based objects do not need care regarding the size of destination string as for such objects sizes are dynamically allocated by the compiler itself. This is a major advantage of using string class as against c-style null terminated strings.
Suggested Filename(s): stringcl-copy-concat.cpp
sunmitra| Created: 25-Jan-2018 | Updated: 25-Jan-2018|