Exercises

1.What would happen if we compiled the program in Figure 8.37? Why?
Figure 8.37. Exercise 1 (codestrex5.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 n("Test");
  string x = n;

  n = "My name is Susan";

  return 0;
}

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

int main()
{
  string n;

  n = "My name is Susan";

  return 0;
}

3.We have already implemented operator < and operator ==. However, a concrete data type that allows for ordered comparisons such as < should really implement all six of the comparison operators. The other four of these operators are >, >=, <=, and != ("greater than", "greater than or equal to", "less than or equal to", and "not equal to", respectively). Add the declarations of each of these operators to the string interface definition.
4.Implement the four comparison operators that you declared in the previous exercise.
5.Write a test program to verify that all of the comparison operators work. This program should test that each of the operators returns the value true when its condition is true; equally important, it should test that each of the operators returns the value false when the condition is not true.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset