Chapter 7. Working with Classes

In This Chapter

  • Understanding objects and classes and the difference between the two

  • Becoming familiar with member functions and variables in a class

  • Making parts of a class public, private, and protected

  • Using constructors and destructors

  • Building hierarchies of classes

Back in the early 1990s, the big buzzword in the computer world was object-oriented. For anything to sell, it had to be object-oriented, whatever that meant. Programming languages were object-oriented. Software applications were object-oriented. Computers were object-oriented. Refrigerators were object-oriented. What did that all mean? Nothing. It was simply a catchphrase that was cool at the time.

Those days are gone, and now we can explore what object-oriented really means and how you can use it to organize your C++ programs. In this chapter, we introduce object-oriented programming and show how you can do it in C++. Although people disagree on the strict definition of object-oriented, in this book it means programming with objects and classes.

Understanding Objects and Classes

Consider a pen, a regular, old pen. One of us actually has a pen on our desk. Here's what we can say about it:

  • Ink Color: Black

  • Shell Color: Light gray

  • Cap Color: Black

  • Style: Ballpoint

  • Length: Six inches

  • Brand: Office Depot

  • Ink Level: 50 percent full

  • Capability #1: Write on paper

  • Capability #2: Break in half

  • Capability #3: Run out of ink

Now, look around for other things. We see a printer. Let us describe that:

  • Kind: Laser

Get C++ All-In-One For Dummies®, 2nd 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.