数据结构、算法与应用--类
来源:互联网 发布:c语言教程电子书 编辑:程序博客网 时间:2024/05/22 10:27
curr1.h
#ifndef Currency_#define Currency_#include <iostream>#include <stdlib.h>using namespace std;enum sign {plus, minus};class Currency {// public 部分用于定义一些函数 public://constructor 创建一个给定类型的对象,他不可以有返回值, Currency(sign s = plus, unsigned long d = 0, unsigned int c = 0); // destructor 当一个Currency对象超出作用域时将自动调用析构函数,不可以有返回值 ~Currency() {} bool Set(sign s, unsigned long d, unsigned int c); bool Set(float a); sign Sign() const {return sgn;} unsigned long Dollars() const {return dollars;} unsigned int Cents() const {return cents;} Currency Add(const Currency& x) const; Currency& Increment(const Currency& x); void Output() const;// private部分用于定义一些函数和数据成员,这些函数和数据成员对于用户是不可见得 private: sign sgn; unsigned long dollars; unsigned int cents;};// 在具体实现时,必须在每个函数名的前面加上Currency::,以指明该函数是Currency类的成员函数Currency::Currency(sign s, unsigned long d, unsigned int c){// Create a Currency object. if (c > 99) {// too many cents cerr << "Cents should be < 100" << endl;// 正常终止程序 exit(1);} sgn = s; dollars = d; cents = c;}// 验证参数合法性bool Currency::Set(sign s, unsigned long d, unsigned int c) {// Reset value. if (c > 99) return false; sgn = s; dollars = d; cents = c; return true;} // 处理小数点后面的头两个数字bool Currency::Set(float a) {// Reset value. if (a < 0) {sgn = minus; a = -a;} else sgn = plus; dollars = a; // extract integer part // get two decimal digits cents = (a + 0.001 - dollars) * 100; return true;}Currency Currency::Add(const Currency& x) const{// Add x and *this. long a1, a2, a3; Currency ans; // convert invoking object to signed integers a1 = dollars * 100 + cents; if (sgn == minus) a1 = -a1; // convert x to signed integer a2 = x.dollars * 100 + x.cents; if (x.sgn == minus) a2 = -a2; a3 = a1 + a2; // convert to currency representation if (a3 < 0) {ans.sgn = minus; a3 = -a3;} else ans.sgn = plus; ans.dollars = a3 / 100; ans.cents = a3 - ans.dollars * 100; return ans;}/*利用成员函数来设置数据成员的值可以确保数据成员拥有合法的值,如果数据成员被声明为public,还是在处理任务之前就要验证数据的合法性*/Currency& Currency::Increment(const Currency& x){// Increment by x. *this = Add(x); return *this;}void Currency::Output() const{// Output currency value. if (sgn == minus) cout << '-'; cout << '$' << dollars << '.'; if (cents < 10) cout << "0"; cout << cents;}#endif
curr1.cpp
// test currency class 我们一般把类的定义和类的实现分放在不同的文件中#include <iostream>#include <string>#include "curr1.h"using namespace std;void main(void){ Currency g, h(plus, 3, 50), i, j; g.Set(minus, 2, 25); i.Set(-6.45); j = h.Add(g); j.Output(); cout << endl; i.Increment(h); i.Output(); cout << endl; j = i.Add(g).Add(h); j.Output(); cout << endl; j = i.Increment(g).Add(h); j.Output(); cout << endl; i.Output(); cout << endl;}
0 0
- 数据结构、算法与应用--类
- 数据结构与算法:分治法应用总结
- 数据结构、算法与应用--习题(1)
- [数据结构与算法]Huffman树及其应用
- 数据结构,算法与应用(1)
- 数据结构,算法与应用(2)
- 数据结构,算法与应用(3)
- 数据结构,算法与应用(4)
- 【数据结构与算法】treemap应用 排序
- 《数据结构、算法与应用》C++语言描述
- 数据结构与算法的应用场景
- 数据结构与算法3: 栈的算法应用(Stack applications)
- 数据结构算法应用
- 数据结构与算法 -- 算法
- 数据结构中关键路径算法的实现与应用
- 数据结构中关键路径算法的实现与应用
- 数据结构中关键路径算法的实现与应用
- 数据结构算法与应用第二章-程序性能
- poj 3134 Power Calculus (IDA*)
- 计算机科学期刊介绍--各种杂志投稿方式与评价
- 24 java 常用的方法
- 快速排序
- 开发过程中如何提高用户体验(1)
- 数据结构、算法与应用--类
- Git 和 GitHub学习更多资料
- OSB 自定义XPath Function
- toad 显示某一用户下的所有表结构信息
- iphone imei查询方法
- ffmpeg采集视频+压缩转码成h264+实时播放。
- 关于“CSDN提问”的瞎想
- Linux apr的安装
- 体会函数参数传递(2)