Chapter 22. Structured Play: Making Classes Do Things
In This Chapter
Adding member functions to a class
Defining the member function
Invoking the member function
Accessing one member from another member
Overloading member functions
Classes were introduced to the C language as a convenient way to group unalike but related data elements — for example, the Social Security number and name of the same person. That's the way I introduce them in Chapter 19. C++ expanded the concept of classes to give them the ability to mimic objects in the real world. That's the essence of the difference between C and C++.
In the previous chapter, I review at a high level the concept of object-oriented programming. In this chapter, I make it more concrete by examining the active features of a class that allow them to better mimic the object-oriented world we live in.
Activating Our Objects
C++ uses classes to simulate real-world objects. However, the classes in Chapter 19 are lacking in that regard because classes do things. (The classes in Chapter 19 don't have any verbs associated with them — they don't do anything.) Consider for example, a savings account. It is necessary for a Savings
class to save the owner's name, probably her Social Security number, certainly her account number and balance. But this isn't sufficient.
Objects in the real world do things. Ovens cook. Savings accounts accumulate interest. CDs charge a substantial penalty for early withdrawal. Stuff like that.
Consider the problem of handling ...
Get Beginning Programming with C++ For Dummies® 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.