C++ new operator get memory for char array
#include <iostream> #include <cstring> //for strcpy(), etc using namespace std; class String //user-defined string type { private://from w w w . j a v a 2 s.c om 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; }