Advertisement
Constructor | Description |
---|---|
string() | This constructor is used to create an empty string object. |
string(const string & str) | This constructor is used to create a string object, by passing a string object to it. The new string object is initialized with the content of the passed string object. |
string(const char *st) | This constructor is used to create a string object, by passing a char array to it. The new string object is initialized with the content of the passed char array. |
string s1();
string s2("Hello There");
string s1("Hello There");
string s2(s1);
char arr[] = {'W', 'e', 'l', 'c', 'o', 'm', 'e', '\0'}; // line 1
string s(arr); // line 2
Advertisement
//Initializing strings in different ways.
#include<iostream>
#include<string>
using namespace std;
int main()
{
//Creating first string object by passing a string literal to constructor
string str1("New York");
//Creating second string object, initializing from another string
string str2(str1);
//Creating a third string empty object.
string str3;
str3 ="London";
//Creating fourth string object by assigning a string literal
string str4 = "Poland";
cout<<"String 4 : " << str4 << "\n";
cout<<"String 1 : " << str1 << "\n";
cout<<"String 2 : " << str2 << "\n";
cout<<"String 3 : " << str3 << "\n";
//Creating fifth string object by adding two string objects by + operator
string str5 = str1+str2;
cout<<"String 4 : " << str5;
}
String 4 : Poland
String 1 : New York
String 2 : New York
String 3 : London
String 4 : New YorkNew York
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement