O'Reilly logo

Sams Teach Yourself C++ in 24 Hours, Third Edition by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required