convert between ordinary strings and class String : Your string « Data Types « C++ Tutorial






#include <iostream>  
  using namespace std;  
  #include <string.h>             
  
  class String                    
    {  
     private:  
        enum { SZ = 80 };         
        char str[SZ];             
     public:  
        String() { str[0] = '\0'; }  
        String( char s[] ) { strcpy(str, s); }    
        void display() const { cout << str; }  
        operator char*() { return str; }        
    };  

  int main()  
    {  
     String s1;                   
                                  
     char xstr[] = "this is a test";  
    
     s1 = xstr;                   
                                    
     s1.display();                
    
     String s2 = "this is another test";  
                                  
     cout << static_cast<char*>(s2);
      cout << endl;                   
      return 0;                     
    }








2.42.Your string
2.42.1.Your own string class
2.42.2.Define and use a string class
2.42.3.convert between ordinary strings and class String
2.42.4.strings defined using array and pointer notation