




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C++程序设计基础王海涛汇文教育2011.07Time类实例研究//Time.h#ifndefTIME_H#defineTIME_HclassTime{public:Time();
voidsetTime(int,int,int);voidprintUniversal();voidprintStandard();private:inthour;//0-23(24-hourclockformat)intminute;//0-59intsecond;//0-59};#endif//
Time.cpp#include<iostream>#include<iomanip>#include"Time.h"usingnamespacestd;Time::Time(){
hour=minute=second=0;}voidTime::setTime(inth,intm,ints){
hour=(h>=0&&h<24)?h:0;
minute=(m>=0&&m<60)?m:0;second=(s>=0&&s<60)?s:0;}voidTime::printUniversal(){cout<<setfill('0')<<setw(2)<<hour<<":"
<<setw(2)<<minute<<":"<<setw(2)<<second;}voidTime::printStandard(){
cout<<((hour==0||hour==12)?12:hour%12)<<":"<<setfill('0')<<setw(2)<<minute<<":"<<setw(2)<<second<<(hour<12?"AM":"PM");}预处理器封套:阻止重复定义类的作用域和类成员的访问#include<iostream>usingnamespacestd;classCount{public:voidsetX(intvalue)
{
x=value;
}voidprint()
{
cout<<x<<endl;
}private:intx;};类的作用域和类成员的访问intmain(){Countcounter;Count*counterPtr=&counter;Count&counterRef=counter;cout<<"Setxto1andprintusingtheobject'sname:";counter.setX(1);
counter.print();cout<<"Setxto2andprintusingareferencetoanobject:";counterRef.setX(2);
counterRef.print();cout<<"Setxto3andprintusingapointertoanobject:";counterPtr->setX(3);//setdatamemberxto3counterPtr->print();//callmemberfunctionprint}//endmain类的成员函数可以重载,但只能由类的其它成员函数重载//SalesPerson.cpp#include<iostream>#include<iomanip>#include"SalesPerson.h"//includeSalesPersonclassdefinitionusingnamespacestd;SalesPerson::SalesPerson(){for(inti=0;i<monthsPerYear;i++)sales[i]=0.0;}//endSalesPersonconstructorvoidSalesPerson::getSalesFromUser(){doublesalesFigure;
for(inti=1;i<=monthsPerYear;i++)
{cout<<"Entersalesamountformonth"<<i<<":";cin>>salesFigure;setSales(i,salesFigure);}//endfor}//endfunctiongetSalesFromUser#include"SalesPerson.h"intmain(){SalesPersons;//createSalesPersonobjects
s.getSalesFromUser();//notesimplesequentialcode;thereares.printAnnualSales();//nocontrolstatementsinmain}//endmain默认实参的构造函数//Fig.9.8:Time.h#ifndefTIME_H#defineTIME_HclassTime{public:Time(int=0,int=0,int=0);//defaultconstructorvoidsetTime(int,int,int);//sethour,minute,secondvoidsetHour(int);//sethour(aftervalidation)voidsetMinute(int);//setminute(aftervalidation)voidsetSecond(int);//setsecond(aftervalidation)intgetHour();//returnhourintgetMinute();//returnminuteintgetSecond();//returnsecond
voidprintUniversal();//outputtimeinuniversal-timeformatvoidprintStandard();//outputtimeinstandard-timeformatprivate:inthour;//0-23(24-hourclockformat)intminute;//0-59intsecond;//0-59};//endclassTime#endif//Fig.9.9:Time.cpp#include<iostream>#include<iomanip>#include"Time.h"//includedefinitionofclassTimefromTime.husingnamespacestd;Time::Time(inthr,intmin,intsec){setTime(hr,min,sec);//validateandsettime}//endTimeconstructorvoidTime::setTime(inth,intm,ints){setHour(h);//setprivatefieldhoursetMinute(m);//setprivatefieldminutesetSecond(s);//setprivatefieldsecond}//endfunctionsetTimevoidTime::setHour(inth){hour=(h>=0&&h<24)?h:0;//validatehour}//endfunctionsetHourvoidTime::setMinute(intm){minute=(m>=0&&m<60)?m:0;//validateminute}//endfunctionsetMinutevoidTime::setSecond(ints){second=(s>=0&&s<60)?s:0;//validatesecond}//endfunctionsetSecondintTime::getHour(){returnhour;}//endfunctiongetHourintTime::getMinute(){returnminute;}//endfunctiongetMinuteintTime::getSecond(){returnsecond;}//endfunctiongetSecondvoidTime::printUniversal(){cout<<setfill('0')<<setw(2)<<getHour()<<":"<<setw(2)<<getMinute()<<":"<<setw(2)<<getSecond();}//endfunctionprintUniversalvoidTime::printStandard(){cout<<((getHour()==0||getHour()==12)?12:getHour()%12)<<":"<<setfill('0')<<setw(2)<<getMinute()<<":"<<setw(2)<<getSecond()<<(hour<12?"AM":"PM");}#include<iostream>#include"Time.h"usingnamespacestd;intmain(){
Timet1;//allargumentsdefaultedTimet2(2);//hourspecified;minuteandseconddefaultedTimet3(21,34);//hourandminutespecified;seconddefaultedTimet4(12,25,42);//hour,minuteandsecondspecifiedTimet5(27,74,99);//allbadvaluesspecifiedcout<<"Constructedwith:\n\nt1:allargumentsdefaulted\n";t1.printUniversal();//00:00:00cout<<"\n";t1.printStandard();//12:00:00AMcout<<"\n\nt2:hourspecified;minuteandseconddefaulted\n";t2.printUniversal();//02:00:00cout<<"\n";t2.printStandard();//2:00:00AM构造函数、析构函数的调用//
CreateAndDestroy.h#include<string>usingnamespacestd;#ifndefCREATE_H#defineCREATE_HclassCreateAndDestroy{public:CreateAndDestroy(int,string);//constructor~CreateAndDestroy();//destructorprivate:intobjectID;//IDnumberforobjectstringmessage;//messagedescribingobject};//endclassCreateAndDestroy#endif//Fig.9.12:CreateA
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 保育老师健康知识培训
- 项目工程应急演练课件
- 《平面设计》课件-第6章 设计符号学基础
- 音乐信息技术课件
- 市政污水管网改造项目建设管理方案(模板范文)
- 城镇污水管网建设工程运营管理方案(模板范文)
- xx片区城乡供水一体化项目规划设计方案(范文参考)
- 2025年氯铂酸合作协议书
- 基于风险指标的低压设备退役优化及其在新加坡电网中的应用
- 2025年专用小麦新品种项目合作计划书
- LS/T 3240-2012汤圆用水磨白糯米粉
- GB/T 15298-1994电子设备用电位器第一部分:总规范
- 泥水平衡盾构简介课件
- 新教科版六下科学4-6《生命体中的化学变化》教案
- 2023高中学业水平合格性考试历史重点知识点归纳总结(复习必背)
- 自然指数NatureIndex(NI)收录的68种自然科学类期刊
- 手术报告审批单
- 《专业导论光电信息科学与工程》教学大纲
- 广东省湛江市各县区乡镇行政村村庄村名明细
- 煤矿智能化综采工作面系统运行维护管理制度
- 少儿美术国画- 少儿希望 《紫藤课件》
评论
0/150
提交评论