TOC PREV NEXT INDEX

C++: A Dialog


7.10. 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 = s;

return 0;
}

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

int main()
{
string s("Test");

return 0;
}

4. What would happen if a user of our string class wrote an expression that tried to set a string variable to itself (e.g., a = a;)?

www.steveheller.com
steve@steveheller.com
TOC PREV NEXT INDEX