Exercises

1.What would happen if we compiled the program in Figure 7.19? Why?
Figure 7.19. Exercise 1 (code\strex1.cpp)
class string
{
public:
  string();
  string(const string& Str);
  string(char* p);
  string& operator = (const string& Str);
  ~string();
private:
  short m_Length;
  char* m_Data;
};

int main()
{
  string s;
  string n("Test");
  string x;
  short Length;
  Length = n.m_Length;

  s = n;
  n = "My name is Susan";

  x = n;
  return 0;
}
2.What would happen if we compiled the program in Figure 7.20? Why?
Figure 7.20. Exercise 2 (code\strex2.cpp)
 class string { public: string(const string& Str); string(char* p); string& operator=(const string& Str); ~string(); private: string(); short m_Length; char* m_Data; }; int main() { string s("Test"); string n; n = ...

Get C++: A Dialog Programming with the C++ Standard Library 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.