Review Questions

1:Suppose a String class has the following private members:
class String
{
private:
    char * str;    // points to string allocated by new
    int len;       // holds length of string
//...
};
  1. What's wrong with this default constructor?

    String::String() {}
    
  2. What's wrong with this constructor?

    String::String(const char * s)
    {
         str = s;
         len = strlen(s);
    }
    
  3. What's wrong with this constructor?

    String(const char * s)
    {
        strcpy(str, s);
        len = strlen(s);
    }
    
2:Name three problems that may arise if you define a class in which a pointer member is initialized using new and indicate how they can be remedied.
3:What class methods does the compiler generate automatically if you don't provide them explicitly? Describe how these implicitly generated functions ...

Get The Waite Group's C++ Primer Plus, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.