constructors 1.
#include iostream
#include string
#include 
using namespace std;
int main () 
{
    char *line = "short line for testing";
    
    // with no arguments
    string s1;
    s1 = "Anatoliy";
    cout << "s1  is: " << s1 << endl;
    // copy constructor
    string s2 (s1);
    cout << "s2  is: " << s2 << endl;
    // one argumen
    string s3 (line);
    cout << "s3  is: " << s3 << endl;
    // first argumen C string
    // second number of characters
    string s4 (line,10);
    cout << "s4  is: " << s4 << endl;
    // 1 - C++ string
    // 2 - start position
    // 3 - number of characters
    string s5 (s3,6,4); // copy word 'line' from s3
    cout << "s5  is: " << s5 << endl;
    // 1 - number characters
    // 2 - character itself
    string s6 (15,'*');
    cout << "s6  is: " << s6 << endl;
    // 1 - start iterator
    // 2 - end iterator
    string s7 (s3.begin(),s3.end()-5);
    cout << "s7  is: " << s7 << endl;
    // you can instantiate string with assignment
    string s8 = "Anatoliy";
    cout << "s8  is: " << s8 << endl;
    return 0;
}
........
OUTPUT:
// s1  is: Anatoliy
// s2  is: Anatoliy
// s3  is: short line for testing
// s4  is: short line
// s5  is: line
// s6  is: ***************
// s7  is: short line for te
// s8  is: Anatoliy
 
No comments:
Post a Comment