Convert between ordinary strings and class String - C++ Class

C++ examples for Class:Operator Overload

Description

Convert between ordinary strings and class String

Demo Code

#include <iostream>
using namespace std;
#include <string.h>             //for strcpy(), etc.
class String                    //user-defined string type
{
   private:/*from www.  ja  v  a  2 s.com*/
       enum { SZ = 80 };         //size of all String objects
       char str[SZ];             //holds a C-string
   public:
       String()                  //no-arg constructor
       { str[0] = '\0'; }
       String( char s[] )        //1-arg constructor
       { strcpy(str, s); }    //   convert C-string to String
       void display() const
       { cout << str; }
       operator char*()          //conversion operator
       { return str; }        //convert String to C-string
};
int main()
{
   String s1;                   //use no-arg constructor
   char xstr[] = "Joyeux Noel! ";
   s1 = xstr;                   //use 1-arg constructor
   //   to convert C-string to String
   s1.display();
   String s2 = "Bonne Annee!";  //uses 1-arg constructor to initialize String
   cout << static_cast<char*>(s2); //use conversion operator
   cout << endl;                //to convert String to C-string
   return 0;                 //before sending to << op
}

Result


Related Tutorials