版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026四川雅安市芦山县招聘县属国有企业总经理1人笔试参考题库及答案解析
- 契约执行效果优化保证承诺书(5篇)
- 2026年北京达特集成技术有限责任公司招聘10人考试备考题库及答案解析
- 2026年天津市河西区教育系统招聘290人笔试备考题库及答案解析
- 2026北京中国人民大学新闻学院招聘3人笔试备考题库及答案解析
- 2026江苏南京六合经济开发区所属国有企业招聘17人笔试模拟试题及答案解析
- 2026年度东营市市属事业单位公开招聘工作人员(75人)笔试模拟试题及答案解析
- 《九年级物理电磁感应现象讲解》
- 数据分析报告制作及分享工具
- 成长中的我演讲稿关于成长话题5篇
- 食品安全管理制度打印版
- 多联机安装施工方案
- 煤矿副斜井维修安全技术措施
- 公共视频监控系统运营维护要求
- 河南省职工养老保险参保人员关键信息变更核准表
- 四川大学宣传介绍PPT
- 小学数学人教版六年级上册全册电子教案
- 液氨储罐区风险评估与安全设计
- 阿司匹林在一级预防中应用回顾
- 2023年福海县政务中心综合窗口人员招聘笔试模拟试题及答案解析
- GB/T 4103.10-2000铅及铅合金化学分析方法银量的测定
评论
0/150
提交评论