Using new to get memory for strings - C++ Operator

C++ examples for Operator:new

Description

Using new to get memory for strings

Demo Code

#include <iostream>
#include <cstring>       //for strcpy(), etc
using namespace std;
class String             //user-defined string type
{
   private://w  ww.j  a  v a 2s. com
   char* str;                    //pointer to string
   public:
   String(char* s)               //constructor, one arg
   {
      int length = strlen(s);    //length of string argument
      str = new char[length+1];  //get memory
      strcpy(str, s);            //copy argument to it
   }
   ~String()                     //destructor
   {
      cout << "Deleting str\n";
      delete[] str;              //release memory
   }
   void display()                //display the String
   {
      cout << str << endl;
   }
};
int main()
{                                //uses 1-arg constructor
    String s1 = "Who knows nothing doubts nothing.";
    cout << "s1=";                   //display string
    s1.display();
    return 0;
}

Result


Related Tutorials