当前位置:文档之家› 5.5 类与对象应用的例子

5.5 类与对象应用的例子


Pump::Pump(float start, float todays) { amtint=start; price=todays; } void Pump::values( ) { cout<<"The gas tank has: "<<amtint<<endl; cout<<"The price per kg of gas is: "<<price<<endl; } void Pump::request( float pumpamt) { float pumped; if( amtint>=pumpamt ) pumped=pumpamt; //满足 else pumped= amtint; amtint=amtint-pumped; //加油后剩余数 cout<<pumpamt<<" kg were requested"<<endl; cout<<pumped<<" kg were pumped"<<endl; cout<<amtint<<" kg remain in the tank"<<endl; cout<<"The total price is: "<<(pumped*price)<<endl; }
♦ 关于操作的具体分析如下:
油泵进入服务 初始化油桶中的汽油数量 初始化每公斤汽油的价格 显示操作(函数) 显示油桶中汽油的数量 显示每公斤汽油的价格 抽出汽油进行加油的操作(函数) IF(油桶中汽油数量大于或等于需要被抽出的汽油数 量)将抽出数量设置成需要抽出的数量 ELSE将抽数量设置成油桶中所剩汽油的数量 ENDIF 将油桶中汽油的数量减去被抽出汽油的数量,计算总 金额(汽油单价乘以被抽出数量),显பைடு நூலகம்被抽出的汽油数 量,显示油桶中剩下的汽油数量,显示应付金额。
3.单实例对象类 ♦ 在一些应用环境中只允许一个对象实例, 但按照前面介绍的定义方法,必须先定义 类,然后声明这个类的多个对象,这样, 我们对声明对象实例的个数无法进行控制。 ♦ 为控制对象实例的个数,我们可以在设计 类时引入一个静态数据成员来记录当前对 象实例的个数,并让这个对象均共享这一 计数器。
Pump::Pump(floatstart, float todays) { amtint=start; price=todays; } Void Pump::values( ) { cout<<“The gas tank havs: “<<amtint<<endl; cout<<“The price per kg of gas is: “<<price<<endl; }
1)问题分析 ♦ 根据要求,我们可设计出如下形式的油 泵对象模型:
2)关于操作的具体分析 ♦ 油泵进入服务
初始化油桶中汽油的数量 初始化每公斤汽油的价格

显示操作(函数)
显示油桶中汽油的数量 显示每公斤汽油的价格
♦ 抽出汽油进行加油的操作(函数)
IF (油桶中汽油数量大于或等于需要被抽出的汽油数量) 将抽出数量设置成需要抽出的数量 ELSE 将抽出数量设置成油桶中所剩汽油的数量 ENDIF 将油桶中汽油的数量减去被抽出汽油的数量 计算总金额(汽油单价乘以被抽出数量) 显示被抽出的汽油数量 显示油上剩下的汽油数量 显示应付金额
2.模拟加油站油泵的对象工作 ♦ 正常情况下,我们在任何时候,应该知道 每公斤汽油的价格和油泵所在的油桶中还 有多少汽油。当一个加油的请求出现时, 如果要求加油的数量少于(或等于)油泵 中的汽油数量时,就满足这个加油请求。 否则,只能抽出油桶所剩下的汽油给加油。 ♦ 每次抽出汽油加油后,应显出被抽出加油 的汽油数量及价格;同时,还要计算出汽 油桶里剩余的汽油量。
//程序:SINGLE.CPP //单实例类的实现文件 #include"single.hpp" #include<iostream.h> //初始化实例计数,注意静态数据成员的这种用法 int SINGLE_INSTANCE::instance_count=0; //构造函数中必须为实例计数加1 SINGLE_INSTANCE::SINGLE_INSTANCE( ) { instance_count+=1; if(instance_count>1) cout<<"Wanring:more than one object instance!=n"; return; } //析构函数中必须为实例计数减1 SINGLE_INSTANCE::~SINGLE_INSTANCE( ) { instance_count-=1; return; }
void main( ) //演示程序 { Pump obj(AMTINT, PRICE); //创建一个油泵对象 obj.values( ); //显示当前状态 cout<<endl; obj.values(30.0); //要求加油 cout<<endl; obj.request(280.0); //两次要求加油 }
程序如下
♦ //功能:加油站油泵实现文件
#include<iostream.h> #include<iomanip.h> const float AMTINT=300.0; //寝化油桶中的油量 const float PRICE=1.25; //单价 Class Pump { protected: float amtint, price; public: Pump(float, float); void values( ); void request(float); };
§5.5 类与对象应用的例子
1. 使用类和对象构造程序的实例
♦ [例]请用C++语言构造 一个模拟加油站油泵的对
象工作 ♦ 正常情况下,我们在任何时刻,应该知道每公 斤汽油的价格和油泵所在的油桶中还有多少汽油。 当一个加油的请求出现时,如果要求加油的数量 少于(或等于)油桶中的汽油数量时,就满足这个 加油请求。否则,只能抽出油桶所剩下的汽油给 予加油。 ♦ 每次抽出汽油加油后,应显出被抽出加油的汽 油数量及价格;同时,还要计算出加油后汽油桶 里剩余的汽油量。
//程序:SGL_DEMO.CPP //功能:演示单实例对象的用法 #include"single.hpp" #include<iostream.h> int main( ) {//创建第一个对象 cout<<"I have the first object.\n"; SINGLE_INSTANCE obj1; //创建第一个对象 cout<<"I have the second object.\n"; SINGLE_INSTANCE obj2; //结束程序 cout<<"End of demonstration.\n"; return 0; }
♦ 3)归纳以上分析可得到以下程序: #include<iostream.h> #include<iomanip.h> const float AMTINT=300.0; //初始化油桶中的油量 const float PRICE=1.25; //单价 class Pump { protected: float amtint, price; public: Pump(float, float); //构造函数 void values( ); void request( float); };
♦ 根据程序功能分析,应得到如下执行结果:
The gas tank has: 300.0 The price per kg of gas is: 1.25 30.0 kg were requested 30.0 kg were pumped 270.0 kg remain in the tank The total price is: 337.50
void Pump::request(float pumpamt) { float pumped; if(amtint>=pumpamt) pumped=pumpamt; else pumped=amtint; amtint=amtint-pmped; cout<<pumpamt<<“kg were requested”<<endl; cout<<pumped<<“kg were pumped<<endl; cout<<amtint<<“kg remain in the tank”<<endl; cout<<“The total price is :”<<(pumped*price)<<endl; }
上一节
返回
下一节
void main( ) { Pump obj(AMTINT,PRICE); obj,values( ); cout<<endl; obj.request(30.0); cout<<endl; obj.request(280.0); }
根据程序功能分析,应该得到如下执行结果: 根据程序功能分析,应该得到如下执行结果: The gas tank has:300.0 The price per kg of gas is :1.25 30.0 kg were requested 30.0 kg were pumped 270.0 kg remain in the tank The total price is : 37.5 28.0 kg were requested 27.0 kg were pumped 0 kg remain in the tank The total price is : 337.5
相关主题