版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
C++面向?qū)ο蟪绦蛟O(shè)計(jì)上機(jī)考試題庫(kù)一、第一類題目(20道,每題7分,在word中保存代碼并將輸出成果窗口保存)1.定義盒子Box類,規(guī)定具備如下成員:長(zhǎng)、寬、高分別為x,y,z,可設(shè)立盒子形狀;可計(jì)算盒子體積;可計(jì)算盒子表面積。#include<iostream>classBox{private:intx,y,z;intv,s;public:voidint(intx1=0,inty1=0,intz1=0){x=x1;y=y1;z=z1;}voidvolue(){v=x*y*z;}voidarea(){s=2*(x*y+x*z+y*z);}voidshow(){cout<<"x="<<x<<"y="<<y<<"z="<<z<<endl;cout<<"s="<<s<<"v="<<v<<endl;}};voidmain(){Boxa;a.init(2,3,4);a.volue();a.area();a.show();}有兩個(gè)長(zhǎng)方柱,其長(zhǎng)、寬、高分別為:(1)30,20,10;(2)12,10,20。分別求她們體積。編一種基于對(duì)象程序,在類中用帶參數(shù)構(gòu)造函數(shù)。#include<iostream>usingnamespacestd;classBox{public:Box(int,int,int);//帶參數(shù)構(gòu)造函數(shù)intvolume();private:intlength;intwidth;intheight;};Box::Box(intlen,inth,intw){length=len;height=h;width=w;}//Box::Box(intlen,intw,int,h):length(len),height(h),width(w){}intBox::volume(){return(length*width*height);}intmain(){Boxbox1cout<<"Thevolumeofbox1is"<<box1.volume()<<endl;Boxbox2cout<<"Thevolumeofbox2is"<<box2.volume()<<endl;return0;}有兩個(gè)長(zhǎng)方柱,其長(zhǎng)、寬、高分別為:(1)12,20,25;(2)10,30,20。分別求她們體積。編一種基于對(duì)象程序,且定義兩個(gè)構(gòu)造函數(shù),其中一種有參數(shù),一種無(wú)參數(shù)。#include<iostream>usingnamespacestd;classBox{public:Box();Box(intlen,intw,inth):length(len),width(w),height(h){}intvolume();private:intlength;intwidth;intheight;};intBox::volume(){return(length*width*height);}intmain(){Boxbox1cout<<"Thevolumeofbox1is"<<box1.volume()<<endl;Boxbox2cout<<"Thevolumeofbox2is"<<box2.volume()<<endl;return0;}聲明一種類模板,運(yùn)用它分別實(shí)現(xiàn)兩個(gè)整數(shù)、浮點(diǎn)數(shù)和字符比較,求出大數(shù)和小數(shù)。#include<iostream>usingnamespacestd;template<classnumtype>//聲明一種類模板classCompare{public:Compare(numtypea,numtypeb){x=a;y=b;}numtypemax(){return(x>y)?x:y;}numtypemin(){return(x<y)?x:y;}private:numtypex,y;};intmain(){Compare<int>cmp1(3,7);cout<<cmp1.max()<<"istheMaximumoftwointedernumbers."<<endl;cout<<cmp1.min()<<"istheMinimumoftwointedernumbers."<<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;}建立一種對(duì)象數(shù)組,內(nèi)放5個(gè)學(xué)生數(shù)據(jù)(學(xué)號(hào)、成績(jī)),用指針指向數(shù)組首元素,輸出第1,3,5個(gè)學(xué)生數(shù)據(jù)。初值自擬。#include<iostream>usingnamespacestd;classStudent{public:Student(intn,doubles):num(n),score(s){}voiddisplay();private:intnum;doublescore;};voidStudent::display(){cout<<num<<""<<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;}建立一種對(duì)象數(shù)組,內(nèi)放5個(gè)學(xué)生數(shù)據(jù)(學(xué)號(hào)、成績(jī)),設(shè)立一種函數(shù)max,用指向?qū)ο笾羔樧骱瘮?shù)參數(shù),在max函數(shù)中找出5個(gè)學(xué)生中成績(jī)最高者,并輸出其學(xué)號(hào)。初值自擬。#include<iostream>usingnamespacestd;classStudent{public:Student(intn,floats):num(n),score(s){}intnum;floatscore;};voidmain(){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);}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<<arr[k].num<<""<<max_score<<endl;}用new建立一種動(dòng)態(tài)一維數(shù)組,并初始化int[10]={1,2,3,4,5,6,7,8,9,10},用指針輸出,最后銷毀數(shù)組所占空間。#include<iostream>#include<string>usingnamespacestd;voidmain(){int*p;p=newint[10];for(inti=1;i<=10;i++){*(p+i-1)=i;cout<<*(p+i-1)<<"";}cout<<endl;delete[]p;return;}定義一種復(fù)數(shù)類Complex,重載運(yùn)算符“+”,使之能用于復(fù)數(shù)加法運(yùn)算。將運(yùn)算符函數(shù)重載為非成員、非友元普通函數(shù)。編寫(xiě)程序,求兩個(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){returnComplex(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;}定義一種復(fù)數(shù)類Complex,重載運(yùn)算符“+”,“—”,使之能用于復(fù)數(shù)加,減運(yùn)算,運(yùn)算符重載函數(shù)作為Complex類成員函數(shù)。編程序,分別求出兩個(gè)復(fù)數(shù)之和,差。初值自擬。usingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}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;}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();return0;}定義一種復(fù)數(shù)類Complex,重載運(yùn)算符“*”,“/”,使之能用于復(fù)數(shù)乘,除。運(yùn)算符重載函數(shù)作為Complex類成員函數(shù)。編程序,分別求出兩個(gè)復(fù)數(shù)之積和商。初值自擬。提示:兩復(fù)數(shù)相乘計(jì)算公式為:(a+bi)*(c+di)=(ac-bd)+(ad+bc)i。兩復(fù)數(shù)相除計(jì)算公式為:(a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d)i。#include<iostream>usingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei){real=r;imag=i;}Complexoperator*(Complex&c2);Complexoperator/(Complex&c2);voiddisplay();private:doublereal;doubleimag;};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();return0;}定義一種復(fù)數(shù)類Complex,重載運(yùn)算符“+”,使之能用于復(fù)數(shù)加法運(yùn)算。參加運(yùn)算兩個(gè)運(yùn)算量可以都是類對(duì)象,也可以其中有一種是整數(shù),順序任意。例如:c1+c2,i+c1,c1+i均合法(設(shè)i為整數(shù),c1,c2為復(fù)數(shù))。編程序,分別求兩個(gè)復(fù)數(shù)之和、整數(shù)和復(fù)數(shù)之和。初值自擬。#include<iostream.h>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.h>classMatrix{public:Matrix();friendMatrixoperator+(Matrix&,Matrix&);voidinput();voiddisplay();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;}voidMatrix::input(){cout<<"inputvalueofmatrix:"<<endl;for(inti=0;i<2;i++)for(intj=0;j<3;j++)cin>>mat[i][j];}voidMatrix::display(){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;cout<<endl<<"Matrixc=Matrixa+Matrixb:"<<endl;c.display();return0;}將運(yùn)算符“+”重載為合用于復(fù)數(shù)加法,重載函數(shù)不作為成員函數(shù),而放在類外,作為Complex類友元函數(shù)。初值自擬。#include<iostream.h>classComplex{public:Complex(){real=0;imag=0;}Complex(doubler){real=r;imag=0;}Complex(doubler,doublei){real=r;imag=i;}friendComplexoperator+(Complex&c1,Complex&c2);voiddisplay();private:doublereal;doubleimag;};Complexoperator+(Complex&c1,Complex&c2){returnComplex(c1.real+c2.real,c1.imag+c2.imag);}voidComplex::display(){cout<<"("<<real<<","<<imag<<"i)"<<endl;}intmain(){Complexc1(3,4),c2(5,-10),c3;c3=c1+c2;cout<<"c1=";c1.display();cout<<"c2=";c2.display();cout<<"c1+c2=";c3.display();return0;}定義一種字符串類String,用來(lái)存儲(chǔ)不定長(zhǎng)字符串,重載運(yùn)算符“==”,,用于兩個(gè)字符串等于比較運(yùn)算。初值自擬。#include<iostream.h>#include<string.h>classString{public:String(){p=NULL;}String(char*str);friendbooloperator==(String&string1,String&string2);voiddisplay();private:char*p;};String::String(char*str){p=str;}voidString::display(){cout<<p;}booloperator==(String&string1,String&string2){if(strcmp(string1.p,string2.p)==0)returntrue;elsereturnfalse;}voidcompare(String&string1,String&string2){if(operator==(string1,string2)==1){string1.display();cout<<"=";string2.display();}cout<<endl;}intmain(){Stringstring1("Hello"),string2("Hello");compare(string1,string2);return0;}定義一種字符串類String,用來(lái)存儲(chǔ)不定長(zhǎng)字符串,重載運(yùn)算符"<",用于兩個(gè)字符串不大于比較運(yùn)算。初值自擬。#include<iostream.h>#include<string.h>classString{public:String(){p=NULL;}String(char*str);friendbooloperator<(String&string1,String&string2);voiddisplay();private:char*p;};String::String(char*str){p=str;}voidString::display(){cout<<p;}booloperator<(String&string1,String&string2){if(strcmp(string1.p,string2.p)<0)returntrue;elsereturnfalse;}voidcompare(String&string1,String&string2){if(operator<(string1,string2)==1){string1.display();cout<<"<";string2.display();}cout<<endl;}intmain(){Stringstring1("Book"),string2("Computer");compare(string1,string2);return0;}定義一種字符串類String,用來(lái)存儲(chǔ)不定長(zhǎng)字符串,重載運(yùn)算符">",用于兩個(gè)字符串不不大于比較運(yùn)算。初值自擬。#include<iostream.h>#include<string.h>classString{public:String(){p=NULL;}String(char*str);friendbooloperator>(String&string1,String&string2);voiddisplay();private:char*p;};String::String(char*str){p=str;}voidString::display(){cout<<p;}booloperator>(String&string1,String&string2){if(strcmp(string1.p,string2.p)>0)returntrue;elsereturnfalse;}voidcompare(String&string1,String&string2){if(operator>(string1,string2)==1){string1.display();cout<<">";string2.display();}cout<<endl;}intmain(){Stringstring1("Hello"),string2("Book");compare(string1,string2);return0;}定義一種描述學(xué)生基本狀況類,數(shù)據(jù)成員涉及姓名、學(xué)號(hào)、C++成績(jī)、英語(yǔ)和數(shù)學(xué)成績(jī),成員函數(shù)涉及輸出數(shù)據(jù),求出總成績(jī)和平均成績(jī)。數(shù)據(jù)自擬。#include"string.h"#include<iostream.h>classCStuScore{public: char strName[12]; char strStuNO[9]; void SetScore(charsname[12],charNO[9],floats0,floats1,floats2) {strcpy(strName,sname); strcpy(strStuNO,NO); fScore[0]=s0; fScore[1]=s1; fScore[2]=s2;} void print() {cout<< cout<<"姓名:"<<strName;cout<<"學(xué)號(hào):"<<strStuNO;cout<<"C++成績(jī):"<<fScore[0]<<"英語(yǔ)成績(jī):"<<fScore[1]<<"數(shù)學(xué)成績(jī):"<<fScore[2]<<endl;} floatGetSUM(){return(float)((fScore[0]+fScore[1]+fScore[2]));}float GetAverage();private: float fScore[3]; }; floatCStuScore::GetAverage(){return(float)((fScore[0]+fScore[1]+fScore[2])/3.0);}voidmain(){ CStuScoreone; floata,b,c; char Name[12]; char StuNO[9];cout<<"姓名:";cin>>Name;cout<<"學(xué)號(hào):";cin>>StuNO;cout<<"成績(jī)1:"<<"成績(jī)2:"<<"成績(jī)3:"<<"\n";cin>>a>>b>>c;one.SetScore(Name,StuNO,a,b,c);one.print();cout<<"平均成績(jī)?yōu)?<<one.GetAverage()<<"\n";cout<<"總成績(jī)"<<one.GetSUM()<<"\n";}先建立一種Point(點(diǎn))類,包括數(shù)據(jù)成員x,y(坐標(biāo)點(diǎn))。以它為基類,派生出一種Circle(圓)類,增長(zhǎng)數(shù)據(jù)成員r(半徑),再以Circle類為直接基類,派生出一種Cylinder(圓柱體)類,在增長(zhǎng)數(shù)據(jù)成員h(高)。編寫(xiě)程序,重載運(yùn)算符“<<”和“>>”,使之可以用于輸出以上類對(duì)象。#include<iostream.h>classPoint{public:Point(float=0,float=0);voidsetPoint(float,float);floatgetX()const{returnx;}floatgetY()const{returny;}friendostream&operator<<(ostream&,constPoint&);protected:floatx,y;};Point::Point(floata,floatb){x=a;y=b;}voidPoint::setPoint(floata,floatb){x=a;y=b;}ostream&operator<<(ostream&output,constPoint&p){output<<"["<<p.x<<","<<p.y<<"]"<<endl;returnoutput;}classCircle:publicPoint{public:Circle(floatx=0,floaty=0,floatr=0);voidsetRadius(float);floatgetRadius()const;floatarea()const;friendostream&operator<<(ostream&,constCircle&);protected:floatradius;};Circle::Circle(floata,floatb,floatr):Point(a,b),radius(r){}voidCircle::setRadius(floatr){radius=r;}floatCircle::getRadius()const{returnradius;}floatCircle::area()const{return3.14159*radius*radius;}ostream&operator<<(ostream&output,constCircle&c){output<<"Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area()<<endl;returnoutput;}classCylinder:publicCircle{public:Cylinder(floatx=0,floaty=0,floatr=0,floath=0);voidsetHeight(float);floatgetHeight()const;floatarea()const;floatvolume()const;friendostream&operator<<(ostream&,constCylinder&);protected:floatheight;};Cylinder::Cylinder(floata,floatb,floatr,floath):Circle(a,b,r),height(h){}voidCylinder::setHeight(floath){height=h;}floatCylinder::getHeight()const{returnheight;}floatCylinder::area()const{return2*Circle::area()+2*3.14159*radius*height;}floatCylinder::volume()const{returnCircle::area()*height;}ostream&operator<<(ostream&output,constCylinder&cy){output<<"Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height<<"\narea="<<cy.area()<<",volume="<<cy.volume()<<endl;returnoutput;}intmain(){Cylindercy1(3.5,6.4,5.2,10);cout<<"\noriginalcylinder:\nx="<<cy1.getX()<<",y="<<cy1.getY()<<",r="<<cy1.getRadius()<<",h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<",volume="<<cy1.volume()<<endl;cy1.setHeight(15);cy1.setRadius(7.5);cy1.setPoint(5,5);cout<<"\nnewcylinder:\n"<<cy1;Point&pRef=cy1;cout<<"\npRefasapoint:"<<pRef;Circle&cRef=cy1;cout<<"\ncRefasaCircle:"<<cRef;return0;}寫(xiě)一種程序,定義抽象類型Shape,由她派生三個(gè)類:Circle(圓形),Rectangle(矩形),Trapezoid(梯形),用一種函數(shù)printArea分別輸出三者面積,3個(gè)圖形數(shù)據(jù)在定義對(duì)象是給定。#include<iostream>usingnamespacestd;classShape{public:virtualdoublearea()const=0;};classCircle:publicShape{public:Circle(doubler):radius(r){}virtualdoublearea()const{return3.14159*radius*radius;};protected:doubleradius;};classRectangle:publicShape{public:Rectangle(doublew,doubleh):width(w),height(h){}virtualdoublearea()const{returnwidth*height;}protected:doublewidth,height;};classTrapezoid:publicShape{public:Trapezoid(doublew,doubleh,doublelen):width(w),height(h),length(len){}virtualdoublearea()const{return0.5*height*(width+length);}protected:doublewidth,height,length;};voidprintArea(constShape&s){cout<<s.area()<<endl;}intmain(){Circlecircle(12.6);cout<<"areaofcircle=";printArea(circle);Rectanglerectangle(4.5,8.4);cout<<"areaofrectangle=";printArea(rectangle);Trapezoidtrapezoid(4.5,8.4,8.0);cout<<"areaoftrapezoid=";printArea(trapezoid);return0;}定義一種人員類Cperson,涉及數(shù)據(jù)成員:姓名、編號(hào)、性別和用于輸入輸出成員函數(shù)。在此基本上派生出學(xué)生類CStudent(增長(zhǎng)成績(jī))和教師類Cteacher(增長(zhǎng)教齡),并實(shí)現(xiàn)對(duì)學(xué)生和教師信息輸入輸出。#include<iostream.h>#include<string.h>classCPerson {public: voidSetData(char*name,char*id,boolisman=1) {intn=strlen(name); strncpy(pName,name,n); pName[n]='\0'; n=strlen(id); strncpy(pID,id,n); pID[n]='\0'; bMan=isman; } voidOutput() { cout<<"姓名:"<<pName<<endl; cout<<"編號(hào):"<<pID<<endl; char*str=bMan?"男":"女"; cout<<"性別:"<<str<<endl; }private: charpName[20]; charpID[20]; boolbMan; };classCStudent:publicCPerson {public: voidInputScore(doublescore1,doublescore2,doublescore3) { dbScore[0]=score1; dbScore[1]=score2; dbScore[2]=score3; } voidPrint() { Output(); for(inti=0;i<3;i++) cout<<"成績(jī)"<<i+1<<":"<<dbScore[i]<<endl; }private: doubledbScore[3]; };classCteacher:publicCPerson {public: voidInputage(doubleage) { tage=age; } voidPrint() { Output(); cout<<"教齡:"<<tage<<endl; }private: doubletage; };voidmain() { CStudentstu; Cteachertea; stu.SetData("LiMing","21010211"); stu.InputScore(80,76,91); stu.Print(); tea.SetData("zhangli","001"); tea.Inputage(12); tea.Print();}二、第二類題目(20道,每題9分,請(qǐng)自行設(shè)計(jì)輸出格式)1.某商店經(jīng)銷一種貨品,貨品成箱購(gòu)進(jìn),成箱賣出,購(gòu)進(jìn)和賣出時(shí)以重量為單位,各箱重量不同樣,因而,商店需要記下當(dāng)前庫(kù)存貨品總量,規(guī)定把商店貨品購(gòu)進(jìn)和賣出狀況模仿出來(lái)。#include<iostream>usingnamespacestd;classGoods{public:Goods(intw){weight=w;totalWeight+=w;}; ~Goods(){totalWeight-=weight;}; intWeight(){returnweight;};staticintTotalWeight(){returntotalWeight;}; private:intweight; staticinttotalWeight; };intGoods::totalWeight=0; main(){intw;cin>>w;Goods*g1=newGoods(w); cin>>w;Goods*g2=newGoods(w);cout<<Goods::TotalWeight()<<endl; deleteg2; cout<<Goods::TotalWeight()<<endl;}2.設(shè)計(jì)一種Time類,涉及三個(gè)私有數(shù)據(jù)成員:hour,minute,sec,用構(gòu)造函數(shù)初始化,內(nèi)設(shè)公用函數(shù)display(Date&d),設(shè)計(jì)一種Date類,涉及三個(gè)私有數(shù)據(jù)成員:month,day,year,也用構(gòu)適函數(shù)初始化;分別定義兩個(gè)帶參數(shù)對(duì)象t1(12,30,55),d1(3,25,),通過(guò)友員成員函數(shù)應(yīng)用,輸出d1和t1值。#include<iostream>usingnamespacestd;classDate;classTime{public:Time(int,int,int);voiddisplay(constDate&);private:inthour;intminute;intsec;};Time::Time(inth,intm,ints){hour=h;minute=m;sec=s;}classDate{public:Date(int,int,int);friendvoidTime::display(constDate&);private:intmonth;intday;intyear;};Date::Date(intm,intd,inty){month=m;day=d;year=y;}voidTime::display(constDate&da){cout<<da.month<<"/"<<da.day<<"/"<<da.year<<endl;cout<<hour<<":"<<minute<<":"<<sec<<endl;}intmain(){Timet1(12,30,55);Dated1(3,25,);t1.display(d1);return0;}3.設(shè)計(jì)一種Time類,涉及三個(gè)私有數(shù)據(jù)成員:hour,minute,sec,用構(gòu)造函數(shù)初始化,,設(shè)計(jì)一種Date類,涉及三個(gè)私有數(shù)據(jù)成員:month,day,year,也用構(gòu)適函數(shù)初始化;設(shè)計(jì)一種普通函數(shù)display(…),將display分別設(shè)立為T
ime類和Date類友元函數(shù),在主函數(shù)中分別定義兩個(gè)帶參數(shù)對(duì)象t1(12,30,55),d1(3,25,),調(diào)用desplay,輸出年、月、日和時(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,30,55);Dated1(3,25,);display(d1,t1);return0;}4.可以定義點(diǎn)類(Point),再定義一種類(Distance)描述兩點(diǎn)之間距離,其數(shù)據(jù)成員為兩個(gè)點(diǎn)類對(duì)象,兩點(diǎn)之間距離計(jì)算可設(shè)計(jì)由構(gòu)造函數(shù)來(lái)實(shí)現(xiàn)。#include<iostream>#include<math.h>usingnamespacestd;classPoint{public:Point(doublexi,doubleyi){X=xi;Y=yi;} doubleGetX(){returnX;}doubleGetY(){returnY;}private:doubleX,Y;};classDistance{public: Distance(Pointp,Pointq); doubleGetdist(){returndist;}private: Pointa,b; doubledist;};Distance::Distance(Pointq1,Pointq2):a(q1),b(q2){doubledx=double(a.GetX()-b.GetX());doubledy=double(a.GetY()-b.GetY());dist=sqrt(dx*dx+dy*dy);}intmain(){Pointp1(3.0,5.0),p2(4.0,6.0);Distancedis(p1,p2);cout<<"Thisdistanceis:"<<dis.Getdist()<<endl;return0;}5.定義點(diǎn)類(Point),再定義一種函數(shù)(Distance)描述兩點(diǎn)之間距離,其數(shù)據(jù)成員為兩個(gè)點(diǎn)類對(duì)象,將兩點(diǎn)之間距離函數(shù)聲明為Point類友元函數(shù)。#include<iostream>#include<math.h>usingnamespacestd;classPoint{public:Point(doublexi,doubleyi){X=xi;Y=yi;} doubleGetX(){returnX;}doubleGetY(){returnY;}frienddoubleDistance(Point&a,Point&b); private:doubleX,Y;};doubleDistance(Point&a,Point&b){doubledx=a.X-b.X; doubledy=a.Y-b.Y;returnsqrt(dx*dx+dy*dy);}intmain(){Pointp1(3.0,5.0),p2(4.0,6.0);doubled=Distance(p1,p2);cout<<"Thisdistanceis"<<d<<endl;return0;}6.實(shí)現(xiàn)重載函數(shù)Double(x),返回值為輸人參數(shù)兩倍;參數(shù)分別為整型、浮點(diǎn)型、雙精度型,返回值類型與參數(shù)同樣。(用類模板實(shí)現(xiàn))#include<iostream>usingnamespacestd;template<classnumtype>classDouble{public: Double(numtypea) {x=a;} numtypebei() {return2*x;} private: numtypex;};intmain(){Double<int>dou1(3);cout<<dou1.bei()<<endl;Double<float>dou2(12.36);cout<<dou2.bei()<<endl;Double<double>dou3(25.33333);cout<<dou3.bei()<<endl;return0;}7.有一種Time類,包括數(shù)據(jù)成員minute(分)和sec(秒),模仿秒表,每次走一秒,滿60秒進(jìn)一分鐘,此時(shí)秒又從0開(kāi)始算。規(guī)定輸出分和秒值。初值自擬。#include<iostream>usingnamespacestd;classTime{public:Time(){minute=0;sec=0;}Time(intm,ints):minute(m),sec(s){}Timeoperator++();voiddisplay(){cout<<minute<<":"<<sec<<endl;}private:intminute;intsec;};TimeTime::operator++(){if(++sec>=60){sec-=60;++minute;}return*this;}intmain(){Timetime1(34,0);for(inti=0;i<61;i++) {++time1;time1.display();}return0;}8.聲明一種教師(Teacher)類和一種學(xué)生(Student)類,用多重繼承方式聲明一種研究生(Graduate)派生類。教師類中涉及數(shù)據(jù)成員name(姓名),age(年齡),title(職稱)。學(xué)生類中涉及數(shù)據(jù)成員name(姓名),age(年齡),score(成績(jī))。在定義派生類對(duì)象時(shí)給出初始化數(shù)據(jù)(自已定),然后輸出這些數(shù)據(jù)。初值自擬。#include<iostream>#include<string>usingnamespacestd;classTeacher{public:Teacher(stringnam,inta,stringt){name=nam;age=a;title=t;}voiddisplay(){cout<<"name:"<<name<<endl;cout<<"age"<<age<<endl;cout<<"title:"<<title<<endl;}protected:stringname;intage;stringtitle;};classStudent{public:Student(stringnam,chars,floatsco){name1=nam;sex=s;score=sco;}voiddisplay1(){cout<<"name:"<<name1<<endl;cout<<"sex:"<<sex<<endl;cout<<"score:"<<score<<endl;}protected:stringname1;charsex;floatscore;};classGraduate:publicTeacher,publicStudent{public:Graduate(stringnam,inta,chars,stringt,floatsco,floatw):Teacher(nam,a,t),Student(nam,s,sco),wage(w){}voidshow(){cout<<"name:"<<name<<endl;cout<<"age:"<<age<<endl;cout<<"sex:"<<sex<<endl;cout<<"score:"<<score<<endl;cout<<"title:"<<title<<endl;cout<<"wages:"<<wage<<endl;}private:floatwage;};intmain(){Graduategrad1("Wang-li",24,'f',"assistant",89.5,1234.5);grad1.show();return0;}9.在上題基本上,在Teacher類和Student類之上增長(zhǎng)一種共同基類Person,如下圖所示。作為人員某些基本數(shù)據(jù)都放在Person中,在Teacher類和Student類中再增長(zhǎng)某些必要數(shù)據(jù)(Student類中增長(zhǎng)score,Teacher類中增長(zhǎng)職稱title,Graduate類中增長(zhǎng)工資wages)。初值自擬。PersonPersonStudentStudentTeacherTeacherGraduateGraduate#include<iostream>#include<string>usingnamespacestd;classPerson{public:Person(char*nam,chars,inta){strcpy(name,nam);sex=s;age=a;}protected:charname[20];charsex;intage;};classTeacher:virtualpublicPerson{public:Teacher(char*nam,chars,inta,char*t):Person(nam,s,a){strcpy(title,t);}protected:chartitle[10];};classStudent:virtualpublicPerson{public:Student(char*nam,chars,inta,floatsco):Person(nam,s,a),score(sco){}protected:floatscore;};classGraduate:publicTeacher,publicStudent{public:Graduate(char*nam,chars,inta,char*t,floatsco,floatw):Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w){}voidshow(){cout<<"name:"<<name<<endl;cout<<"age:"<<age<<endl;cout<<"sex:"<<sex<<endl;cout<<"score:"<<score<<endl;cout<<"title:"<<title<<endl;cout<<"wages:"<<wage<<endl;}private:floatwage;};intmain(){Graduategrad1("Wang-li",'f',24,"assistant",89.5,1234.5);grad1.show();return0;}寫(xiě)一種程序,定義抽象類型Shape,由她派生三個(gè)類:Circle(圓形),Rectangle(矩形),Trapezoid(梯形),用一種函數(shù)printArea分別輸出三者面積,3個(gè)圖形數(shù)據(jù)在定義對(duì)象是給定。#include<iostream>usingnamespacestd;classShape{public:virtualdoublearea()const=0;//純虛函數(shù)};classCircle:publicShape{public:Circle(doubler):radius(r){}virtualdoublearea()const{return3.14159*radius*radius;};protected:doubleradius;};classRectangle:publicShape{public:Rectangle(doublew,doubleh):width(w),height(h){}virtualdoublearea()const{returnwidth*height;}protected:doublewidth,height;};classTrapezoid:publicShape{public:Trapezoid(doublew,doubleh,doublelen):width(w),height(h),length(len){}virtualdoublearea()const{return0.5*height*(width+length);}protected:doublewidth,height,length;};voidprintArea(constShape&s){cout<<s.area()<<endl;}intmain(){Circlecircle(12.6);cout<<"areaofcircle=";printArea(circle);Rectanglerectangle(4.5,8.4);cout<<"areaofrectangle=";printArea(rectangle);Trapezoidtrapezoid(4.5,8.4,8.0);cout<<"areaoftrapezoid=";printArea(trapezoid);return0;}聲明一種Shape抽象類,在此基本上派生出Rectangle和Circle類,兩者均有GetArea()函數(shù)計(jì)算對(duì)象面積,GetPerim()函數(shù)計(jì)算對(duì)象周長(zhǎng)。#include<iostream>usingnamespacestd;classShape{public:Shape(){}~Shape(){}virtualfloatGetPerim()=0;virtualfloatGetArea()=0;};classRectangle:publicShape{public:Rectangle(floati,floatj):L(i),W(j){}~Rectangle(){}floatGetPerim(){return2*(L+W);}floatGetArea(){returnL*W;}private:floatL,W;};classCircle:publicShape{pub
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 綠色學(xué)校評(píng)審方案
- 康復(fù)項(xiàng)目服務(wù)方案
- 江西省南昌市南昌縣蓮塘第一中學(xué)2024-2025學(xué)年高二上學(xué)期期中考試物理試題(含答案)
- 農(nóng)村初中家校社協(xié)同育人的問(wèn)題及對(duì)策
- 化工公司策劃書(shū)
- 地方公務(wù)員江蘇申論99
- 膽囊息肉臨床路徑標(biāo)準(zhǔn)住院流程
- 湖北公務(wù)員面試模擬58
- 法定代表人失蹤委托書(shū)范本
- 天津申論模擬86
- 第9課秋冬的色彩(課件)(共13張PPT)贛美版美術(shù)三年級(jí)上冊(cè)
- 奧派物業(yè)管理軟件手冊(cè)
- 教師資格考試《小學(xué)數(shù)學(xué)專業(yè)面試》
- 三類汽車專項(xiàng)維修業(yè)戶主要作業(yè)設(shè)備一覽表【范本模板】
- 2022年楚雄彝族自治州醫(yī)療系統(tǒng)事業(yè)編制鄉(xiāng)村醫(yī)生招聘筆試試題及答案解析
- 下肢骨折教學(xué)幻燈詳解
- GB/T 9808-2008鉆探用無(wú)縫鋼管
- GB/T 3452.1-2005液壓氣動(dòng)用O形橡膠密封圈第1部分:尺寸系列及公差
- GB/T 30317-2013地理空間框架基本規(guī)定
- 六年級(jí)英語(yǔ)上冊(cè) Unit 8《The Universe》(Part B)2 閩教版
- 不對(duì)稱高壓脈沖軌道電路講義課件
評(píng)論
0/150
提交評(píng)論