Question: Suppose a String class has the following private members: class String { private: char * str; // points to string allocated by new int len;

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
//...
};

a. What’s wrong with this default constructor?
String::String() {}
b. What’s wrong with this constructor?
String::String(const char * s)
{
str = s;
len = strlen(s);
}
c. What’s wrong with this constructor?
String::String(const char * s)
{
strcpy(str, s);
len = strlen(s);
}


Step by Step Solution

3.45 Rating (174 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

a There is no problem with this default constructor but it might do a lot of work for an empt... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Introduction Java Program Questions!