版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验十一类与对象.参照实验案例,定义Date日期类。其数据成员包括年year,月month,日day。定义构造函数、析构函数。构造函数要对初始化数据进行合法性检查。(年份4位数,月份1-12,日期根据年份、月份判断其合法范围)。成员函数有调整日期,显示日期(“****年・・月・・日”)。〃程序设计如下:#include<iostream.h>#include<fstream.h>classDate{intyear,month,day;staticconstintdays[2][13];boolLeapYear();public:Date(int=1900,int=1,int=1);~Date(){};voidSetDate(int,int,int);voidShowDate();};constintDate::days[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31));Date::Date(inty,intm,intd){SetDate(y,m,d);)voidDate::SetDate(inty,intm,intd){year=(y>=1900&&y<=9999)?y:1900;month=(m>=l&&m<=12)?m:l;day=(d>=l&&d<=days[LeapYear()][month])?d:1;}voidDate::ShowDate(){coutvvyearv〈"年"vvmonthvv"月"vvdayvv"日"v<endl;}boolDate::LeapYear(){return((year%400=0)ll(year%4==0&&year%100!=0))?true:false;)intmain(){Datedl(l899,13,32)02(20001,4),d3(2004,2,29),d4;dl.ShowDate();d2.ShowDate();d3.ShowDate();d4.ShowDate();d3.SetDate(2011,2,29);d3.ShowDate();return0;).在第1题基础上,为Date类定义日期增减函数Add(int),Reduce(血),其中参数为增减的天数。#include<iostream.h>#include<fstream.h>classDate{intyear,month,day;staticconstintdays[2][13];boolLeapYear();public:Date(int=1900,int=l,int=1);〜Dale(乂};voidSetDate(int,int,int);voidShowDate();voidAdd(int);voidReduce(int);boolEndofMonth();};constintDate::days[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31});Date::Date(inty,intm,intd){SetDate(y,m,d);)voidDate::SetDate(inty,intm,intd){year=(y>=1900&&y<=9999)?y:1900;month=(m>=l&&m<=12)?m:l;day=(d>=1&&d<=days[LeapYear()][month])?d:1;}voidDate::ShowDate(){couivvyearvv"年"vvmonthvv”月,,«day«,*Hn«endl;}boolDate::LeapYear(){return((year%400==0)ll(year%4==0&&year%100!=0))?true:false;)boolDate::EndofMonth(){returnday==daysILeapYear()][month];)voidDate::Add(intn){for(inti=0;i<n;i++)if(EndofMonth()&&month==12){year++;month=day=1;)elseif(EndofMonth()){month++;day=1;)elseday++;}voidDate::Reduce(intn){for(inti=0;i<n;i++)if(day==l){if(month==l){year—;month=12;day=31;}elseday=days[LeapYear()][-month];)elseday--;intmain(){Datedl(2009,12,23)42(2007,2,27),d3(2008,2,27),d4(2011,1,4);dl.ShowDate();d2.ShowDate();d3.ShowDate();d4.ShowDate();dl.Add(lO);d2.Add(5);d3.Add(5);d4.Reduce(10);dl.ShowDate();d2.ShowDate();d3.ShowDate();d4.ShowDate();return0;)3.建立一个分数类Fraction。其数据成员包括分子nume和分母deno。成员函数包括构造函数、析构函数。构造函数要对初始化数据进行必要的检查(分母不能为〇)。成员函数包括约分、通分、加、减、乘、除、求倒数、比较大小、显示和数据输入等,显示函数以“a/b”形式显示分数。完成所有成员函数的定义并在主函数中进行检验。程序设计如下:定义ー个Fraction类classFraction)private: 〃这句语句可以省去intnume;intdeno;public:Fraction(inti=l,intn=0){ 〃复制构造函数if(i!=0){nume=i;deno=n;)elsenume=l;deno=0;voidyuefen();FractionaddFraction(Fractiont);FractioncutFraction(Fractiont);FractionchengfaFraction(Fractiont);FractionchufaFraction(Fractiont);voiddaoshu();voidshow();};c.cpp#include<iostream>#include<cmath>#include',Fraction.h,,usingnamespacestd;voidFraction::yuefen(){intr,i=abs(nume),j=abs(deno);while(j){r=i%j;i=j;j=r;)nume/=i;deno/=i;}voidFraction::addFraction(Fractiont){inti=deno;deno=deno*t.deno;nume=nume*t.deno+t.nume*i;)voidFraction::cutFraction(Fractiont){inti=deno;deno=deno*t.deno;nume=nume*t.deno-t.nume*i;voidFraction::chengfaFraction(Fractiont){deno=deno*t.deno;nume=nume*t.nume;)voidFraction::chufaFraction(Fractiont){deno=deno*t.nume;nume=nume*t.deno;)voidFraction::daoshu(){inti;i=nume;nume=deno;deno=i;)voidFraction::show(){cout«nume«,7,'«deno«endl;main.cpp#include<iostream>#include"Fraction.h,,usingnamespacestd;intmain(){intn,d,ne,de;coutvv"请输入分母n=";cin»n;coutvv"请输入分子d=";cin»d;Fractiont(n,d);cout«nt=H;t.show();coutvv"请输入分母ne二”;cin»ne;coutvv”请输入分子de=";cin»de;Fractioni(ne,de);cout«Mi=K;i.yuefenQ;i.show();coutvぐi+l=";i.addFraction(t);i.yuefen();i.show();i.cutFraction(t);i.yuefen();〃这里的I是已计算过的i.show();i.chengfaFraction(t);cout«ni*t=H;i.yuefen();i.show();cout«"t倒数是";t.yuefen();t.daoshu();t.show();return0;)实验十二类的复制构造函数1.定义矩形类Rectangle,该类有left、top、right、bottom四个数据成员函数等。在主函数中建立Rectangle对象加以验证。〃定义头文件classRectangle{private:doubletop;doublebottom;doubleleft;doubleright;public:Rectangle(doublei,doublej);Rectangle(Rectangle&);voidshow();,定义矩形类的构造函数、复制构造函数、读写数据成员函数、计算面积doublesize();};//c.cpp文件#include<iostream>#include"Rectangle.hMusingnamespacestd;Rectangle::Rectangle(doublei,doublej){top=bottom=i;left=right=j;}Rectangle::Rectangle(Rectangle&t){left=right=t.left;top=bottom=t.top;)voidRectangle::show(){coutvv”矩形的边为:"vvendl;cout«',top=,'«top«endl«,,bottom=',«bottom«eindl«,'left=M«left«endl«,'right="«right«endl;doubleRectangle::size(){doubles=top*left;returns;)min.cpp文件#include<iostream>#includeMRectangle.hHusingnamespacestd;intmain(){coutvv”请输入矩形的四条边"vvendl;doublet;double1;cin»t»1;RectangleT(t,l);T.show();RectangleB(T);coutvv”矩形的面积为:"v<T.size()vvendl;return0;.为实验H••ー实验内容第1题的Date类定义复制构造函数。设计fun函数。在主函数中设计相应的语句调用fun函数,理解在调用该函数过程中复制构造函数被3次调用的过程。Datefun(Dated){Dated1=d;dl.add(4);returnd1;)代码部分如下://Date.h文件〃定义一〃定义一个Date类protected:intYear;intMonth;intDay;public:Date(Date&d){Year=d.Year;Month=d.Month;Day=d.Day;)Date(intyear=1000,intmonth=l,intday=l); //Date的构造函数~Date(乂}; //Date的析构函数voidSetDate(intyeanintmonth,intday); 〃设置时间函数voidShowDate(); 〃显示时间函数intInspectY(intm,intn); 〃判断year是否符合要求intInspectM(intm,intn); 〃判断month是否符合要求intInspectD(intyear,intmonth,intday); 〃判断I!期voidAdd(intn);voidReduce(intn);voidchoice(intn);friendDatefun(Dated););//c.cpp文件#include<iostream>#includeMDate.h"usingnamespacestd;inta1u={31,28,31,30,31,30,31,31,30,31,30,31);inta2[]={31,29,31,30,31,30,31,31,30,31,30,31);int*p;Date::Date(intyear,intmonth,intday){inta=1000,b=12;Year=InspectY(year,a);Month=InspectM(month,b);Day=InspectD(year,month,day);}voidDate::SelDate(intyear,intmonth,intday){inta=1000,b=12;Year=InspectY(year,a);Month=InspectM(month,b);Day=InspectD(year,month,day);}intDate::InspectM(intm,intn){elsereturnm;intDate:InspectY(intmjntn){if(m<1000)return1000;elsereturnm;)intDate::InspectD(intyear,intmonth,intday){if((year%400==0)ll((year%4==0)&&(year%100!ニ0))){if(month==2){if(day>29llday<=0)return1;elsereturnday;))elseif(month=2){if(day>=29llday<=0)return1;elsereturnday;if(month==4llmonth==6llmonth==9llmonth=11){if(day>30llday<=0)return1;elsereturnday;}else{if(day>3lllday<=0)return1;elsereturnday;}}voidDate::ShowDate()Icout«Year«"^"«Month<<"月"«Day«"0";}voidDate::choice(intn){if((n%400==0)ll((n%4==0)&&(n%100!=0)))P=a2;elsep=al;IvoidDate::Add(intn){choice(Year);n=n+Day-p[Month-1];for(inti=Month;n>0;i++){if(i%12==0){Year++;choice(Year);)n=n-p[i%12];)if(i%12==0)Month=12;elseMonth=i%12;Day=n+p[(i-1)%12];voidDate::Reduce(intn){choice(Year);n=n-Day;Month—;while(n>=0){if(Month==0){Month=12;Year—;choice(Year);)n=n-p[Month-l];Month—;}Month++;Day=-n;}Datefun(Dated){Datedl=d;dl.Add(4);returnd1;}//main.cpp文件#include<iostream>#includenDate.h"usingnamespacestd;intmain(){intyear,month,day,n;coutvv”输入年份Year=M;cin»year;coutvv”输入月份Monthゴ;cin»month;coutvv”输入日期Dayゴ;cin»day;Datedate(year,month,day),datel;date.ShowDate();cout«endl;coutcv”调用fun函数”vvendl;datel=fun(date);dateLShowDate();cout«endl;return0;).定义组合类DateTime,类中的成员有实验十一中的Date类和Time类的成员。定义DateTime的构造函数和复制构造函数。并在主函数中实例化DateTime类的对象。//datetime.h文件classDate{ 〃定义ー个Date类protected:intYear;intMonth;intDay;public:Date(intyear,intmonth,intday); //Date的构造函数〜Date(乂}; //Date的析构函数Date(Date&date);voidShowDale。;〃显示时间函数intInspectY(intm,intn); 〃判断year是否符合要求intInspectM(intm,intn); 〃判断month是否符合要求intInspectD(intyear,intmonth,intday); 〃判断日期friendclassDateTime;);classTime{intHour;intMinte;intSecond;public:Time(inth,intm,ints);Time(Time&T);intchoicehour(inth);intchoicetime(intm);voidShowTime();friendclassDateTime;);classDateTime{DateD;TimeT;public:DateTime(Dated,Timet):D(d),T(t){}DateTime(DateTime&DT):D(DT.D),T(DT.T){};voidShowDateTime(););//c.cpp文件#include<iostream>#include,'Datelime.h,'usingnamespacestd;Date::Date(intyear,intmonth,intday){inta=1000,b=12;Year=InspectY(year,a);Month=InspectM(month,b);Day=InspectD(year,month,day);intDate::InspectM(intm,intn){if(m>n)return1;elsereturnm;)intDate::InspectY(intm,intn){if(m<1000)return1000;elsereturnm;}intDate::InspectD(intyear,intmonth,intday){if((year%400==0)ll((year%4==0)&&(year%100!=0))){if(month==2){if(day>29llday<=0)return1;elsereturnday;)elseif(month==2){if(day>=29llday<=0)return1;elsereturnday;)if(month==4llmonth==6llmonth==9llmonth==lif(day>30llday<=0)return1;elsereturnday;)else{if(day>31llday<=0)return1;elsereturnday;})Date::Date(Date&date){Year=date.Year;Month=date.Month;1)(Day=date.Day;)voidDate::ShowDate(){coulvvYearvv“年“vvMonlhvv"月”vvDayvv”日)Time::Time(inth,intm,ints){Hour=choicehour(h);Minte=choicetime(m);Second=choicetime(s);}Time::Time(Time&T){Hour=T.Hour;Minte=T.Minte;Second=T.Second;)intTime::choicehour(inth){if(h<=0llh>24){couivv”输入有误,强制至为「‘vvendl;return1;)elsereturnh;)intTime::choicetime(intm){if(m<0llm>60){coutvv”输入有误,强制至为ド<vendl;return1;}elsereturnm;)voidTime::ShowTime(){cout«Hour«n:"«Minte«H:"«Second«endl;)voidDateTime::ShowDateTime(){D.ShowDate();T.ShowTime();//main.cpp文件#include<iostream>#include,'DateTime.h"usingnamespacestd;intmain(){inty,mn,d,h,m,s;coutvv”请输入年ア;cin»y;coutvv”请输入月:“;cin»mn;coutvv”请输入日:”;cin»d;Dateda(y,mn,d);cout«”输入时间Hourゴ;cin»h;coutvv”输入时间Minte=";cin»m;coulvv”输入时间second=";cin»s;Timet(h,m,s);DateTimeDT(da,t);DT.ShowDateTime();return0;).改写案例2,要求如下:定义和案例2相同的点类Points三角形类Triangle定义如下:classTriangle{public:Triangle(Point,Point,Point);Triangle(Triangle&);doubleGetArea();private:Pointpl,p2,p3;doublearea;完成Triangle类的成员函数的定义,并在主函数中定义3个Point对象,〃定义一个point类和Triangle类classPoint{protected:intX,Y;public:Point(intx=0,inty=0){X=x;Y=y;)Point(Point&P){X=P.X;Y=P.Y;)friendclassTriangle;voidShowpoint(););classTriangle{1个Triangle对象,计算三角形的面枳。public:Triangle(Point,Point,Point);Triangle(Triangle&);doubleGetArea();doublele(Pointa,Pointb);private:Pointpl,p2,p3;doublearea;};//c.cpp文件#include<iostream>#include"point.h"#include<cmath>usingnamespacestd;voidPoint::Showpoint()(cout«M(n«X«,'.H«Y«,,),,«endl;}Triangle::Triangle(Pointa,Pointb,Pointc):p1(a),p2(b),p3(c){double11,12,13;doubleav;ll=le(pl,p2);12=le(p2,p3);13=le(pl,p3);av=(l1+12+13)/2.0;area=sqrt(av*(av-l1)*(av-12)*(av-13));)Triangle::Triangle(Triangle&t):p1(t.p1),p2(t.p2),p3(t.p3){double11,12,13;doubleav;H=le(pl,p2);12=le(p2,p3);13=le(pl,p3);av=(l1+12+13)/2.0;area=sqrt(av*(av-l1)*(av-12)*(av-13));}doubleTriangle::le(Pointa,Pointb){doubledeltx=abs(a.X-b.X);doubledelty=abs(a.Y-b.Y);doublelength=sqrt(deltx*deltx+delty*delty);returnlength;)doubleTriangle::GetArea(){returnarea;)#include<iostream>#include,'point.hnusingnamespacestd;//main.cpp文件intmain(){inta,b,c,d,e,f;coutvv”请输入六个整数"vvendl;cin»a»b»c»d»e»f;PointpeI(a,b),pe2(c,d),pe3(e,f),pe4(pe3);TriangleT(pel,pe2,pe3),Tl(peI,pe2,pe4);coutvv”三角形T的面积为:"«T.GetArea();cout«endl;coutv〈”三角形T1的面积为:"vvTl.GetArea。;cout«endl;return0;}・实验十三类的深复制.为实验案例中的A类定义赋值运算符的重载函数,完成对象的赋值运算。程序设计如下:〃这里的赋值是发生在调用fun函数时#include<iostream>usingnamespacestd;classA{int*p;public:A(intn){inti二n;p=newint(i);)A(A&ra){p=newint(*ra.p);}〜A(){deletep;}intGetValue(){return*p;}A&operator=(A&ra){if(ra.p)p=ra.p;elsep二NULL;return*this;));intfun(Aoa){returnoa.GetValue();//new函数用于动态申请空间,并初始化〃复制构造函数完成对象的深复制〃析构函数用于释放内存空间〃赋值运算符“ゴ’的重载voidmain(){inti;coutくく”请输入一•个整数1=";cin>>i;Aa(i);cout«fun(a)<<endl;).设计ー个学生类(Student)。数据成员包括:身份证号(Id),姓名(Name),性别(Gender),生日(Birthday)和家庭住址(pHA)。考虑到表示每个学生家庭住址的字符长度不一,所以pHA为指向学生家庭住址的字符型指针,家庭住址的内存由new运算动态申请。要求编写:构造函数、完成深复制的复制构造函数、释放动态内存的析构函数、以及学生数据输入和显示函数。在主函数中建立Student类对象并调用成员函数。程序设计如下:〃这部分是在ー个cpp文件里完成,当然也可以分成一个头文件,和两个cpp文件#include<iostream>usingnamespacestd;classstudent〃定义私有成员char*pld;char*pName;char*pGender;char*pBirthday;char*pPHA;〃定义私有成员public:〃定义公有成员public:student();〃定义无参构造函数相当于默认的构造函数student(char*pname,char*pid,char*pgender,char*pbirthday,char*ppHA);〃定义含参构造函数〃定义复制构造函数〃定义析构函数〃定义复制构造函数〃定义析构函数〃重载”ゴ运算符-student();student&operator=(student&s);voidshowstudent();};student::student()(〃初始化全赋值为空pName=NULL;〃初始化全赋值为空pId=NULL;pGender=NULL;pBirthday=NULL;pPHA二NULL;)student::student(char*pname,char*pid,char*pgender,char*pbirthday,char*ppHA)if(pName=newcharlstrlen(pname)+1J)strcpy(pName,pname);if(pld=newchar[strlen(pid)+l])strcpy(pld,pid);if(pGender=newchar[strlen(pgender)+1])strcpy(pGender,pgender);if(pBirthday=newchar[strlen(pbirthday)+1])strcpy(pBirthday,pbirthday);if(pPHA=newchar[strlen(ppHA)+1])strcpy(pPHA,ppHA);)student::-student(){if(pName)pName[O]='\O';delete[]pName;if(pld)pId[O]=\O,;deletef]pld;〃利用new函数动态申请空间〃利用delete函数释放内存空间if(pGender)pGender[O]=AO';delete[]pGender;if(pBirthday)pBirthday[〇]='\〇';delete[]pBirthday;if(pPHA)pPHA⑼=ヘ〇’;delete[]pPHA;)student::student(student&s)(if(s.pName){if(pName=newchar[strlen(s.pName)+l])strcpy(pName,s.pName);)elsepName=NULL;if(s.pld){if(pld=newchar[strlen(s.pld)+l])strcpy(pld,s.pld);)elsepId=NULL;if(s.pGender){if(pGender=newchar[strlen(s.pGender)+1])strcpy(pGender,s.pGender);}elsepGender=NULL;if(s.pBirthday){if(pBirthday=newchar[strlen(s.pBirthday)+1])strcpy(pBirthday,s.pBirthday);}elsepBirthday=NULL;if(s.pPHA){if(pPHA=newchar[strlen(s.pPHA)+1J)strcpy(pPHA,s.pPHA);)elsepPHA=NULL;)student&student::operator=(student&s){deletef]pName;if(s.pName){if(pName=newchar[strlen(s.pName)+1])strcpy(pName,s.pName);}elsepName=NULL;deletedpld;if(s.pld){if(pld=newchar[strlen(s.pld)+l])strcpy(pld,s.pld);)elsepId=NULL;delete[]pGender;if(s.pGender){if(pGender=newchar[strlen(s.pGender)+l])strcpy(pGender,s.pGender);}elsepGender=NULL;deletedpBirthday;if(s.pBirthday){if(pBirthday=newchar[strlen(s.pBirthday)+1])strcpy(pBirthday,s.pBirthday);}elsepBirthday=NULL;deletedpPHA;if(s.pPHA){if(pPHA=newchar[strlen(s.pPHA)+1])strcpy(pPHA,s.pPHA);)elsepPHA=NULL;return*this;Ivoidstudent::showstudent(){cout«pName«\t'«pId«,\t,«pGender«V«pBirthday«V,«pPHA«endl;)intmain() 〃主函数(studentsi(“张三”,”09080ド,“男”,”90.11.0ド,“南京市”);sl.showstudent();students2(”李四”,”090802”,”女”,”90.12.0ド,”常州市”),s3;s2.showstudent();cout«“"«endl;students4=s1;s4.showstudent();s3=s2;s3.showstudent();cout«endl;return0;)实验十四友元I.将实验十二实验内容第1题Rectangle类中计算面积的函数定义为友元函数,程序设计如下:#include<iostream>usingnamespacestd;classRectangle{ 〃定义Rectangle类private:doubletop;doublebottom;doubleleft;doubleright;public:并在主函数中调用该友元函数计算Rectangle对象的面枳。Rectangle(doublei,doublej);Rectangle(Rectangle&);voidshow();frienddoublesize(Rectangle&); 〃定义完成面积计算的友元函数};Rectangle::Rectangle(doublei,doublej){top=bottom=i;left=right=j;}Rectangle::Rectangle(Rectangle&t){left=right=t.left;top=bottom=tlop;)voidRectangle::show(){cout<v”矩形的边为:“vvendl;cout«ntop=n«top«endl«"bottom=n«bottom«endl«,'left=',«left«endl«,'right=n«right«endl:doublesize(Rectangle&r){doubles=r.top*r.left;returns;)intmain(){coutvv”请输入矩形的两组对边值:“vvendl;doublet;double1;cin»t»l;RectangleT(t,l);T.show();coutvv”矩形的面积为:“vvsize(T)vvendl; 〃调用友元函数return0;)2.参考案例2,定义Time类的友元类DiffTime。其中DiffTime类有一个成员函数Time_DiffTime(Time&,start,Time&end)»计算两个Time对象的时间差。程序设计如下:#include<iostream>
usingnamespacestd;classTime{private:intHounMinute,Second;public:Time();Time(intNewH,intNewM,intNewS);voidSetTime(int,int,int);voidShowTime();〜Time(乂};friendclassDiffTime;};Time::Time(){SetTime(0,0,0);}Time::Time(intNewH,intNewM,intNewS){SetTime(NewH,NewM,NewS);〃时间类的定义〃时间类的定义〃私有数据成员〃外部接口,公有成员函数〃默认构造函数〃构造函数〃设置时间的函数〃显示时间的函数〃析构函数〃定义Time类的友元类DiffTime类Hour=NewH<0IINewH>23?0:NewH;Minute=NewM<0IINewM>59?0:NewM;Second=NewS<0IINewS>59?0:NewS;}voidTime::ShowTime(){cout«Hour«n:n«Minute«n:n«Second«endl;}classDiffTime{ //Time友元类DifiTime的定义public:TimeTime_DiffTime(Time&start,Time&end); 〃完成时间差的计算};TimeDiffTime::Time_Diffnme(Time&start,Time&end){intth,tm,ts;intcarry=0;Timett;(ts=end.Second-start.Second)>0?carry=0:ts+=60,carry=l;(tm=end.Minute-start.Minute-carry)>O?carry=O:tm+=60,carry=l;(th=end.Hour-start.Hour-60-carry)>0?carry=0:th+=24;tt.SetTime(th,tm,ts);returntt;voidmain(){Timetl(8,10,10)42(3,20,4);tl.ShowTime();t2.ShowTime();Timet; 〃该变量用于表示t!和t2的时间差DiffTimet3;t=t3.Time_DiffTime(tl,t2);t.ShowTime();・实验十五函数模板与类模板1.编写ー个求绝对值的函数模板,并在主函数中用int、double等类型的数据测试。程序代码如下:#include<iostream>#include<cmath>usingnamespacestd;template<typenameT>Tmas(Tn){returnfabs(n);}intmain(){inta;doubleb;coutvv”输入ー个整数a二”;cin»a;coutvv”输入ー个数b=M;cin»b;cout«Hlal=u«mas<int>(a)«endl;cout«"lbl=n«mas<double>(b)«endl;return0;〃是fabs的头文件//fabs是求浮点型的整数的函数3.设计ー个函数模板sort(),完成对数组Ta[n]的排序。程序代码如下:#include<iostream>#include<iomanip>usingnamespacestd;template<typenameT>voidsort(Ta[],intn){Ttemp;intij;for(i=0;i<n-l;i++)for(j=0;j<n-i-1;j++)if(a|j]>aU+l]){temp=a[j];a[jj=a[j+l];a[j+l]=temp;})intmain(){intn;〃定义一个函数模板〃采用冒泡排序coutvv”输入数组长度n=";cin»n;int*p=newint[n];for(inti=0;i<n;i++){coutvv”请输入第"vvi+kv”个数a[,,«i«,']=M;cin»p[i];}sort<int>(p,n);for(i=0;i<n;i++){cout«setw(5)«p[i]«setw(5);if((i+1)%4==0)cout«endl; 〃保证每行输出4个元素)return0;)*3.模仿案例2,编写ー个通用的数组类模板Array,该类模板包含有构造函数完成数组元素的初始化。排序函数完成元素的排序。输出函数显示排序结果。在主函数中将Array实例化成int、double、char等对象。程序代码如下:#include<iostream>//#include<cassert>#include<iomanip>#include<string>usingnamespacestd;template<typenameT,intn>classArray{ 〃定义ー个函数模板private:intsize;T*element;public:Array(T*A);〜Array。; 〃要注意重复释放地址的有关问题Array(Array&Ary); 〃得注意深复制方面的问题voidsort。;voidshow。;};template<typenameT,intn>Array<T,n>::Array(T*A=NULL){if(n>l)size=n;elsesize=l;element=newT[sizeJ;for(inti=O;i<size;i++)element[i]=A[i];Itemplate<typenameT,intn>Array<T,n>::-Array(){deletedelement;}template<typenameT,intn>Array<T,n>::Array(Array&Ary){size=Ary.size;element=newT[size];for(inti=O;i<size;i++)element[i]=Ary.element[i];}template<typenameT,intn>voidArray<T,n>::sort(){Ttemp;inti,j;for(i=0;i<n-l;i++)for(j=0;j<n-i-l;j++)if(element[j]>element[j+1]){temp=element[j];element[j]=element[j4-1];element[j+lJ=temp;)}template<typenameT,intn>voidArray<T,n>::show()(for(inti=O;i<n;i++){cout«setw(5)«element[i]«setw(5);if((i+l)%5==0)cout«endl;)}intmain(){coutvv”输入数组长度c=8H«endl;intc=8;intp[8];for(inti=0;i<c;i++){couivv”请输入第”vci+kv”个整数a[H«i«"]=n;cin»p[i];)coutvv”排好序的数组如下”<<endl;Array<int,8>intAry(p);intAry.sort();intAry.show();cout«endl;doubled[8J;for(i=0;i<c;i++){coutvv”请输入第”vvi+l<v”个数d[,,«i«"]=n;cin»d[i];)coutvv”排好序的数组如下”vvendl;Array<double,8>douAry(d);douAry.sort();douAry.show();cout«endl;charch网;for(i=0;i<c;i++){coutvv”请输入第"vvi+kv”个字符ch[M«i«n]=M;cin»ch[i];)coutvv”排好序的数组如下"vvendl;Array<char,8>charAry(ch);charAry.sort();charAry.show();cout«endl;return0;实验十六继承与派生1.首先,定义图形基类Shape,在类中有保护数据成员:长度length和宽度width,构造函数完成数据成员的初始化。其次由Shape类公有派生出矩形类Rectangle和三角形类Triangle,每个派生类都有一个计算面积函数Area。,分别计算矩形和三角形的面积。最后在主函数中加以测试。〃程序设计如下:#include<iostream>#include<cmath>constdoublepi=3.14159;〃定义常变量usingnamespacestd;classShape〃定义ー个抽象类Sh叩e{public:virtualdoubleArea()const=0;Shape(){length=O;width=0;}protected:doublelength,width;);classRectangle:publicShape〃通过抽象类派生出•个矩形累public:Rectangle(double1=0,doublew=O):length(l),width(w){}〃构造函数doubleArea()const; 〃计算矩形的面积private:doublelength,widthJ 〃私有数据成员,长,宽);doubleRectangle::Area()const 〃计算矩形的面积(returnlength*width;)classTriangle:publicShape〃定义ー个三角形{public:Triangle(doublea=0,doubleb=0,doublec=0):sl(a),s2(b),s3(c){)〃构造函数doubleArea()const;private:doublesl,s2,s3; 〃私有数据成员,三边长};doubleTriangle::Area()const 〃计算三角形的面积if((sl+s2)<=s3II(sl+s3)<=s2II(s2+s3)<=sl)return0;doubles=(sl+s2+s3)/2;returnsqrt(s*(s-sI)*(s-s2)*(s-s3));)voidprintArea(constShape&s)〃图形面积显示函数{coutvv”面积为:“vvs.Area。vvendl;)intmain()(Rectangler(5,10);Trianglet(5,6,7);coutcv"矩形";〃显示矩形的面积printArea(r);coutvv"三角形〃显示三角形的面积printArea(t);〃如果不能构成三角形return0;.将实验ー|——中实验案例的Time类派生出ZoneTime类,ZoneTime类增加一个表示时区的数据成员zone»编写ZoneTime类的构造,复制构造,显示日期、时间、时区等函数。程序设计如下:#include<iostream>#include<cstring>usingnamespacestd;classZoneTime:publicTime{intZone;intYear,Month,Day,Hour,Minute,Second;public:ZoneTime();ZoneTime(ZoneTime&);voidSetTime(intNewY,intNewMon,intNewD,intNewH,intNewMin,intNewS,intNewZ);voidShowTime();ZoneTime::ZoneTime(){Year=Hour=Minute=Second=0;Month=Day=1;ZoneTime::ZoneTime(ZoneTime&s){Zone=s.Zone;Year=s.Year;Month=s.Month;Day=s.Day;Hour=s.Hour;Minute=s.Minute;Second=s.Second;}voidZoneTime::SetTime(intNewY,intNewMonJntNewDJntNewH,intNcZone=NewZ<0IINewZ>24?0:NewZ;Month=NewMon<1IINewMon>12?0:NewMon;Day=NewD<0IINewD>31?0:NewD;Hour=NewH<0IINewH>23?0:NewH;Minute=NewMin<0IINewMin>59?0:NewMin;Second=NewS<0IINewS>59?0:NewS;iwMinJntNewSjntNewZ){voidZoneTime::ShowTime(){SetTime(intNewYjntNewMon,intNewDjntNewHJntNewMin,intNewS,intNewZ);cout«Year«,,-,,«Month«,,-,,«Day«,\t,«Hour«,,:,,«Minute«,,:,,«Second«,\t,«,,0tK^J:,,«Zone«,\n,;)voidmain(){ZoneTimemyTime;cout«,'Firsttimesetandoutput:M«endl;myTime.ShowTimeO;cout«HSecondtimesetandoutput:u«endl;myTime.SetTime(2010,12,25,10,15,20,8);myTime.ShowTimeO;cout«nThirdtimesetandoutput:"vvendl;ZoneTimemyTimel(myTime);myTime1.ShowTime();.定义一个人员基类Person,其数据成员有姓名、性别、身份证号等基本信息,函数成员包括构造函数、析构函数,用于初始化数据成员和释放动态申请的内存,输出基本信息的函数Perinfo()。从Person类公有派生出Student类,增加数据成员No保存学号、Profe保存专、ル。构造函数初始化基类和自身的数据。输出基本信息函数Stuinfo输出Student类和Person类的数据。程序代码如下:〃定义一个类作为头文件,person.h#defineMas19 〃宏替换#defineN9classperson{ 〃定义ー个person类protected:char*name; 〃注意深复制问题,这chargender;charID[Mas];public:person(char*p,charg,chard[]);person(person&p);-person。{
if(name!=NULL)deletedname;)voidperinfo();〃用person〃用person类派生一个student类classstudent:publicperson{private:charNo[N];char*profe;public:student(personpl,charno[],char*pro);student(student&st);〜student。{if(profe!=NULL)deletedprofe;)voidstuinfo。;);//c.cpp文件#include<iostream>#include<string>#include,,Person.h,,usingnamespacestd;person::person(char*p,charg,chard[]){if(p!=NULL)if(name=newchar[strlen(p)+l])strcpy(name,p);elsename=NULL;gender=g;strcpy(ID,d);)person::person(person&p){if(!=NULL)if(name=newchar[strlen()+l])strcpy(name,);elsename=NULL;〃深复制〃strlen()求字符数组长度gender=p.gender;strcpy(ID,p.ID);voidperson::perinfo(){coutvぐ姓名:“vvnamevvendkv”性别:“vvgendervvendkv”身份证号:“vvIDvvendl;)student::student(personpl,charno[],char*pro):person(p1){strcpy(No,no);if(pro!=NULL)if(profe=newchar[strlen(pro)+1])strcpy(profe,pro);elseprofe=NULL;)student::student(student&st):person(st){strcpy(No,st.No);if(fe!=NULL)if(profe=newchar[strlen(fe)+1])strcpy(profe,fe);elseprofe=NULL;)voidstudent::stuinfo(){coutvv"学号:"vvNovvendl;perinfo();coutvv”专业:n«profe«endl;}//main.cpp文件#include<iostream>#include<string>#include,,Person.h,,usingnamespacestd;intmain(){charno[N];coutvv”请输入学号:";cin»no;char*p,na[19J;COUIVV”请输入姓名:";cin»na;p=na;charg;coutvv”请输入性别cin»g;chard[Mas];coutvc”请输入身份证号:";cin»d;char*pro,zy[9];coutcv”请输入专业:”cin»zy;pro=zy;personpl(p,g,d);pl.perinfo();studentst(pl,no,pro);st.stuinfo();return0;・实验十七多继承与虚基类.实验十二中实验内容第3题类DateTime由Date类和Time类组合而成。现在要求重新定义DateTime类,该类由Date类和Time类公有派生而来。从而理解ー个类由其它类组合而成(hasa关系)和继承其他类(isa关系)的区别。程序代码如下:〃定义一个头文件DateTime.hclassDate{ 〃定义ー个Date类protected:intYear;intMonth;intDay;public:Date(intyear,intmonth,intday); //Date的构造函数〜Date。{}; //Date的析构函数Date(Date&date);voidShowDate。;〃显示时间函数intInspectY(intm,intn); 〃判断year是否符合要求intlnspectM(intm,intn); 〃判断month是否符合要求intInspectD(intyear,intmonth,intday); 〃判断日期};classTime{intHour;intMinte;intSecond;public:Time(inth,intm,ints);Time(Time&T);intchoicehour(inth);intchoicetime(intm);voidShowTime();};classDateTime:publicDate,publicTime{public:DateTime(Dated,Timet):Date(d),Time(t){}DateTime(DateTime&DT):Date(DT),Time(DT){}voidShowDateTime(););//c.cpp文件如F:#include<iostream>#include,'DateTime.hHusingnamespacestd;Date::Date(intyear,intmonth,intday){inta=1000,b=12;Year=InspectY(year,a);Month=InspectM(month,b);Day=InspectD(year,month,day);}intDate::InspectM(intm,intn){if(m>n)return1;elsereturnm;intDate::InspectY(intm,intn){if(m<1000)return1000;elsereturnm;)intDate::InspectD(intyear,intmonth,intday){if((year%400==0)ll((year%4==0)&&(year%100!=0))){if(month==2){if(day>29llday<=0)return1;elsereturnday;))elseif(month==2){if(day>=29llday<=0)return1;elsereturndayif(month==4llmonth==6llmonth==9llmonth==l1){if(day>30llday<=0)return1;elsereturnday;)else{if(day>31llday<=0)return1;elsereturnday;)}Date::Date(Date&date){Year=date.Year;Month=date.Month;Day=date.Day;}voidDate::ShowDate()coutvvYearvv“年“vvMonthvv"月"vvDayvv”日”;Time::Time(inth,intmjnts){Hour=choicehour(h);Minte=choicetime(m);Second=choicetime(s);)Time::Time(Time&T){Hour=T.Hour;Minte=T.Minte;Second=T.Second;}intTime::choicehour(inth){if(h<=0llh>24){coutvv”输入有误,强制至为ドvvendl;return1;elsereturnh;intTime::choicetime(intm){if(m<0llm>60){coutvぐ输入有误,强制至为ドvvendl;return1;)elsereturnm;)voidTime::ShowTime(){cout<<Hour«',:',«Minte«M:,,«Second«endl;}voidDateTime::ShowDateTime(){ShowDate();ShowTime();}//main.cpp文件#include<iostream>#include,fDateTime.h',usingnamespacestd;intmain(){inty,mn,d,h,m,s;cout«”请输入年:";cin»y;cout<<”请输入月:“;cin»mn;coul<<”请输入日:”;cin»d;Dateda(y,mn,d);cout«”输入时间Hour=";cin»h;cout«”输入时间Minte=M;cin»m;coutvv”输入时间secondゴ;cin»s;DateTimeDT(da,t);DT.ShowDateTime();return0;).使用实验十六实验内容第3题的Person类,将其分别公有派生出专家类Technician和市长类Mayor。其中Technician类增加成员有技术专长,Mayor类增加工作城市。再由Technician类和Mayor类共同公有派生出专家市长类Tech_Mayor。为所有类定义构造、析构函数。在主函数中创建Tech_Mayor类对象,通过该对象访问Person类中的成员。程序代码如下:〃定义一个头文件Person.h#defineMas19 〃宏替换classperson{ 〃疋义ー个person类protected:char*name; 〃注意深复制问题,这chargender;charID[Mas];public:person(char*p,charg,chard[]);person(person&p);〜person(乂//delete动态生成的空间,防止空间泄露if(name!=NULL)deletef]name;)voidperinfo();};classTechnician:publicperson{protected:char*jishu;public:Technician(personp,char*js);Technician(Technician&Te);〃跟上面的name一样注意深复制上的,空间泄露问题〜〃跟上面的name一样注意深复制上的,空间泄露问题if(jishu!=NULL)delete[]jishu;voidshowTechnician。;};classMayor:publicperson{protected:char*city;public:Mayor(personp,char*ct);Mayor(Mayor&ma);〜Mayor。{if(city!=NULL)delete[Jcity;)voidshowMayor。;};classTech_Mayor:publicMayor,publicTechnician(public:Tech_Mayor(Mayorma,Techniciante):Mayor(ma),Technician(te){)Tech_Mayor(Tech_Mayor&T_M);-Tech_Mayor(){}voidshowTech_Mayor();};//c.cpp文件#include<iostream>#include<string>#include"Person.hnusingnamespacestd;person::person(char*p,charg,chard[]){if(p!=NULL)if(name=newchar[strlen(p)+l])strcpy(name,p);elsename=NULL;gender=g;strcpy(ID,d);)person::person(person&p){if(!=NULL)if(name=newchar[strlen()+l])strcpy(name,);〃深复制〃strlen()求字符数组长度〃重新动态生成一个name空间,防止空间的二次释放elsename=NULL;gender=p.gender;strcpy(ID,p.ID);Ivoidperson::perinfb(){coutvv”姓名:“vvnamevvendkv”性另リ:“vvgendervvendlvv”身份证号:“vvIDvvendl;)Technician::Technician(personp,char*js):person(p){if(js!=NULL)if(jishu=newchar[strlen(js)+l])strcpy
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年山西工程职业学院单招职业适应性测试题库有答案详细解析
- 2026年威海荣成市面向村(社区)党组织书记公开招聘事业单位工作人员(3人)考试备考题库及答案解析
- 河南省南召县联考2026年初三下学期第二次周练语文试题含解析
- 甘肃省天水市秦安县2025-2026学年下学期初三联考试卷英语试题含解析
- 山东省德州市乐陵市花园中学2026年初三1月第一次诊断语文试题理试卷含解析
- 2026届云南省楚雄州-重点名校初三重点班下学期开学英语试题含解析
- 浙江省鄞州区2025-2026学年初三第一次联考(4月)语文试题试卷含解析
- 2026届山东省济宁地区初三下学期期中统考英语试题含解析
- 浙江杭州余杭区重点中学2025-2026学年初三下学期3月练习卷英语试题试卷含解析
- 环境提升整改承诺书(6篇)
- 建筑防水工程技术规程DBJ-T 15-19-2020
- 《创新创业基础》课件-模块四 创新成果保护与转化
- 燃料检修潜在风险与预控措施
- 中学生防震减灾知识
- 劳务合同模板电子下载
- 新安全生产法全文-安全生产法全文
- 初中体育-篮球绕杆运球教学课件设计
- 《物理(下册)》教学课件-第六章-光现象及其应用
- 麦积山石窟课件
- 分数百分数应用题的复习课件
- 开复工安全检查表
评论
0/150
提交评论