String Declaring and intializing string objects
· The string object declaration and intialization can be done once by using a constructor of the
string class.
Constructor Meaning
string() ; produces an empty string
string(const char *text) produces a string object from a null-ended string
string(const string & text) produces a string object from another string obj
we can declare and intialize string object as follows :
string text ; // using constructors without arguments
string text("C++") ; // using constructors with one argument
text1=text2 ; // Assignment of two string objects
text="C++" + text1 // concatenation of string objects
cin>>text // reading string without spaces through the keyboard
getline(cin,text) // reading string with blank page
Write a program for string objects
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
clrscr();
string text;
string text("C++");
string text2("OOP");
cout<<"\n text1:"<<text1;
cout<<"\n text2:"<<text2;
text=text1;
cout<<"\n text:"<<text;
text=text1+" " +text2;
cout<<"\n now text:"<<text;
getch();
OUTPUT
text1: C++
text2: OOP
text : C++
now text : C++ OOP