I learned this from the book
Professional C++. And code this example to deepen the this practical pattern.
The whole design is very compact, and very practical. I think that it is very suitable for applications in the future as an example of engineering practice. Hope you like it.
I have drew the UML class diagrams for overview the classes:
SpreadsheetCell.h
#ifndef SPREAD_SHEET_CELL_H
#define SPREAD_SHEET_CELL_H
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class Spreadsheet;
class SpreadsheetCell
{
public:
typedef enum {Red=1, Green, Blue, Yellow} Colors;
SpreadsheetCell& operator+=(const SpreadsheetCell& rhs);
SpreadsheetCell& operator-=(const SpreadsheetCell& rhs);
SpreadsheetCell& operator*=(const SpreadsheetCell& rhs);
SpreadsheetCell& operator/=(const SpreadsheetCell& rhs);
const SpreadsheetCell operator-() const;
SpreadsheetCell(double initialValue = 0.0);
explicit SpreadsheetCell(const string& initialValue);
SpreadsheetCell(const SpreadsheetCell& src);
SpreadsheetCell& operator=(const SpreadsheetCell& rhs);
void set(double inValue);
void set(const string& inString);
void setColor(Colors color);
double getValue() const;
string getString() const;
const SpreadsheetCell operator+(const SpreadsheetCell& cell) const;
const SpreadsheetCell operator+(double rhs) const;
SpreadsheetCell& operator++(); //Prefix
SpreadsheetCell& operator--(); //Prefix
SpreadsheetCell operator++(int); //Postfix
SpreadsheetCell operator--(int); //Postfix
friend class Spreadsheet;
//Supply the friend method is not a good idea, it will break the encapsulation.
friend bool checkSpreadsheetCell(const SpreadsheetCell& cell);
friend const SpreadsheetCell operator+(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
friend const SpreadsheetCell operator-(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
friend const SpreadsheetCell operator*(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
friend const SpreadsheetCell operator/(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
friend bool operator==(const SpreadsheetCell& lhs,const SpreadsheetCell& rhs);
friend bool operator<(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
friend bool operator>(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
friend bool operator!=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
friend bool operator<=(const