资源描述:
《阅读程序分析结果(第5章)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、cout«Hx=,,«x«,y=H«y«endl;阅读程序分析结果程序一#includeclassTDate{public:voidSetDate(inty.intmjntd);voidPrint();private:intyear,month,day;};voidTDate::SetDate(inty.intm.intd){year=y;month=m;day=d;}voidTDate::Print(){cout«year«H.n«month«H.n«day«endl;}voidmain(){TDatedatel,date2;datel.SetDate(2008,
2、2,26);date2.SetDate(2001,5,19);datel.Print();程序二#includeclassSample{intx,y;public:voidSample1(){x=y=0;)voidSample1(intauntb){x=a;y=b;}voiddisp()};voidmain(){Samplesl,s2;si.Sample1();s2.Sample1(1,2);sl.dispO;s2.disp();}程序三#includeclasspoint{private:intx,y;public:voidsetpoint(
3、intxx,intyy){X二XX;y=yy;}intgetx(){returnx;}intgety(){returny;)};voidmain(){pointopl,op2;inti,j;opl.setpoint(l,2);op2.setpoint(3,4);i=opl.getx();j=opl.gety();cout«Mopli=H«i«H,oplj=u«j«endl;i=op2.getx();j=op2.gety();cout«Mop2i=,,«i<<,op2j=n«j«endl;程序四#include#include"tdate.h"voidmain(){T
4、Datedatel,date2;datel.SetDate(l996,5,4);date2.SetDate(1979,6,6);intleap=datel.IsleapYear();cout«leap«endl;dalel.Print();date2.Print();}classTDate{public:voidSetDate(intyjntm,intd);intIsleapYear();voidPrint();private:intyear,month,day;};voidTDate::SetDate(inty,intm,intd){yeai-y;month=m;day=d;}intTDa
5、te::IsleapYear(){return(year%4==0&&year%100!=0)
6、
7、(year%4()0=0);}voidTDate::Print(){cout«year«n.n«month«n.M«day«endl;}程序五#includeclasspair{charcl,c2;public:voidset(charb){cl=I+(c2=b);}voidprint(){cout«c1;)};voidmain(){paira,b;a.set(A);b.set('B');a.print();b.print();}程序六#include8、h>classLocal{intX;public:voidini(intx){X=x;}intval(){returnX;}1;voidmain(){Localss[10];inti=0,s=0;for(i=0;i<10;i++)ss[i].ini(i);for(i=0;i<10;i++)s+=ss[i].val();cout«s;}程序七#includeclassTDate1{public:TDate1(intyjntm,intd);-TDate1();voidprint();private:intyear,month,day;};TDate1::TDate1(in
9、tytintmjntd)year=y;month=m;day=d;cout«HConstructorcalledAn";}TDatel::~TDatel(){cout«MDestructorcalled.H;}voidTDatel::print(){cout«year«,,.H«month«,,.,,«day«endl;}voidmain(){TDate1today(1998,4,9),tomorrow(