Online Book Reader

Home Category

Professional C__ - Marc Gregoire [72]

By Root 1106 0
Even if you are already comfortable with classes and objects, you should skim this chapter because it contains various tidbits of information with which you might not yet be familiar.

INTRODUCING THE SPREADSHEET EXAMPLE


This chapter and the next present a runnable example of a simple spreadsheet application. A spreadsheet is a two-dimensional grid of “cells,” and each cell contains a number or string. Professional spreadsheets such as Microsoft Excel provide the ability to perform mathematical operations, such as calculating the sum of the values of a set of cells. The spreadsheet example in these chapters does not attempt to challenge Microsoft in the marketplace, but is useful for illustrating the issues of classes and objects.

The spreadsheet application uses two basic classes: Spreadsheet and SpreadsheetCell. Each Spreadsheet object contains SpreadsheetCell objects. In addition, a SpreadsheetApplication class manages a collection of Spreadsheets. This chapter focuses on the SpreadsheetCell. Chapter 7 develops the Spreadsheet and SpreadsheetApplication classes.

This chapter shows several different versions of the SpreadsheetCell class in order to introduce concepts gradually. Thus, the various attempts at the class throughout the chapter do not always illustrate the “best” way to do every aspect of class writing. In particular, the early examples omit important features that would normally be included, but have not yet been introduced. You can download the final version of the class as described in the Introduction.

WRITING CLASSES


When you write a class you specify the behaviors, or methods, that will apply to objects of that class and the properties, or data members, that each object will contain.

There are two components in the process of writing classes: defining the classes themselves and defining their methods.

Class Definitions

Here is a first attempt at a simple SpreadsheetCell class, in which each cell can store only a single number:

class SpreadsheetCell

{

public:

void setValue(double inValue);

double getValue() const;

protected:

double mValue;

};

Code snippet from SpreadsheetCellNumOnly\SpreadsheetCell.h

As described in Chapter 1, every class definition begins with the keyword class and the name of the class. A class definition is a statement in C++, so it must end with a semicolon. If you fail to terminate your class definition with a semicolon, your compiler will probably give you several errors, most of which will appear to be completely unrelated.

Class definitions usually go in a file named after the class. For example, the SpreadsheetCell class definition can be put in a file called SpreadsheetCell.h. This rule is not enforced and you are free to call your file however you like.

Methods and Members

The two lines that look like function prototypes declare the methods that this class supports:

void setValue(double inValue);

double getValue() const;

Chapter 1 points out that it is always a good idea to declare member functions that do not change the object as const.

The line that looks like a variable declaration declares the data member for this class.

double mValue;

A class defines the methods and members that apply, but the methods and members typically apply only to a specific instance of the class, which is an object. Classes define concepts; objects contain real bits. So, each object will contain its own value for the mValue variable. The implementation of the methods is shared across all objects. Classes can contain any number of methods and members. You cannot give a member the same name as a method.

Access Control

Every method and member in a class is subject to one of three access specifiers: public, protected, or private. An access specifier applies to all method and member declarations that follow it, until the next access specifier. In the SpreadsheetCell class, the setValue() and getValue() methods have public access, while the mValue member has protected access.

The default access specifier for classes is private: all method and member declarations

Return Main Page Previous Page Next Page

®Online Book Reader