{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter14 -...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Viewing Hints  ] [  Exercise Solutions  ] [  Volume 2  ] [  Free Newsletter  ]  Seminars  ] [  Seminars on CD ROM  ] [  Consulting  ]  Thinking in C++, 2nd ed. Volume 1 © 2000 by Bruce Eckel Previous Chapter  ] [  Table of Contents  ] [  Index  ] [  Next Chapter  ]  14: Inheritance & Composition One of the most compelling features about C++ is  code reuse. But to be revolutionary, you need to be  able to do a lot more than copy code and change it. That’s the C approach, and it hasn’t worked very well. As with most everything in C++, the solution  revolves around the class. You reuse code by creating new classes, but instead of creating them from  scratch, you use existing classes that someone else has built and debugged. The trick is to use the classes without soiling the existing code. In this chapter you’ll see two ways to  accomplish this. The first is quite straightforward: You simply create objects of your existing class  inside the new class. This is called  composition  because the new class is composed of objects of  existing classes. The second approach is subtler. You create a new class as a  type of  an existing class. You literally  take the form of the existing class and add code to it, without modifying the existing class. This  magical act is called  inheritance , and most of the work is done by the compiler. Inheritance is one of  the cornerstones of object-oriented programming and has additional implications that will be  explored in Chapter 15. It turns out that much of the syntax and behavior are similar for both composition and inheritance  (which makes sense; they are both ways of making new types from existing types). In this chapter,  you’ll learn about these code reuse mechanisms. Composition syntax Actually, you’ve been using composition all along to create classes. You’ve just been composing  classes primarily with built-in types (and sometimes  string s). It turns out to be almost as easy to use  composition with user-defined types.
Image of page 1

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

View Full Document Right Arrow Icon
Consider a class that is valuable for some reason: //: C14:Useful.h // A class to reuse #ifndef USEFUL_H #define USEFUL_H class X { int i; public : X() { i = 0; } void set( int ii) { i = ii; } int read() const { return i; } int permute() { return i = i * 47; } }; #endif // USEFUL_H ///:~ The data members are  private  in this class, so it’s completely safe to embed an object of type  X  as a  public  object in a new class, which makes the interface straightforward: //: C14:Composition.cpp // Reuse code with composition #include "Useful.h" class Y { int i; public : X x; // Embedded object Y() { i = 0; } void f( int ii) { i = ii; } int g() const { return i; } }; int main() { Y y; y.f(47); y.x.set(37); // Access the embedded object } ///:~
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern