Chp~15 - C+ PROGRAMMING Chapter 15 Advanced Inheritance...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ PROGRAMMING Chapter 15 Advanced Inheritance Containment Listing 15.1. The String class . Listing 15.2. The Employee class and driver program . Accessing Members of the Contained Class Filtering Access to Contained Members Cost of Containment Listing 15.3. Contained class constructors . Copying by Value Listing 15.4. Passing by value . Implementation in Terms of Inheritance/Containment Versus Delegation Delegation Listing 15.5. Delegating to a contained LinkedList . Private Inheritance Listing 15.6. Private inheritance . Friend Classes Listing 15.7. Friend class illustrated . Friend Class Friend Functions Friend Functions and Operator Overloading Listing 15.8. Friendly operator+. Friend Functions Overloading the Insertion Operator Listing 15.9. Overloading operator<<(). Summary Quiz Exercises
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Chapter 15 Advanced Inheritance So far you have worked with single and multiple inheritance to create is-a relationships. ToChapter you will learn What containment is and how to model it. What delegation is and how to model it. How to implement one class in terms of another. How to use private inheritance. 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. Thus, 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 15.1. The String class. 1: #include <iostream.h> 2: #include <string.h> 3: 4: class String 5: { 6: public: 7: // constructors 8: String(); 9: String(const char *const); 10: String(const String &); 11: ~String(); 12: 13: // overloaded operators 14: char & operator[](int offset); 15: char operator[](int offset) const; 16: String operator+(const String&); 17: void operator+=(const String&); 18: String & operator= (const String &); 19: 20: // General accessors 21: int GetLen()const { return itsLen; } 22: const char * GetString() const { return itsString; } 23: // static int ConstructorCount; 24: 25: private: 26: String (int); // private constructor 27: char * itsString; 28: unsigned short itsLen;
Background image of page 2
29: 30: }; 31: 32: // default constructor creates string of 0 bytes 33: String::String() 34: { 35: itsString = new char[1]; 36: itsString[0] = `\0'; 37: itsLen=0; 38: // cout << "\tDefault string constructor\n"; 39: // ConstructorCount++; 40: } 41: 42: // private (helper) constructor, used only by 43: // class methods for creating a new string of 44: // required size. Null filled. 45: String::String(int len) 46: { 47: itsString = new char[len+1]; 48: for (int i = 0; i<=len; i++) 49: itsString[i] = `\0'; 50: itsLen=len; 51: // cout << "\tString(int) constructor\n"; 52: // ConstructorCount++; 53: } 54: 55: // Converts a character array to a String 56: String::String(const char * const cString)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CS 131 taught by Professor Clayton during the Spring '08 term at Bethune Cookman.

Page1 / 44

Chp~15 - C+ PROGRAMMING Chapter 15 Advanced Inheritance...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online