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 ;
}
return temp;
}

3. SpreadSheetCellInStackTest.cpp

(在堆栈中创建并使用对象)

#include <iostream>
#include "SpreadsheetCell.h"
#include "SpreadSheetCellInStackTest.h" using namespace std; void SpreadSheetCellInStackTest::run()
{
SpreadsheetCell myCell, anotherCell;
myCell.setValue();
anotherCell.setString("3.2"); cout << "cell 1: " << myCell.getValue() << endl;
cout << "cell 2: " << anotherCell.getValue() << endl;
}

一个完整的C++程序SpreadSheet - 1) 类的声明和定义的更多相关文章

  1. 《Java从入门到失业》第四章:类和对象(4.3):一个完整的例子带你深入类和对象

    4.3一个完整的例子带你深入类和对象 到此为止,我们基本掌握了类和对象的基础知识,并且还学会了String类的基本使用,下面我想用一个实际的小例子,逐步来讨论类和对象的一些其他知识点. 4.3.1需求 ...

  2. 你好,C++(32) 类是对现实世界的抽象和描述 6.2.1 类的声明和定义

    6.2  类:当C++爱上面向对象 类这个概念是面向对象思想在C++中的具体体现:它既是封装的结果,同时也是继承和多态的载体.因此,要想学习C++中的面向对象程序设计,也就必须从“类”开始. 6.2. ...

  3. 关于C++的变量和类的声明和定义

    什么是变量?变量或者叫对象,是一个有具名的.可以供程序操作的存储空间.这里具名是指变量是有名字的,可供操作是指能进行加减乘除或者输入输出等操作,存储空间则是指有一块属于它的内存空间. 为了便于说明,标 ...

  4. C++类模板声明与定义为何不能分开

    我们用C++写类的时候,通常会将.cpp和.h文件分开写,即实现和声明分开写了:但在C++的类模板中,这种写法是错误的. 在<C++编程思想>的第16章的"16.3模板语法&qu ...

  5. ASP.NET + MVC5 入门完整教程八 -—-- 一个完整的应用程序(上)

    https://blog.csdn.net/qq_21419015/article/details/80509513 SportsStore 1.开始创建Visual Studio 解决方案和项目这里 ...

  6. 一个完整的Java程序示例

    (1) 第一个程序HelloWorld: package mypack; //相当于一个目录 public class HelloWorld{ public static void main(Stri ...

  7. 一个完整的hadoop程序开发过程

    目的 说明hadoop程序开发过程 前提条件 ubuntu或同类OS java1.6.0_45 eclipse-indigo hadoop-0.20.2 hadoop-0.20.2-eclipse-p ...

  8. ASP.NET + MVC5 入门完整教程八 -—-- 一个完整的应用程序(下)

    https://blog.csdn.net/qq_21419015/article/details/80802931 SportsStore 1.导航 添加导航控件 如果客户能够通过产品列表进行分类导 ...

  9. 逆向分析一个完整的C++程序包含寄存器与参数传递详解

    最近在分析C++ dump 文件的时候觉得有必要将一些必要的反汇编东西总结一下以备别人参考,自己有时间的时候也可以进行更多的改进.下面通过一个简单的C++代码转成汇编代码后的详细解释说明一下C++和汇 ...

随机推荐

  1. Vue 2.0学习(一)简介

    简介 Vue是一套用于构建用户界面的渐进式框架.简单小巧( 压缩后仅17KB),Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件.它不仅易于上手,还便于与第三方库或既 ...

  2. FastReport.Net使用:[21]表格(Table)控件

    对表格控件的一些常用操作 合并单元格:选择需要合并的单元格(按住Shitf多选),然后在右键菜单中选择[合并单元格].         2.删除/插入行 鼠标移到在行头,当鼠标状态变为向右的箭头时点击 ...

  3. POJ 2185 kmp

    题目链接:http://poj.org/problem?id=2185 题意:给出一个R*C(10000 * 75)的矩形字符串,然你求最小的不严格重复矩阵,比如,ABA,最小的重复矩阵是AB,经过复 ...

  4. JZYZOJ1237 教授的测试 dfs

    http://172.20.6.3/Problem_Show.asp?id=1237   锻炼搜索的代码能力,不错的题. 开始对dfs到底向下传递什么搞不清楚,需要想一下,noip难度的题还有这种情况 ...

  5. POJ3233 Matrix Power Series 矩阵乘法

    http://poj.org/problem?id=3233 挺有意思的..学习到结构体作为变量的转移, 题意 : 给定矩阵A,求A + A^2 + A^3 + ... + A^k的结果(两个矩阵相加 ...

  6. maven搭建企业级多模块项目

    1.创建一个maven项目 选择pom 完成 2.创建模块 项目右键选择module,创建模块.创建子模块 其余的打包时都为jar 地址:https://github.com/LeviFromCN/m ...

  7. 矩阵乘法快速幂 cojs 1717. 数学序列

    矩阵乘法模板: #define N 801 #include<iostream> using namespace std; #include<cstdio> int a[N][ ...

  8. [转]Jquery实现页面定时跳转

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) A. Little Artem and Presents 水题

    A. Little Artem and Presents 题目连接: http://www.codeforces.com/contest/669/problem/A Description Littl ...

  10. iOS 画圆

    _demoView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; [self.view addSubview:_de ...