? / professional-program-09-OperatorOverloading-AddFirstAttempt-SpreadsheetCell.c
include <SpreadsheetCell.h> include <iostream> include <sstream> using namespace std; SpreadsheetCell::SpreadsheetCell() : mValue(0), mNumAccesses(0) { } SpreadsheetCell::SpreadsheetCell(double initialValue) : mNumAccesses(0) { set(initialValue); } SpreadsheetCell::SpreadsheetCell(const string& initialValue) : mValue(stringToDouble(initialValue)), mString(initialValue), mNumAccesses(0) { } SpreadsheetCell::SpreadsheetCell(const SpreadsheetCell& src) { mValue = src.mValue; mString = src.mString; mNumAccesses = src.mNumAccesses; } SpreadsheetCell& SpreadsheetCell::operator=(const SpreadsheetCell& rhs) { if (this == &rhs) { return (*this); } mValue = rhs.mValue; mString = rhs.mString; mNumAccesses = rhs.mNumAccesses; return (*this); } void SpreadsheetCell::set(double inValue) { mValue = inValue; mString = doubleToString(mValue); } void SpreadsheetCell::set(const string& inString) { mString = inString; mValue = stringToDouble(mString); } string SpreadsheetCell::doubleToString(double inValue) { ostringstream ostr; ostr << inValue; return (ostr.str()); } double SpreadsheetCell::stringToDouble(const string& inString) { double temp; istringstream istr(inString); istr >> temp; if (istr.fail() || !istr.eof()) { return (0); } return (temp); } const SpreadsheetCell SpreadsheetCell::add(const SpreadsheetCell& cell) const { SpreadsheetCell newCell; newCell.set(mValue + cell.mValue); // call set to update mValue and mString return (newCell); }
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.