Containment

As you have seen in previous examples, it is possible for the member data of a class to include objects of another class. C++ programmers say that the outer class contains the inner class. Therefore, an Employee class might contain string objects (for the name of the employee) as well as integers (for the employee's salary), and so forth.

Listing 20.3 is a stripped-down but useful String class.

Listing 20.3. The String Class
 0: #include <iostream> 1: #include <string.h> 2: 3: class String 4: { 5: public: 6: // constructors 7: String(); 8: String(const char *const); 9: String(const String &); 10: ~String(); 11: 12: // overloaded operators 13: char & operator[](int offset); 14: char operator[](int offset) const; 15: String operator+(const ...

Get Sams Teach Yourself C++ in 24 Hours, Third Edition 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.