标签:
1. SpreadsheetCell.h
#pragma once #include <string> class SpreadsheetCell { public: void setValue(double inValue); double getValue() const; void setString(const std::string& inString); const std::string& getString() const; private: std::string doubleToString(double inValue) const; double stringToDouble(const std::string& inString) const; double mValue; std::string mString; };
2. SpreadsheetCell.cpp
#include "SpreadsheetCell.h" #include <iostream> #include <sstream> using namespace std; void SpreadsheetCell::setValue(double inValue) { mValue = inValue; mString = doubleToString(mValue); } double SpreadsheetCell::getValue() const { return mValue; } void SpreadsheetCell::setString(const string& inString) { mString = inString; mValue = stringToDouble(mString); } const string& SpreadsheetCell::getString() const { return mString; } string SpreadsheetCell::doubleToString(double inValue) const { ostringstream ostr; ostr << inValue; return ostr.str(); } double SpreadsheetCell::stringToDouble(const string& inString) const { double temp; istringstream istr(inString); istr >> temp; if (istr.fail() || !istr.eof()) { return 0; } return temp; }
3. SpreadSheetCellInStackTest.cpp
(在堆栈中创建并使用对象)
#include <iostream> #include "SpreadsheetCell.h" #include "SpreadSheetCellInStackTest.h" using namespace std; void SpreadSheetCellInStackTest::run() { SpreadsheetCell myCell, anotherCell; myCell.setValue(6); anotherCell.setString("3.2"); cout << "cell 1: " << myCell.getValue() << endl; cout << "cell 2: " << anotherCell.getValue() << endl; }
一个完整的C++程序SpreadSheet - 1) 类的声明和定义
标签:
原文地址:http://www.cnblogs.com/davidgu/p/4640721.html