C++課后習(xí)題第八章-第十二章_第1頁
C++課后習(xí)題第八章-第十二章_第2頁
C++課后習(xí)題第八章-第十二章_第3頁
C++課后習(xí)題第八章-第十二章_第4頁
C++課后習(xí)題第八章-第十二章_第5頁
已閱讀5頁,還剩81頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第八章請(qǐng)檢查下面的程序,找出其中的錯(cuò)誤(先不要上機(jī),在紙面上作人工檢查),并改正。然后上機(jī)調(diào)試,使之能正常運(yùn)行。運(yùn)行時(shí)從鍵盤輸入時(shí)、分、秒的值,檢查輸出是否正確。原文:#include<iostream>usingnamespacestd;classTime{ voidset_time(void) ; voidshow_time(void); inthour; intminute; intsec;};Timet;intmain(){ set_time(); show_time(); return0;}voidset_time(void) { cin>>t.hour; cin>>t.minute; cin>>t.sec;}voidshow_time(void){ cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}改:#include<iostream>usingnamespacestd;classTime{ public://成員改為公用的 inthour; intminute; intsec;};Timet;voidset_time(void) {//在main函數(shù)之前定義 cin>>t.hour; cin>>t.minute; cin>>t.sec;}voidshow_time(void){//在main函數(shù)之前定義 cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}intmain(){ set_time(); show_time(); return0;}改寫例8.1程序,要求:將數(shù)據(jù)成員改為私有的;將輸入和輸出的功能改為由成員函數(shù)實(shí)現(xiàn);在類體內(nèi)定義成員函數(shù);#include<iostream>usingnamespacestd;classTime{ public: voidset_time(void){ cin>>hour; cin>>minute; cin>>sec; } voidshow_time(void){ cout<<hour<<":"<<minute<<":"<<sec<<endl; } private: inthour; intminute; intsec;};Timet;intmain(){ t.set_time(); t.show_time(); return0;}在第2題的基礎(chǔ)上進(jìn)行如下修改:在類體內(nèi)聲明成員函數(shù),而在類外定義成員函數(shù)。#include<iostream>usingnamespacestd;classTime{ public: voidset_time(void); voidshow_time(void); private: inthour; intminute; intsec;};voidTime::set_time(void){ cin>>hour; cin>>minute; cin>>sec;}voidTime::show_time(void){ cout<<hour<<":"<<minute<<":"<<sec<<endl;}Timet;intmain(){ t.set_time(); t.show_time(); return0;}在第8.3.3節(jié)中分別給出了包含類定義的頭文件student.h,包含成員函數(shù)定義的源文件studnet.cpp以及包含主函數(shù)的源文件main.cpp。請(qǐng)對(duì)該程序完善化,在類中增加一個(gè)對(duì)數(shù)據(jù)成員賦初值的成員函數(shù)set_value。原文8.3.3:#include<iostream>usingnamespacestd;classStudent{ public: voiddisplay(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; }; private: intnum; charname[20]; charsex;};intmain(){ Studentstud; stud.display(); return0;}改:main.cpp#include<iostream>#include"student.h"usingnamespacestd;intmain(){ Students; s.SetValue(); s.Display(); return0;}Student.husingnamespacestd;classStudent{ public: voidDisplay(); voidSetValue(); private: intnum; charname[20]; charsex;};Student.cpp#include<iostream>#include"student.h"usingnamespacestd;voidStudent::Display(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl;}voidStudent::SetValue(){ cin>>num>>name>>sex;}將例8.4改寫為一個(gè)多文件的程序:將類定義放在頭文件arraymax.h中;將成員函數(shù)定義放在源文件arrymax.cpp中;主函數(shù)放在源文件file1.cpp中。原文例8.4:#include<iostream>usingnamespacestd;classArray_max{ public: voidset_value(); voidmax_value(); voidshow_value(); private: intarray[10]; intmax;};voidArray_max::set_value(){ inti; for(i=0;i<10;i++) cin>>array[i];}voidArray_max::max_value(){ inti; max=array[0]; for(i=1;i<10;i++) if(array[i]>max)max=array[i];}voidArray_max::show_value(){ cout<<"max="<<max<<endl;}intmain(){ Array_max arrmax; arrmax.set_value(); arrmax.max_value(); arrmax.show_value(); return0;}改:main.cpp#include<iostream>#include"arraymax.h"usingnamespacestd;intmain(){ ArrayMaxarrmax; arrmax.SetValue(); arrmax.MaxValue(); arrmax.ShowMax(); return0;}arraymax.hclassArrayMax{ public: voidSetValue();//設(shè)置數(shù)組元素值 voidMaxValue();//找出最大值 voidShowMax();//輸出最大值 private: intarray[10];//整型數(shù)組 intmax;//數(shù)組最大值};arraymax.cpp#include<iostream>#include"arraymax.h"usingnamespacestd;voidArrayMax::SetValue(){ inti; for(i=0;i<10;i++){ cin>>array[i]; }}voidArrayMax::MaxValue(){ inti; max=array[0]; for(i=1;i<10;i++){ if(max<array[i]){ max=array[i]; } }}voidArrayMax::ShowMax(){ cout<<"max="<<max<<endl;}需要求3個(gè)長方柱的體積,請(qǐng)編寫一個(gè)基于對(duì)象的程序。數(shù)據(jù)成員包括length(長)、width(寬)和weight(高)。要求用成員函數(shù)實(shí)現(xiàn)以下功能。由鍵盤分別輸入3個(gè)長方柱的長、寬和高。計(jì)算長方柱的體積;輸出3個(gè)長方柱的體積。#include<iostream>usingnamespacestd;classBox{ public: voidget_value(); floatvolume(); voiddisplay(); public: floatlengh; floatwidth; floatheight;};voidBox::get_value(){ cout<<"pleaseinputlengh,width,height:"; cin>>lengh;//由鍵盤分別輸入3個(gè)長方柱的長、寬和高。 cin>>width; cin>>height;}floatBox::volume(){ return(lengh*width*height);//計(jì)算長方體體積。}voidBox::display(){ cout<<volume()<<endl;}intmain(){ Boxbox1,box2,box3;//輸出3個(gè)長方體的體積。 box1.get_value(); cout<<"volmueofbax1is"; box1.display(); box2.get_value(); cout<<"volmueofbax2is"; box2.display(); box3.get_value(); cout<<"volmueofbax3is"; box3.display(); return0;}第九章分析下面的程序,寫出其運(yùn)行時(shí)的輸出結(jié)果。#include<iostream>usingnamespacestd;classDate{ public: Date(int,int,int); Date(int,int); Date(int); Date(); voiddisplay(); private: intmonth; intday; intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}Date::Date(intm,intd):month(m),day(d){ year=2005;}Date::Date(intm):month(m){ day=1; year=2005;}Date::Date(){ month=1; day=1; year=2005;}voidDate::display(){ cout<<month<<"/"<<day<<"/"<<year<<endl;}intmain(){ Dated1(10,13,2005); Dated2(12,30); Dated3(10); Dated4; d1.display(); d2.display(); d3.display(); d4.display(); return0;}如果將第2題程序的第5行改為用默認(rèn)參數(shù),即Date(int=1;int=1;int=2005);分析程序有無問題。上機(jī)編譯,分析出錯(cuò)信息,修改程序使之能通過編譯。要求保留上面一行給出的構(gòu)造函數(shù),同時(shí)能輸出與第2題程序相同的輸出結(jié)果。改:#include<iostream>usingnamespacestd;classDate{ public: Date(int=1,int=1,int=2005); voiddisplay(); private: intmonth; intday; intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}voidDate::display(){ cout<<month<<"/"<<day<<"/"<<year<<endl;}intmain(){ Dated1(10,13,2005); Dated2(12,30); Dated3(10); Dated4; d1.display(); d2.display(); d3.display(); d4.display(); return0;}建立一個(gè)對(duì)象數(shù)組,內(nèi)放5個(gè)學(xué)生的數(shù)據(jù)(學(xué)號(hào)、成績),用指針指向數(shù)組首元素,輸出第1,3,5學(xué)生的數(shù)據(jù)。#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} voiddisplay(); private: intnum; floatscore;};voidStudent::display(){ cout<<"num="<<num<<""<<"score="<<score<<endl;}intmain(){ Studentstud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5) }; Student*p=stud; for(inti=0;i<=2;p=p+2,i++) p->display(); return0;}建立一個(gè)對(duì)象數(shù)組,內(nèi)放5個(gè)學(xué)生的數(shù)據(jù)(學(xué)號(hào)、成績),設(shè)立一個(gè)函數(shù)max,用指向?qū)ο蟮闹羔樧龊瘮?shù)參數(shù),在max函數(shù)中找出5個(gè)學(xué)生中成績最高者,并輸出其學(xué)號(hào)。#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} intnum; floatscore;};intmain(){ Studentstud[5]={ Student(101,78.5),Student(102,85.5),Student(103,98.5), Student(104,100.0),Student(105,95.5) }; voidmax(Student*); Student*p=&stud[0]; max(p); return0;}voidmax(Student*arr){ floatmax_score=arr[0].score; intk=0; for(inti=1;i<5;i++) if(arr[i].score>max_score){ max_score=arr[i].score; k=i; } cout<<"num="<<arr[k].num<<""<<"max_score="<<max_score<<endl;}閱讀下面程序,分析其執(zhí)行過程,寫出輸出結(jié)果#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){}voidchange(intn,floats){ num=n; score=s; }voiddisplay(){ cout<<num<<""<<score<<endl; } private: intnum; floatscore;};intmain(){ Studentstud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); return0;}將第6題程序分別作一下修改,分析所修改部分的含義以及編譯和運(yùn)行的情況。將main函數(shù)第2行改為constStudentstud(01,78.5);在(1)的基礎(chǔ)上修改程序,使之能正常運(yùn)行,用change函數(shù)修改數(shù)據(jù)成員mun和score的值。將main函數(shù)改為intmain(){Studentstud(101,78.5);Student*p=&stud;>display();P->change(101,80.5);P-display();return0;}其他部分仍同第6題程序。在(2)的基礎(chǔ)上將main函數(shù)第3行改為constStudent*p=&stud;再把main函數(shù)第3行改為Student*constp=&stud;(1)[Error]passing'constStudent'as'this'argumentof'voidStudent::display()'discardsqualifiers[-fpermissive][Error]passing'constStudent'as'this'argumentof'voidStudent::change(int,float)'discardsqualifiers[-fpermissive][Error]passing'constStudent'as'this'argumentof'voidStudent::display()'discardsqualifiers[-fpermissive](2)

#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} voidchange(intn,floats)const{ num=n; score=s; }voiddisplay()const{ cout<<num<<""<<score<<endl; } private: mutableintnum; mutablefloatscore;};intmain(){ constStudentstud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); return0;}(3)#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} voidchange(intn,floats){ num=n; score=s; }voiddisplay(){ cout<<num<<""<<score<<endl; } private: intnum; floatscore;};intmain(){ Studentstud(101,78.5); Student*p=&stud; p->display(); p->change(101,80.5); p->display(); return0;}(4)#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} voidchange(intn,floats){ num=n; score=s; } voiddisplay()const{ cout<<num<<""<<score<<endl; } private: intnum; floatscore;};intmain(){ Studentstud(101,78.5); constStudent*p=&stud; p->display(); stud.change(101,80.5); p->display(); return0;}(5)#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){} voidchange(intn,floats){ num=n; score=s; } voiddisplay(){ cout<<num<<""<<score<<endl; } private: intnum; floatscore;};intmain(){ Studentstud(101,78.5); Student*constp=&stud; p->display(); p->change(101,80.5); p->display(); return0;}修改第6題程序,增加一個(gè)fun函數(shù),改寫main函數(shù)。改為在fun函數(shù)中調(diào)用change和display函數(shù)。在fun函數(shù)中使用對(duì)象引用(Student&)作為形參。#include<iostream>usingnamespacestd;classStudent{ public: Student(intn,floats):num(n),score(s){}voidchange(intn,floats){ num=n; score=s; }voiddisplay(){ cout<<num<<""<<score<<endl; } private: intnum; floatscore;};voidfun(Student&stu){ stu.display(); stu.change(101,80.5); stu.display();}intmain(){ Studentstud(101,78.5); voidfun(Student&); fun(stud); return0;}商店銷售某一商品,每天公布統(tǒng)一的折扣(discount)。同時(shí)允許銷售人員在銷售時(shí)靈活掌握售價(jià)(price),在此基礎(chǔ)上,一次購10件以上者,還可以享受9.8折優(yōu)惠?,F(xiàn)已知當(dāng)天3個(gè)銷貨員銷售情況為銷貨員號(hào)(num)銷貨件數(shù)(quantity)銷貨單價(jià)(price)101523.51021224.5610310021.5請(qǐng)編寫程序,計(jì)算出當(dāng)日此商品的總銷售款sum以及每件商品的平均售價(jià)。要求用靜態(tài)數(shù)據(jù)成員和靜態(tài)成員函數(shù)。(提示:將折扣discount,總銷售款sum和商品銷售總件數(shù)n聲明為靜態(tài)數(shù)據(jù)成員,再定義靜態(tài)成員函數(shù)average(求平均售價(jià))和display(輸出結(jié)果))。#include<iostream>usingnamespacestd;classProduct{ public: Product(intn,intq,floatp):num(n),quantity(q),price(p){} voidtotal(); staticfloataverage(); staticvoiddisplay(); private: intnum; intquantity; floatprice; staticfloatdiscount; staticfloatsum; staticintn;};voidProduct::total(){ floatrate=1.0; if(quantity>10)rate=0.98*rate; sum=sum+quantity*price*rate*(1-discount); n=n+quantity;}voidProduct::display(){ cout<<sum<<endl; cout<<average()<<endl;}floatProduct::average(){ return(sum/n);}floatProduct::discount=0.05;floatProduct::sum=0;intProduct::n=0;intmain(){ ProductProd[3]={ Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5) }; for(inti=0;i<3;i++) Prod[i].total(); Product::display(); return0;}將例9.13程序中的display函數(shù)不放在Time類中,而作為類外的普通函數(shù),然后分別在Time和Date類中將display聲明為友元函數(shù)。在主函數(shù)中調(diào)用display函數(shù),display函數(shù)分別引用Time和Date兩個(gè)類的對(duì)象的私有數(shù)據(jù),輸出年、月、日和時(shí)、分、秒。#include<iostream>usingnamespacestd;classDate;classTime{ public: Time(int,int,int); friendvoiddisplay(constDate&,constTime&); private: inthour; intminute; intsec;};Time::Time(inth,intm,ints){ hour=h; minute=m; sec=s;}classDate{ public: Date(int,int,int); friendvoiddisplay(constDate&,constTime&); private: intmonth; intday; intyear;};Date::Date(intm,intd,inty){ month=m; day=d; year=y;}voiddisplay(constDate&d,constTime&t){ cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}intmain(){ Timet1(10,13,56); Dated1(12,25,2004); display(d1,t1); return0;}將例9.13中的Time類聲明為Date類的友元類,通過Time類中的display函數(shù)引用Date類對(duì)象的私有數(shù)據(jù),輸出年、月、日和時(shí)、分、秒。#include<iostream>usingnamespacestd;classTime;classDate{ public: Date(int,int,int); friendTime; private: intmonth; intday; intyear;};Date::Date(intm,intd,inty):month(m),day(d),year(y){}classTime{ public: Time(int,int,int); voiddisplay(constDate&); private: inthour; intminute; intsec;};Time::Time(inth,intm,ints):hour(h),minute(m),sec(s){}voidTime::display(constDate&d){ cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<hour<<":"<<minute<<":"<<sec<<endl;}intmain(){ Timet1(10,13,56); Dated1(12,25,2004); t1.display(d1); return0;}將例9.14改寫為在類模板外定義各成員函數(shù)。#include<iostream>usingnamespacestd;template<classnumtype>classCompare{ public: Compare(numtypea,numtypeb); numtypemax(); numtypemin(); private: numtypex,y;};template<classnumtype>Compare<numtype>::Compare(numtypea,numtypeb){ x=a; y=b;}template<classnumtype>numtypeCompare<numtype>::max(){ return(x>y)?x:y;}template<classnumtype>numtypeCompare<numtype>::min(){ return(x<y)?x:y;}intmain(){ Compare<int>cmp1(3,7); cout<<cmp1.max()<<"istheMaximumoftwointegernumbers."<<endl; cout<<cmp1.min()<<"istheMinimumoftwointegernumbers."<<endl<<endl; Compare<float>cmp2(45.78,93.6); cout<<cmp2.max()<<"istheMaximumoftwofloatnumbers."<<endl; cout<<cmp2.min()<<"istheMinimumoftwofloatnumbers."<<endl<<endl; Compare<char>cmp3('a','A'); cout<<cmp3.max()<<"istheMaximumoftwocharacters."<<endl; cout<<cmp3.min()<<"istheMinimumoftwocharacters."<<endl; return0;}第十章定義一個(gè)復(fù)數(shù)類Complex,重載運(yùn)算符“+”,使之能用于復(fù)數(shù)的加法運(yùn)算。將運(yùn)算符函數(shù)重載為非成員、非友元的普通函數(shù)。編寫程序,求兩個(gè)復(fù)數(shù)之和。#include<iostream>usingnamespacestd;classComplex{ public: Complex(){ real=0; imag=0; }Complex(doubler,doublei){ real=r; imag=i; } doubleget_real(); doubleget_imag(); voiddisplay(); private: doublereal; doubleimag;};doubleComplex::get_real(){ returnreal;}doubleComplex::get_imag(){ returnimag;}voidComplex::display(){ cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complexoperator+(Complex&c1,Complex&c2){ return Complex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());}intmain(){ Complexc1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c3="; c3.display(); return0;}定義一個(gè)復(fù)數(shù)類Complex,重載運(yùn)算符“+”,“-”,“*”,“/”,使之能用于復(fù)數(shù)的加、減、乘和除。運(yùn)算符重載函數(shù)作為Complex類的成員函數(shù)。編寫程序,分別求兩個(gè)復(fù)數(shù)之和、差、積和商。#include<iostream>usingnamespacestd;classComplex{ public: Complex(){ real=0; imag=0; }Complex(doubler,doublei){ real=r; imag=i; }Complexoperator+(Complex&c2); Complexoperator-(Complex&c2); Complexoperator*(Complex&c2); Complexoperator/(Complex&c2); voiddisplay(); private: doublereal; doubleimag;};ComplexComplex::operator+(Complex&c2){ Complexc; c.real=real+c2.real; c.imag=imag+c2.imag; returnc;}ComplexComplex::operator-(Complex&c2){ Complexc; c.real=real-c2.real; c.imag=imag-c2.imag; returnc;}ComplexComplex::operator*(Complex&c2){ Complexc; c.real=real*c2.real-imag*c2.imag; c.imag=imag*c2.real+real*c2.imag; returnc;}ComplexComplex::operator/(Complex&c2){ Complexc; c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag); returnc;}voidComplex::display(){ cout<<"("<<real<<","<<imag<<"i)"<<endl;}intmain(){ Complexc1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c1+c2="; c3.display(); c3=c1-c2; cout<<"c1-c2="; c3.display(); c3=c1*c2; cout<<"c1*c2="; c3.display(); c3=c1/c2; cout<<"c1/c2="; c3.display(); return0;}定義一個(gè)復(fù)數(shù)類Complex,重載運(yùn)算符“+”,使之能用于復(fù)數(shù)的加法運(yùn)算。參加運(yùn)算的兩個(gè)運(yùn)算量可以都是類對(duì)象,也可以是其中有一個(gè)是整數(shù),順序任意。例如,c1+c2,i+c1和c1+i均合法(設(shè)i為整數(shù),c1和c2為復(fù)數(shù))。編寫程序,分別求兩個(gè)復(fù)數(shù)之和、整數(shù)和復(fù)數(shù)之和。#include<iostream>usingnamespacestd;classComplex{ public: Complex(){ real=0; imag=0; } Complex(doubler,doublei){ real=r; imag=i; } Complexoperator+(Complex&c2); Complexoperator+(int&i); friendComplexoperator+(int&,Complex&); voiddisplay(); private: doublereal; doubleimag;};ComplexComplex::operator+(Complex&c){ returnComplex(real+c.real,imag+c.imag);}ComplexComplex::operator+(int&i){ returnComplex(real+i,imag);}voidComplex::display(){ cout<<"("<<real<<","<<imag<<"i)"<<endl;}Complexoperator+(int&i,Complex&c){ returnComplex(i+c.real,c.imag);}intmain(){ Complexc1(3,4),c2(5,-10),c3; inti=5; c3=c1+c2; cout<<"c1+c2="; c3.display(); c3=i+c1; cout<<"i+c1="; c3.display(); c3=c1+i; cout<<"c1+i="; c3.display(); return0;}有兩個(gè)矩陣a和b,均為2行3列。求兩個(gè)矩陣之和。重載運(yùn)算符“+”,使之能用于矩陣相加(如c=a+b)。#include<iostream>usingnamespacestd;classMatrix{ //定義Matrix類 public: Matrix(); //默認(rèn)構(gòu)造函數(shù) friendMatrixoperator+(Matrix&,Matrix&); //重載運(yùn)算符“+” voidinput(); //輸入數(shù)據(jù)函數(shù) voiddisplay(); //輸出數(shù)據(jù)函數(shù) private: intmat[2][3];};Matrix::Matrix(){ //定義構(gòu)造函數(shù) for(inti=0;i<2;i++) for(intj=0;j<3;j++) mat[i][j]=0;}Matrixoperator+(Matrix&a,Matrix&b){//定義重載運(yùn)算符“+”函數(shù) Matrixc; for(inti=0;i<2;i++) for(intj=0;j<3;j++){ c.mat[i][j]=a.mat[i][j]+b.mat[i][j]; } returnc;}voidMatrix::input(){//定義輸入數(shù)據(jù)函數(shù) cout<<"inputvalueofmatrix:"<<endl; for(inti=0;i<2;i++) for(intj=0;j<3;j++) cin>>mat[i][j];}voidMatrix::display(){ //定義輸出數(shù)據(jù)函數(shù) for(inti=0;i<2;i++){ for(intj=0;j<3;j++){ cout<<mat[i][j]<<""; } cout<<endl; }}intmain(){ Matrixa,b,c; a.input(); b.input(); cout<<endl<<"Matrixa:"<<endl; a.display(); cout<<endl<<"Matrixb:"<<endl; b.display(); c=a+b; //用重載運(yùn)算符“+”實(shí)現(xiàn)兩個(gè)矩陣相加 cout<<endl<<"Matrixc=Matrixa+Matrixb:"<<endl; c.display(); return0;}在第4題的基礎(chǔ)上,重載流插入運(yùn)算符“<<”和流提取運(yùn)算符“>>”,使之能用于該矩陣的輸入和輸出。#include<iostream>usingnamespacestd;classMatrix{ public: Matrix(); friendMatrixoperator+(Matrix&,Matrix&); friendostream&operator<<(ostream&,Matrix&); friendistream&operator>>(istream&,Matrix&); private: intmat[2][3];};Matrix::Matrix(){ for(inti=0;i<2;i++) for(intj=0;j<3;j++) mat[i][j]=0;}Matrixoperator+(Matrix&a,Matrix&b){ Matrixc; for(inti=0;i<2;i++) for(intj=0;j<3;j++){ c.mat[i][j]=a.mat[i][j]+b.mat[i][j]; } returnc;}istream&operator>>(istream&in,Matrix&m){ cout<<"inputvalueofmatrix:"<<endl; for(inti=0;i<2;i++) for(intj=0;j<3;j++) in>>m.mat[i][j]; returnin;}ostream&operator<<(ostream&out,Matrix&m){ for(inti=0;i<2;i++){ for(intj=0;j<3;j++){ out<<m.mat[i][j]<<""; } out<<endl; } returnout;}intmain(){ Matrixa,b,c; cin>>a; cin>>b; cout<<endl<<"Matrixa:"<<endl<<a<<endl; cout<<endl<<"Matrixb:"<<endl<<b<<endl; c=a+b; cout<<endl<<"Matrix c = Matrix a + Matrixb:"<<endl<<c<<endl; return0;}請(qǐng)編寫程序,處理一個(gè)復(fù)數(shù)與一個(gè)double數(shù)相加的運(yùn)算,結(jié)果存放在一個(gè)double型的變量d1中,輸出d1的值,再以復(fù)數(shù)形式輸出此值。定義Complex(復(fù)數(shù))類,在成員函數(shù)中包含重載類型轉(zhuǎn)換運(yùn)算符:operatordouble(){returnreal;}#include<iostream>usingnamespacestd;classComplex{ public: Complex(){ real=0; imag=0; } Complex(doubler){ real=r; imag=0; } Complex(doubler,doublei){ real=r; imag=i; } operatordouble(){ returnreal; } voiddisplay(); private: doublereal; doubleimag;};voidComplex::display(){ cout<<"("<<real<<","<<imag<<")"<<endl;}intmain(){ Complexc1(3,4),c2; doubled1; d1=2.5+c1; cout<<"d1="<<d1<<endl; c2=Complex(d1); cout<<"c2="; c2.display(); return0;}定義一個(gè)Teacher(教師)類和一個(gè)Student(學(xué)生)類,二者有一部分?jǐn)?shù)據(jù)成員是相同的,例如num(號(hào)碼),name(姓名),sex(性別)。編寫程序,將一個(gè)Student對(duì)象(學(xué)生)轉(zhuǎn)換為Teacher(教師)類,只將以上3個(gè)相同的數(shù)據(jù)成員移植過去??梢栽O(shè)想為:一位學(xué)生大學(xué)畢業(yè)了,留校擔(dān)任教師,他原有的部分?jǐn)?shù)據(jù)對(duì)現(xiàn)在的教師身份來說任然是有用的,應(yīng)當(dāng)保留并成為其教師數(shù)據(jù)的一部分。#include<iostream>#include<string.h>usingnamespacestd;classStudent{ public: Student(int,char[],char,float); intget_num(){ returnnum; } char*get_name(){ returnname; } charget_sex(){ returnsex; } voiddisplay(){ cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\nscore:"<<score<<"\n\n"; } private: intnum; charname[20]; charsex; floatscore;};Student::Student(intn,charnam[],chars,floatso){ num=n; strcpy(name,nam); sex=s; score=so;}classTeacher{ public: Teacher(){} Teacher(Student&); Teacher(intn,charnam[],charsex,floatpay); voiddisplay(); private: intnum; charname[20]; charsex; floatpay;};Teacher::Teacher(intn,charnam[],chars,floatp){ num=n; strcpy(name,nam); sex=s; pay=p;}Teacher::Teacher(Student&stud){ num=stud.get_num(); strcpy(name,stud.get_name()); sex=stud.get_sex(); pay=1500;}voidTeacher::display(){ cout<<"num:"<<num<<"\nname:"<<name<<"\nsex:"<<sex<<"\npay:"<<pay<<"\n\n";}intmain(){ Teacherteacher1(10001,"Li",'f',1234.5),teacher2; Studentstudent1(20010,"Wang",'m',89.5); cout<<"student1:"<<endl; student1.display(); teacher2=Teacher(student1); cout<<"teacher2:"<<endl; teacher2.display(); return0;}第十一章將例11.1的程序片段補(bǔ)充和改寫成一個(gè)完整、正確地程序,用公用繼承方式。在程序中應(yīng)包括輸入數(shù)據(jù)的函數(shù),在程序運(yùn)行時(shí)輸入num,name,sex,age和addr的值,程序應(yīng)輸出以上5個(gè)數(shù)據(jù)的值。#include<iostream>usingnamespacestd;classStudent{ public: voidget_value(){ cin>>num>>name>>sex; } voiddisplay(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; } private: intnum; charname[10]; charsex;};classStudent1:publicStudent{ public: voidget_value_1(){ get_value(); cin>>age>>addr; } voiddisplay_1(){ cout<<"age:"<<age<<endl;//引用派生類的私有成員,正確。 cout<<"address:"<<addr<<endl; }//引用派生類的私有成員,正確。 private: intage; charaddr[30];};int main(){ Student1stud1; stud1.get_value_1(); stud1.display(); stud1.display_1(); return0;}將例11.2的程序片段補(bǔ)充和改寫成一個(gè)完整、正確地程序,用私有繼承方式。在程序中應(yīng)包括輸入數(shù)據(jù)的函數(shù),在程序運(yùn)行時(shí)輸入num,name,sex,age和addr的值,程序應(yīng)輸出以上5個(gè)數(shù)據(jù)的值。#include<iostream>usingnamespacestd;classStudent{ public: voidget_value(){ cin>>num>>name>>sex; } voiddisplay(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; } private: intnum; charname[10]; charsex;};classStudent1:privateStudent{ public: voidget_value_1(){ get_value(); cin>>age>>addr; } voiddisplay_1(){ display(); cout<<"age:"<<age<<endl;//引用派生類的私有成員,正確。 cout<<"address:"<<addr<<endl; }//引用派生類的私有成員,正確。 private: intage; charaddr[30];};intmain(){ Student1stud1; stud1.get_value_1(); stud1.display_1(); return0;}將例11.3的程序片段修改和補(bǔ)充成一個(gè)完整、正確地程序,用保護(hù)繼承方式。在程序中應(yīng)包括輸入數(shù)據(jù)的函數(shù)。#include<iostream>usingnamespacestd;classStudent{//聲明基類 public://基類公用成員 voidget_value(); voiddisplay(); protected://基類保護(hù)成員 intnum; charname[10]; charsex;};voidStudent::get_value(){ cin>>num>>name>>sex;}voidStudent::display(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl;}classStudent1:protectedStudent{ //聲明一個(gè)保護(hù)派生類 public: voidget_value_1(); voiddisplay1(); private: intage; charaddr[30];};voidStudent1::get_value_1(){ get_value(); cin>>age>>addr;}voidStudent1::display1(){ cout<<"num:"<<num<<endl;//引用基類的保護(hù)成員 cout<<"name:"<<name<<endl;//引用基類的保護(hù)成員 cout<<"sex:"<<sex<<endl;//引用基類的保護(hù)成員 cout<<"age:"<<age<<endl; //引用派生類的私有成員 cout<<"address:"<<addr<<endl; //引用派生類的私有成員}intmain(){ Student1stud1;//student1類的對(duì)象 stud1.get_value_1();//stud1的公用成員函數(shù) stud1.display1();//stud1是派生類 return0;}修改例11.3的程序,改為公用繼承方式。上機(jī)調(diào)試程序,使之能正確運(yùn)行并得到正確地結(jié)果。對(duì)這兩種繼承方式作比較分析,考慮在什么情況下二者不能互相替代。#include<iostream>usingnamespacestd;classStudent{ //聲明基類 public: //基類公用成員 voidget_value(); voiddisplay(); protected://基類保護(hù)成員 intnum; charname[10]; charsex;};voidStudent::get_value(){ cin>>num>>name>>sex;}voidStudent::display(){ cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl;}classStudent1:publicStudent{ //聲明一個(gè)公用派生類 public: voidget_value_1(); voiddisplay1(); private: intage; charaddr[30];};voidStudent1::get_value_1(){ get_value(); cin>>age>>addr;}voidStudent1::display1(){ cout<<"num:"<<num<<endl;//引用基類的保護(hù)成員,合法 cout<<"name:"<<name<<endl;//引用基類的保護(hù)成員,合法 cout<<"sex:"<<sex<<endl;//引用基類的保護(hù)成員,合法 cout<<"age:"<<age<<endl;//引用派生類的私有成員,合法 cout<<"address:"<<addr<<endl;//引用派生類的私有成員,合法}intmain(){ Student1stud1;//stud1是派生類student1類的對(duì)象 stud1.get_value_1();//調(diào)用派生類對(duì)象stud1的公用成員函數(shù)get_value_1 stud1.display1();//調(diào)用派生類對(duì)象stud1的公用成員函數(shù)display1 return0;}有以下程序結(jié)構(gòu),請(qǐng)分析訪問屬性。在main函數(shù)中能否用b1.i,b1.j和b1.k引用派生類B對(duì)象b1中基類A的成員?派生類B中的成員函數(shù)能否調(diào)用基類A中的成員函數(shù)f1和f2?派生類B中的成員函數(shù)能否引用基類A中的數(shù)據(jù)成員i,j和k?能否在main函數(shù)中用c1.i,c1.j,c1.k,c1.m,c1.n和c1.p引用基類A的成員i,j和k,派生類B的成員m和n,以及派生類C的成員P?能否在main函數(shù)中用c1.f1(),c1.f2(),c1.f3()和c1.f4()調(diào)用f1,f2,f3和f4成員函數(shù)?派生類C的成員函數(shù)f4能否調(diào)用基類A中的成員函數(shù)f1,f2和派生類中的成員函數(shù)f3?classA{ //A為基類 public: voidf1(); inti; protected: voidf2(); intj; private: intk;};classB:publicA{ public: voidf3(); protected: intm; private: intn;//B為A的公用派生類};classC:publicB{ public: voidf4();//C為B的公用派生類 private: intp;};intmain(){ Aa1;//a1是基類A的對(duì)象 Bb1;//b1是派生類B的對(duì)象 Cc1;//c1是派生類C的對(duì)象 return0;}答案總結(jié):(1)類里面能調(diào)用該類或基類的非私有成員變量或成員函數(shù)。(2)類外對(duì)象能調(diào)用該類或基類的公有成員變量或成員函數(shù)。有以下程序結(jié)構(gòu),請(qǐng)分析所有成員在各類的范圍內(nèi)的訪問屬性。#include<iostream>usingnamespacestd;classA{ public: voidf1(); protected: voidf2(); private: inti;};classB:publicA{ public: voidf3(); intk; private: intm;};classC:protectedB{ public: voidf4(); protected: intn; private: intp;};classD:privateC{ public: voidf5(); protected: intq; private: intr;};intmain(){ Aa1; Bb1; Cc1; Dd1; return0;}有如下程序,請(qǐng)完成下面工作:閱讀程度,寫出運(yùn)行時(shí)輸出的結(jié)果。然后上機(jī)運(yùn)行,驗(yàn)證結(jié)果是否正確。分析程序執(zhí)行過程,尤其是調(diào)用構(gòu)造函數(shù)的過程。#include<iostream>usingnamespacestd;classA{ public: A(){ a=0; b=0; } A(inti){ a=i; b=0; } A(inti,intj){ a=i; b=j; } voiddisplay(){ cout<<"a="<<a<<"b="<<b; }private: inta; intb;};classB :publicA{ public: B(){ c=0; } B(inti):A(i){ c=0; } B(inti,intj):A(i,j){ c=0; } B(inti,intj,intk):A(i,j){ c=k; } voiddisplay1(){ display(); cout<<"c="<<c<<endl; } private: intc;};intmain(){ Bb1; Bb2(1); Bb3(1,3); Bb4(1,3,5); b1.display1(); b2.display1(); b3.display1(); b4.display1(); return0;}運(yùn)行結(jié)果如下:a=0b=0c=0a=1b=0c=0a=1b=3c=0a=1b=3c=5有以下程序,請(qǐng)完成下面工作:閱讀程序,寫出運(yùn)行時(shí)輸出的結(jié)果。然后上機(jī)運(yùn)行,驗(yàn)證結(jié)果是否正確。分析程序執(zhí)行過程,尤其是調(diào)用構(gòu)造函數(shù)和析構(gòu)函數(shù)的過程。#include<iostream>usingnamespacestd;classA{ public: A(){ cout<<"constructingA"<<endl; }~A(){ cout<<"destructingA"<<endl; }};classB :publicA{ public: B(){ cout<<"constructingB"<<endl; }~B(){ cout<<"destructingB"<<endl; }};classC :publicB{ public: C(){ cout<<"constructingC"<<endl; }~C(){ cout<<"destructingC"<<endl; }};intmain(){ Cc1; return0;}分別聲明Teacher(教師)類和Cadre(干部)類,采用多重繼承方式由這兩個(gè)類派生出新類Teacher_Cadre(教師兼干部)類。要求:在兩個(gè)基類中都包含姓名、年齡、性別、地址和電話等數(shù)據(jù)成員。在Teacher類中還包含數(shù)據(jù)成員title(稱職),在Cadre類中好包含數(shù)據(jù)成員post(職務(wù))。在Teacher_Cadre類中還包含數(shù)據(jù)成員wages(工資)。對(duì)兩個(gè)基類中的姓名、年齡、性別、地址和電話等數(shù)據(jù)成員用相同的名字,在引用這些數(shù)據(jù)成員時(shí),指定作用域。在類體中聲明成員函數(shù),在類外定義成員函數(shù)。在派生類Tracher_Cadre的成員函數(shù)show中調(diào)用Teacher類中的display函數(shù),輸出姓名、年齡、性別、職稱、地址和電話,然后再用cout語句輸出職務(wù)與工資。#include<string>#include<iostream>usingnamespacestd;classTeacher{ public: Teacher(stringnam,inta,chars,stringtit,stringad,stringt); voiddisplay(); protected: stringname; intage; charsex; stringtitle; stringaddr; stringtel;};Teacher::Teacher(stringnam,inta,chars,stringtit,stringad,stringt): name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){}voidTeacher::display(){ cout<<"name:"<<name<<endl; cout<<"age"<<age<<endl; cout<<"sex:"<<sex<<endl; cout<<"title:"<<title<<endl; cout<<"address:"<<addr<<endl; cout<<"tel:"<<tel<<endl;}classCadre{ public: Cadre(stringnam,inta,chars,stringp,stringad,string t); voiddisplay(); protected: stringname; intage; charsex; stringpost; stringaddr; stringtel;};Cadre::Cadre(stringnam,inta,chars,stringp,stringad,stringt): name(nam),age(a),sex(s),post(p),addr(ad),tel(t){}voidCadre::display(){ cout<<"name:"<<name<<endl; cout<<"age:"<<age<<endl; cout<<"sex:"<<sex<<endl; cout<<"post:"<<post<<endl; cout<<"a

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論