版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025青岛上合控股发展集团有限公司社会公开招聘(31人)笔试备考重点题库及答案解析
- 2025陕西西安市西北工业大学自动化学院智能感知技术团队招聘3人笔试备考重点试题及答案解析
- 2025湖北神农架林区实验初级中学食堂员工招聘1人模拟笔试试题及答案解析
- 2025首都医科大学附属北京安贞医院科技处科研管理人才招聘2人模拟笔试试题及答案解析
- 南充市经济合作和外事局下属事业单位2025年第二批引进高层次人才公开考核招聘(6人)备考考试试题及答案解析
- 2025西藏昌都市第二批市直单位遴选(招聘)公务员(工作人员)64人备考考试试题及答案解析
- 2025浙江丽水遂昌县云峰街道招聘专职消防队员2人备考考试题库及答案解析
- 招商银行春招笔试题库及答案
- 2026年玉溪市元江县教育体育系统招聘高中学校教师校园招聘(9人)备考考试题库及答案解析
- 2026湖南绥宁花园阁国家湿地公园管理处公益性岗位人员招聘9人模拟笔试试题及答案解析
- 阿特拉斯空压机-培训资料
- 2024年江苏省海洋知识竞赛备考试题库(含答案)
- 高一语文经典古代诗词赏析
- 协助扣划存款通知书
- 自动控制原理课程设计报告恒温箱
- 江西d照驾驶员理论考试
- GB/T 30340-2013机动车驾驶员培训机构资格条件
- GB/T 19215.1-2003电气安装用电缆槽管系统第1部分:通用要求
- GB/T 13298-2015金属显微组织检验方法
- 滴滴打车用户出行习惯报告
- 保密管理-保密教育培训签到簿
评论
0/150
提交评论