CSE2050 Exam #2, open book, open notes. Name __________________________ An alternative implementation of the string-like class Str (ch. 12) is a NUL terminated array of char. This simplifies the implementation of c_str(). class Str { private: char* data; // Array of size()+1 chars with '\0' in last element int siz; // size() not including the '\0' public: int size() const {return siz;} Str(const char* p=""): siz(strlen(p)) { data=new char[size()+1]; copy(p, p+size()+1, data); // Copies the '\0' } const char* c_str() const {return data;} // Other members for questions 4-7... }; 1. What do the following print? (3 pts each) Str s1; cout << s1.size(); _______________ 0 Str s2="abcde"; cout << s2.size(); _______________ 5 cout << s2.c_str(); _______________ abcde cout << s2.c_str()[2]; _______________ c cout << s2.c_str()+2; _______________ cde 2. Which const (first or second) in c_str() ensures that the user cannot write into the elements of data? (5 pts) ___ First (If c_str() returned char*, then it would be possible to do things like s2.c_str()[2] = 'x', changing s2.data[2] to 'x') 3. Given only the methods defined above, write operator == as a non-member function (15 pts). bool operator == (const Str& s1, const Str& s2) { if (s1.size() != s2.size()) return false; for (int i=0; i v(3, 'x'); Str s3(v.begin(), v.end()); // "xxx" template Str(T b, T e): siz(e-b) { data = new char[siz+1]; // or size()+1 copy(b, e, data); // or use a loop data[siz] = '\0'; // or data[size()] = 0; }