Online Book Reader

Home Category

Professional C__ - Marc Gregoire [99]

By Root 1358 0
is not empty. You might want this verification routine to be outside the SpreadsheetCell class to model an external audit, but the function should be able to access the internal data members of the object in order to check it properly. Here is the SpreadsheetCell class definition with a friend checkSpreadsheetCell() function:

class SpreadsheetCell

{

public:

friend bool checkSpreadsheetCell(const SpreadsheetCell& cell);

// Omitted for brevity

};

Code snippet from Friends\SpreadsheetCell.h

The friend declaration in the class serves as the function’s prototype. There’s no need to write the prototype elsewhere (although it’s harmless to do so).

Here is the function definition:

bool checkSpreadsheetCell(const SpreadsheetCell& cell)

{

return !(cell.mString.empty());

}

Code snippet from Friends\SpreadsheetCell.cpp

You write this function just like any other function, except that you can directly access private and protected data members of the SpreadsheetCell class. You don’t repeat the friend keyword on the function definition.

friend classes and methods are easy to abuse; they allow you to violate the principle of abstraction by exposing internals of your class to other classes or functions. Thus, you should use them only in limited circumstances such as operator overloading because in that case you need access to protected and private members, as discussed in the next section.

OPERATOR OVERLOADING


You often want to perform operations on objects, such as adding them, comparing them, or streaming them to or from files. For example, spreadsheets are really only useful when you can perform arithmetic actions on them, such as summing an entire row of cells.

Example: Implementing Addition for SpreadsheetCells

In true object-oriented fashion, SpreadsheetCell objects should be able to add themselves to other SpreadsheetCell objects. Adding a cell to another cell produces a third cell with the result. It doesn’t change either of the original cells. The meaning of addition for SpreadsheetCells is the addition of the values of the cells. The string representations are ignored.

First Attempt: The add Method

You can declare and define an add() method for your SpreadsheetCell class like this:

class SpreadsheetCell

{

public:

// Omitted for brevity

const SpreadsheetCell add(const SpreadsheetCell& cell) const;

// Omitted for brevity

};

Code snippet from OperatorOverloading\AddFirstAttempt\SpreadsheetCell.h

This method adds two cells together, returning a new third cell whose value is the sum of the first two. It is declared const and takes a reference to a const SpreadsheetCell because add() does not change either of the source cells. It returns a const SpreadsheetCell because you don’t want users to change the return value. They should just assign it to another object. add() is a method, so it is called on one object and passed another. Here is the implementation:

const SpreadsheetCell SpreadsheetCell::add(const SpreadsheetCell& cell) const

{

SpreadsheetCell newCell;

newCell.set(mValue + cell.mValue); // update mValue and mString

return newCell;

}

Code snippet from OperatorOverloading\AddFirstAttempt\SpreadsheetCell.cpp

Note that the implementation creates a new SpreadsheetCell called newCell and returns a copy of that cell. You might be tempted to return a reference to the cell instead. However, that will not work because as soon as the add() method ends and newCell goes out of scope it will be destroyed. The reference that you returned will then be a dangling reference.

You can use the add() method like this:

SpreadsheetCell myCell(4), anotherCell(5);

SpreadsheetCell aThirdCell = myCell.add(anotherCell);

Code snippet from OperatorOverloading\AddFirstAttempt\SpreadsheetCellTest.cpp

That works, but it’s a bit clumsy. You can do better.

Second Attempt: Overloaded operator+ as a Method

It would be convenient to be able to add two cells with the plus sign the way that you add two ints or two doubles. Something like this:

SpreadsheetCell myCell(4), anotherCell(5);

SpreadsheetCell

Return Main Page Previous Page Next Page

®Online Book Reader