Chapter 12. Adding Class to C++

In This Chapter

  • Grouping data into classes

  • Declaring and defining class members

  • Adding active properties to the class

  • Accessing class member functions

  • Overloading member functions

Programs often deal with groups of data: a person's name, rank, and serial number, stuff like that. Any one of these values is not sufficient to describe a person — only in the aggregate do the values make any sense. A simple structure such as an array is great for holding stand-alone values, but it doesn't work well for data groups. This makes good ol' arrays inadequate for storing complex data (such as personal credit records that the Web companies maintain so they can lose them to hackers).

For reasons that will become clear shortly, I'll call such a grouping of data an object. A microwave oven is an object (see Chapter 11 if that doesn't make sense). You are an object (no offence). Your savings account information in a database is an object.

Introducing the Class

How nice it would be if we could create objects in C++ that have the relevant properties of the real-world objects we're trying to model. What we need is a structure that can hold all the different types of data necessary to describe a single object. C++ calls the structure that combines multiples pieces of data into a single object a class.

The Format of a Class

A class consists of the keyword class followed by a name and an open and closed brace. A class used to describe a savings account including account number and ...

Get C++ For Dummies®, 6th 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.