Online Book Reader

Home Category

Professional C__ - Marc Gregoire [118]

By Root 1546 0
void set(const std::string& inString);

virtual std::string getString() const;

Finally, the string cell adds a protected data member, mValue, which stores the actual cell data:

protected:

std::string mValue;

};

String Spreadsheet Cell Implementation

The .cpp file for StringSpreadsheetCell is a bit more interesting than the base class. In the constructor, mValue is initialized to a string that indicates that no value has been set.

StringSpreadsheetCell::StringSpreadsheetCell() : mValue("#NOVALUE") { }

Code snippet from PolymorphicSpreadsheet\StringSpreadsheetCell.cpp

The set method is straightforward since the internal representation is already a string. Similarly, the getString() method returns the stored value.

void StringSpreadsheetCell::set(const string& inString)

{

mValue = inString;

}

string StringSpreadsheetCell::getString() const

{

return mValue;

}

Code snippet from PolymorphicSpreadsheet\StringSpreadsheetCell.cpp

Double Spreadsheet Cell Class Definition and Implementation

The double version follows a similar pattern, but with different logic. In addition to the set() method that takes a string, it also provides a new set() method that allows a client to set the value with a double. Two new protected methods are used to convert between a string and a double. As in StringSpreadsheetCell, it has a data member called mValue, this time a double. Because DoubleSpreadsheetCell and StringSpreadsheetCell are siblings, no hiding or naming conflicts occur as a result.

class DoubleSpreadsheetCell : public SpreadsheetCell

{

public:

DoubleSpreadsheetCell ();

virtual void set(double inDouble);

virtual void set(const std::string& inString);

virtual std::string getString() const;

protected:

static std::string doubleToString(double inValue);

static double stringToDouble(const std::string& inValue);

double mValue;

};

Code snippet from PolymorphicSpreadsheet\DoubleSpreadsheetCell.h

The implementation of the DoubleSpreadsheetCell constructor is as follows.

DoubleSpreadsheetCell::DoubleSpreadsheetCell() : mValue(-1) { }

Code snippet from PolymorphicSpreadsheet\DoubleSpreadsheetCell.cpp

To keep this example simple, mValue is initialized to -1. In production code you would probably initialize it to NaN which stands for “Not a Number.” In C++ you could do this with std::numeric_limits::quiet_NaN().

The set() method that takes a double is straightforward. The string version uses the protected static method stringToDouble(). The getString() method converts the stored double value into a string:

void DoubleSpreadsheetCell::set(double inDouble)

{

mValue = inDouble;

}

void DoubleSpreadsheetCell::set(const string& inString)

{

mValue = stringToDouble(inString);

}

string DoubleSpreadsheetCell::getString() const

{

return doubleToString(mValue);

}

Code snippet from PolymorphicSpreadsheet\DoubleSpreadsheetCell.cpp

You may already see one major advantage of implementing spreadsheet cells in a hierarchy — the code is much simpler. You don’t need to worry about using two fields to represent the two types of data. Each object can be self-centered and only deal with its own functionality.

Note that the implementations of doubleToString() and stringToDouble() were omitted because they are the same as in Chapter 6.

Leveraging Polymorphism

Now that the SpreadsheetCell hierarchy is polymorphic, client code can take advantage of the many benefits that polymorphism has to offer. The following test program explores many of these features:

int main()

{

Code snippet from PolymorphicSpreadsheet\SpreadsheetTest.cpp

To demonstrate polymorphism, this test program declares an array of three SpreadsheetCell pointers. Remember that since SpreadsheetCell is an abstract class, you can’t create objects of that type. However, you can still have a pointer or reference to a SpreadsheetCell because it would actually be pointing to one of the subclasses. This array, because it is an array of the parent type SpreadsheetCell, allows you to store a heterogeneous mixture of the two subclasses. This

Return Main Page Previous Page Next Page

®Online Book Reader