C++面向?qū)ο蟪绦蛟O(shè)計(jì)上機(jī)考試題庫(kù)_第1頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)上機(jī)考試題庫(kù)_第2頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)上機(jī)考試題庫(kù)_第3頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)上機(jī)考試題庫(kù)_第4頁(yè)
C++面向?qū)ο蟪绦蛟O(shè)計(jì)上機(jī)考試題庫(kù)_第5頁(yè)
已閱讀5頁(yè),還剩43頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

C++面對(duì)對(duì)象程序設(shè)計(jì)上機(jī)考試題庫(kù)

一、第一類(lèi)題目(20道,每題7分,在word中保留代碼并將輸出結(jié)果窗口保留)

1.定義盒子Box類(lèi),要求具有以下成員:長(zhǎng)、寬、高分別為x,y,z,可設(shè)置盒子形態(tài);

可計(jì)算盒子體積;可計(jì)算盒子的表面積。

#include<iostream>

classBox

{private:

intx,y,z;intv,s;

public:

voidint(intxl=0,intyl=0,intzl=0){x=xl;y=yl;z=zl;}

voidvolue0{v=x*y*z;}

voidarea(){s=2*(x*y+x*z+y*z);}

voidshow()

{cout?/zx=z/<<x?,zy=/z<<y?z,z='/<<z?endl;

cout<<z,s=v=z,<<v<<endl;

)

);

voidmainO

{Boxa;

a.init(2,3,4);

a.volue();

a.area();

a.show();

)

2.有兩個(gè)長(zhǎng)方柱,其長(zhǎng)、寬、高分別為:(1)30,20,10;(2)12,10,20。分別求

他們的體積。編一個(gè)基于對(duì)象的程序,在類(lèi)中用帶參數(shù)的構(gòu)造函數(shù)。

#include<iostream>

usingnamespacestd;

classBox

{public:

Box(int,int,int);〃帶參數(shù)的構(gòu)造函數(shù)

intvolume0;

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()

(

Boxboxl(30,20,10);

cout<<,,Thevolumeofboxlis/z<<boxl.volume()?endl;

Boxbox2(12,10,20);

cout<<,,Thevolumeofbox2is/z<<box2.volume()<<endl;

return0;

}

3.有兩個(gè)長(zhǎng)方柱,其長(zhǎng)、寬、高分別為:(1)12,20,25;(2)10,30,20。分別求

他們的體積。編一個(gè)基于對(duì)象的程序,且定義兩個(gè)構(gòu)造函數(shù),其中一個(gè)有參數(shù),一個(gè)無(wú)

參數(shù)。

^include<iostream>

usingnamespacestd;

classBox

{public:

Box();

Box(intlen,intw,inth):length(len),width(w),height(h){}

intvolume0;

private:

intlength;

intwidth;

intheight;

};

intBox::volume()

{return(length*width*height);

}

intmain()

(

Boxboxl(10,20,25);

cout?z/Thevolumeofboxlis,,<<boxl.volume0<<endl;

Boxbox2(10,30,20);

cout<<z/Thevolumeofbox2isz,<<box2.volume()<<endl;

return0;

4.聲明一個(gè)類(lèi)模板,利用它分別實(shí)現(xiàn)兩個(gè)整數(shù)、浮點(diǎn)數(shù)和字符的比較,求出大數(shù)和小

數(shù)。

^include<iostream>

usingnamespacestd;

template<classnumtype>〃聲明一個(gè)類(lèi)模板

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>cmpl(3,7);

cout<<cmpl.max()<<z/istheMaximumoftwointedernumbers.,z?endl;

cout<<cmpl.min0<<,zistheMinimumoftwointeder

numbers.zz<<endl<<endl;

Compare<float>cmp2(45.78,93.6);

cout<<cmp2.max()<<,zistheMaximumoftwofloatnumbers.z,?endl;

cout<<cmp2.min()<<z,istheMinimumoftwofloatnumbers.,,<<endl?endl;

Compare<char>cmp3('a','A');

cout<<cmp3.max()<<z,istheMaximumoftwocharacters./z<<endl;

cout<<cmp3.min()<<z,istheMinimumoftwocharacters.z\<endl;

return0;

5.建立一個(gè)對(duì)象數(shù)組,內(nèi)放5個(gè)學(xué)生的數(shù)據(jù)(學(xué)號(hào)、成果),用指針指向數(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;

)

6.建立一個(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)。

初值自擬。

Sinclude<iostream>

usingnamespacestd;

classStudent

{public:

Student(intn,floats):num(n),score(s){}

intnum;

floatscore;

voidmainO

{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=l;i<5;i++)

if(arr[i].score>max_score){max_score=arr[i].score;k=i;}

cout<<arr[k].num<<,zz,<<max_score<<endl;

7.用new建立一個(gè)動(dòng)態(tài)一維數(shù)組,并初始化5"10]={1,2,3,4,5,6,7,8,9,10},用指針

輸出,最終銷(xiāo)毀數(shù)組所占空間。

#include<iostream>

#include<string>

usingnamespacestd;

voidmain(){

int*p;

p=newint[10];

for(inti=l;i<=10;i++)

*(p+i-l)=i;

cout〈〈*(p+iT)<<〃〃;

}

cout<<endl;

delete[]p;

return;

)

8.定義一個(gè)復(fù)數(shù)類(lèi)Complex,重載運(yùn)算符“+”,使之能用于復(fù)數(shù)的加法運(yùn)算。將運(yùn)算

符函數(shù)重載為非成員、非友元的一般函數(shù)。編寫(xiě)程序,求兩個(gè)復(fù)數(shù)之和。初值自擬。

ttinclude<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<<z,(,,?real?,z,/,<<imag<<,,i)z,<<endl;}

Complexoperator+(Complex&cl,Complex&c2)

(

return

Complex(cl.get_real()+c2?get_real(),cl.get_imag()+c2?get_imag());

)

intmain()

{Complexcl(3,4),c2(5,-10),c3;

c3=cl+c2;

cout〈<〃c3=〃;

c3.display();

return0;

)

9.定義一個(gè)復(fù)數(shù)類(lèi)Complex,重載運(yùn)算符“+”,“一”,使之能用于復(fù)數(shù)的加,減運(yùn)

算,運(yùn)算符重載函數(shù)作為Complex類(lèi)的成員函數(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<<〃〃<<imag<X〃i)〃<<endl;}

intmain()

{Complexcl(3,4),c2(5,-10),c3;

c3=cl+c2;

cout<<,,cl+c2=//;

c3.display();

c3=cl-c2;

cout<<,,cl-c2=zz;

c3.display();

return0;

)

10.定義一個(gè)復(fù)數(shù)類(lèi)Complex,重載運(yùn)算符“*”,“/",使之能用于復(fù)數(shù)的乘,除。

運(yùn)算符重載函數(shù)作為Complex類(lèi)的成員函數(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?z,,/,<<imag<<,,i),z<<endl;}

intmain()

{Complexcl(3,4),c2(5,-10),c3;

c3=cl*c2;

cout<〈〃cl*c2二〃;

c3.display();

c3=cl/c2;

cout?,,cl/c2=,/;

c3.display();

return0;

}

11.定義一個(gè)復(fù)數(shù)類(lèi)Complex,重載運(yùn)算符“+”,使之能用于復(fù)數(shù)的加法運(yùn)算。參加

運(yùn)算的兩個(gè)運(yùn)算量可以都是類(lèi)對(duì)象,也可以其中有一個(gè)是整數(shù),依次隨意。例如:

cl+c2,i+cl,cl+i均合法(設(shè)i為整數(shù),cl,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?,z,//<<imag<<,,i)z,<<endl;}

Complexoperator+(int&i,Complex&c)

{returnComplex(i+c.real,c.imag);}

intmain()

{Complexcl(3,4),c2(5,-10),c3;

inti=5;

c3=cl+c2;

cout<<,,cl+c2=z,;

c3.display();

c3=i+cl;

cout<<,,i+cl=,z;

c3.display();

c3=cl+i;

cout<<z,cl+i=,z;

c3.display0;

return0;

)

12.有兩個(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<<,zinputvalueofmatrix:"z<<endl;

for(inti=0;i<2;i++)

for(intj=0;j<3;j++)

cin?mat[i]Lj];

)

voidMatrix::display()

{for(inti=0;i<2;i++)

{for(intj=0;j<3;j++)

{cout?mat[i][j]<<z,z,;}

cout?endl;}

)

intmain()

{Matrixa,b,c;

a.input();

b.input();

cout<<endl<<,,Matrixa:,,?endl;

a.display();

cout<<endl<<z,Matrixb:,z?endl;

b.display();

c=a+b;

cout<<endl<<,,Matrixc=Matrixa+Matrixb:〃<<endl;

c.display();

return0;

)

13.將運(yùn)算符“十”重載為適用于復(fù)數(shù)加法,重載函數(shù)不作為成員函數(shù),而放在類(lèi)外,

作為Complex類(lèi)的友元函數(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&cl,Complex&c2);

voiddisplay0;

private:

doublereal;

doubleimag;

};

Complexoperator+(Complex&cl,Complex&c2)

(

returnComplex(cl.real+c2.real,cl.imag+c2.imag);

)

voidComplex::display()

{cout<<z,(,,?real?,z,z,?imag<<z,i)zz<<endl;}

intmain()

{Complexc1(3,4),c2(5,-10),c3;

c3=cl+c2;

cout<<,,cl=/z;cl.display();

cout<〈〃c2=〃;c2.display();

cout<<,,cl+c2=/z;c3.display();

return0;

)

14.定義一個(gè)字符串類(lèi)String,用來(lái)存放不定長(zhǎng)的字符串,重載運(yùn)算符“==",,用

于兩個(gè)字符串的等于比較運(yùn)算。初值自擬。

#include<iostream.h>

ttinclude<string.h>

classString

{public:

String0{p=NULL;}

String(char*str);

friendbooloperator==(String&stringl,String&string2);

voiddisplay();

private:

char*p;

);

String::String(char*str)

{p=str;

}

voidString::display()

{cout<<p;}

booloperator==(String&stringl,String&string2)

{if(strcmp(stringl.p,string2.p)==0)

returntrue;

else

returnfalse;

)

voidcompare(String&stringl,String&string2)

{if(operator==(stringl,string2)==l)

{stringl.display();cout<</z=zz;string2.display0;}

cout<<endl;

)

intmainO

{Stringstringl("Hello"),string2("Hello");

compare(stringl,string2);

return0;

)

15.定義一個(gè)字符串類(lèi)String,用來(lái)存放不定長(zhǎng)的字符串,重載運(yùn)算符〃<〃,用于兩個(gè)字

符串的小于的比較運(yùn)算。初值自擬。

^include<iostream.h>

#include<string.h>

classString

{public:

String(){p=NULL;}

String(char*str);

friendbooloperator<(String&stringl,String&string2);

voiddisplay();

private:

char*p;

};

String::String(char*str)

{p=str;

)

voidString::display()

{cout?p;}

booloperator<(String&stringl,String&string2)

{if(strcmp(stringl.p,string2.p)<0)

returntrue;

else

returnfalse;

voidcompare(String&stringl,String&string2)

{if(operator<(stringl,string2)==1)

{stringl.display();cout?z/<z,;string2.display();}

cout<<endl;

)

intmain()

{Stringstringl(/,Book,/),string2("Computer");

compare(stringl,string2);

return0;

)

16.定義一個(gè)字符串類(lèi)String,用來(lái)存放不定長(zhǎng)的字符串,重載運(yùn)算符〃>〃,用于兩個(gè)

字符串的大于的比較運(yùn)算。初值自擬。

ttinclude<iostream.h>

^include<string.h>

classString

{public:

String(){p=NULL;}

String(char*str);

friendbooloperator>(String&stringl,String&string2);

voiddisplay();

private:

char*p;

);

String::String(char*str)

{p=str;

}

voidString::display()

{cout?p;}

booloperator>(String&stringl,String&string2)

{if(strcmp(stringl.p,string2.p)>0)

returntrue;

else

returnfalse;

)

voidcompare(Stringfcstringl,String&string2)

{if(operator>(stringl,string2)==l)

{stringl.display();cout?/z>/z;string2.display();}

cout<<endl;

)

intmain()

{Stringstringl("Hello"),string2(〃Book〃);

compare(stringl,string2);

return0;}

17.定義一個(gè)描述學(xué)生基本狀況的類(lèi),數(shù)據(jù)成員包括姓名、學(xué)號(hào)、C++成果、英語(yǔ)和數(shù)學(xué)

成果,成員函數(shù)包括輸出數(shù)據(jù),求出總成果和平均成果。數(shù)據(jù)自擬。

#includez,string.h〃

Sinclude<iostream.h>

classCStuScore

{public:

charstrName[12];

charstrStuN0[9];

voidSetScore(charsname[12],charN0[9],floatsO,float

si,floats2)

(

strcpy(strName,sname);

strcpy(strStuNO,NO);

fScore[0]=sO;

fScore[l]=si;

fScore[2]=s2;}

voidprint()

{cout?

cout<〈〃姓名:,z<<strName;

cout<X〃學(xué)號(hào):z,<<strStuN0;

cout?z/C++成果:〃《fScore[0"<〃英語(yǔ)成果:〃<<fScore[l]<<〃數(shù)學(xué)成果:

z/<<fScore[2]<<endl;}

floatGetSUMO

{return(float)((fScore[0]+fScore[l]+fScore[2]));}

floatGetAverage();

private:

floatfScore[3];

};

floatCStuScore::GetAverage()

{return(float)((fScore[0]+fScore[l]+fScore[2])/3.0);}

voidmain()

{CStuScoreone;

floata,b,c;

charName[12];

charStuN0[9];

cout<<〃姓名:";

cin>>Name;

cout<<〃學(xué)號(hào):〃;

cin>>StuNO;

cout?z/成果1:?成果2:〃<<〃成果3:〃<<〃\n〃;

cin>>a?b>>c;

one.SetScore(Name,StuNO,a,b,c);

one.print();

cout<X〃平均成果為〃<<one.GetAverage()<<〃\n〃;

cout<〈〃總成果〃<<one.GetSUMO?〃\n〃;}

18.先建立一個(gè)Point(點(diǎn))類(lèi),包含數(shù)據(jù)成員x,y(坐標(biāo)點(diǎn))。以它為基類(lèi),派生出

一個(gè)Circle(圓)類(lèi),增加數(shù)據(jù)成員r(半徑),再以Circle類(lèi)為干脆基類(lèi),派生出一

個(gè)Cylinder(圓柱體)類(lèi),在增加數(shù)據(jù)成員h(高)。編寫(xiě)程序,重載運(yùn)算符和

“>>”,使之能夠用于輸出以上類(lèi)對(duì)象。

ttinclude<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<<,?[,z?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<<,zCenter=x?,z,/z<<c.y?z,],r=,,<<c.radius<<z,,

area=,,<<c.area()<<endl;

returnoutput;

)

classCylinder:publicCircle

{public:

Cylinder(floatx=0,floaty=0,floatr=0,floath=0);

voidsetHeight(float);

floatgetHeight0const;

floatarea()const;

floatvolume()const;

friendostream&operator?(ostream&,constCylinderfc);

protected:

floatheight;

);

Cylinder::Cylinder(floata,floatb,floatr,floath)

:Circle(a,b,r),height(h){}

voidCylinder::setHeight(floath){height=h;}

floatCylinder::getHeight0const{returnheight;}

floatCylinder::area()const

{return2*Circle::area()+2*3.14159*radius*height;}

floatCylinder::volume()const

{returnCircle::area()*height;}

ostream&operator<<(ostream&output,constCylinderfecy)

{output<<,zCenter=[/z<<cy.x<<〃,〃<<cy?y?zz],r=z\<cy.radius?,z,

h=/,<<cy.height

<</,\narea=,,?cy.area()<<,z,volume=,,?cy.volume()<<endl;

returnoutput;

intmain()

{Cylindercyl(3.5,6.4,5.2,10);

cout<<,,\noriginalcylinder:\nx=zz<<cyl.getX()<<,z,y=z/<<cyl.getY()<<〃,r=〃

<<cyl.getRadius()<<〃,h=/,?cyl.getHeight()<<,,\narea=/,?cyl.area()

<<z,,volume=〃<<cyl.volume()?endl;

cyl.setHeight(15);

cyl.setRadius(7.5);

cyl.setPoint(5,5);

cout<<,,\nnewcylinder:\n,z<<cyl;

Point&pRef=cyl;

cout<<z,\npRefasapoint:/z<<pRef;

Circle&cRef=cyl;

cout<<,,\ncRefasaCircle:,,?cRef;

return0;

19.寫(xiě)一個(gè)程序,定義抽象類(lèi)型Shape,由他派生三個(gè)類(lèi):Circle(圓形),Rectangle

(矩形),Trapezoid(梯形),用一個(gè)函數(shù)printArea分別輸出三者的面積,3個(gè)圖形

的數(shù)據(jù)在定義對(duì)象是給定。

ttinclude<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,double

len):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<<z'areaofrectangle=〃;

printArea(rectangle);

Trapezoidtrapezoid(4.5,8.4,8.0);

cout<<,,areaoftrapezoid二〃;

printArea(trapezoid);

return0;

)

20.定義一個(gè)人員類(lèi)Cperson,包括數(shù)據(jù)成員:姓名、編號(hào)、性別和用于輸入輸出的成

員函數(shù)。在此基礎(chǔ)上派生出學(xué)生類(lèi)CStudent(增加成果)和老師類(lèi)Cteacher(增加教齡),

并實(shí)現(xiàn)對(duì)學(xué)生和老師信息的輸入輸出。

#include<iostream.h>

ttinclude<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):z/<<pID?endl;

char*str=bMan?〃男〃:〃女〃;

cout<X〃性別:z,<<str?endl;

)

private:

charpName[20];

charpID[20];

boolbMan;

);

classCStudent:publicCPerson

(

public:

voidInputScore(doublescorel,doublescore2,doublescore3)

{

dbScore[0]=scorel;

dbScore[l]=score2;

dbScore[2]=score3;

)

voidPrint()

{

Output();

for(inti=0;i<3;i++)

cout<<〃成果〃<〃:〃<XdbScore[i]〈〈endl;

}

private:

doubledbScore[3];

);

classCteacher:publicCPerson

(

public:

voidInputage(doubleage)

(

tage=age;

)

voidPrint()

(

Output();

cout<<〃教齡:"z<<tage<<endl;

)

private:

doubletage;

};

voidmain()

(

CStudentstu;

Cteachertea;

stu.SetData(,,LiMingz,,“21010211");

stu.InputScore(80,76,91);

stu.Print();

tea.SetData(〃zhangli〃,〃001〃);

tea.Inputage(12);

tea.Print();

)

二、其次類(lèi)題目(20道,每題9分,請(qǐng)自行設(shè)計(jì)輸出格式)

1.某商店經(jīng)銷(xiāo)一種貨物,貨物成箱購(gòu)進(jìn),成箱賣(mài)出,購(gòu)進(jìn)和賣(mài)出時(shí)以重量為單位,各箱

的重量不一樣,因此,商店須要登記目前庫(kù)存貨物的總量,要求把商店貨物購(gòu)進(jìn)和賣(mài)出

的狀況模擬出來(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*gl=newGoods(w);

cin?w;Goods*g2=newGoods(w);

cout<<Goods::TotalWeight()<<endl;

deleteg2;

cout<<Goods::TotalWeight()<<endl;

2.設(shè)計(jì)一個(gè)Time類(lèi),包括三個(gè)私有數(shù)據(jù)成員:hour,minute,sec,用構(gòu)造函數(shù)初始化,內(nèi)

設(shè)公用函數(shù)display(Date&d),設(shè)計(jì)一個(gè)Date類(lèi),包括三個(gè)私有數(shù)據(jù)成員:

month,day,year,也用構(gòu)適函數(shù)初始化;分別定義兩個(gè)帶參數(shù)的對(duì)象

tl(12,30,55),dl(3,25,2023),通過(guò)友員成員函數(shù)的應(yīng)用,輸出dl和tl的值。

#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?z,//z<<da.day<<z,/,z?da.year<<endl;

cout?hour<<zz:,z<<minute<<z,:,,?sec<<endl;

intmain()

{Timetl(12,30,55);

Datedl(3,25,2023);

tl.display(dl);

return0;

3.設(shè)計(jì)一個(gè)Time類(lèi),包括三個(gè)私有數(shù)據(jù)成員:hour,minute,sec,用構(gòu)造函數(shù)初始化,,

設(shè)封一一個(gè)Date類(lèi),包括三個(gè)私有數(shù)據(jù)成員:month,day,year,也用構(gòu)適函數(shù)初始化;設(shè)

計(jì)一個(gè)一般函數(shù)display(…),將display分別設(shè)置為T(mén)ime類(lèi)和Date類(lèi)的友元函數(shù),

在主函數(shù)中分別定義兩個(gè)帶參數(shù)的對(duì)象11(12,30,55),dl(3,25,2023),

調(diào)用desplay,輸出年、月、日和時(shí)、分、秒。

Sinclude<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<<z,:zz<<t.minute<<〃:〃<<t?sec<<endl;

intmain()

Timetl(10,30,55);

Datedl(3,25,2023);

display(dl,tl);

return0;

)

4.可以定義點(diǎn)類(lèi)(Point),再定義一個(gè)類(lèi)(Distance)描述兩點(diǎn)之間的距離,其數(shù)據(jù)成

員為兩個(gè)點(diǎn)類(lèi)對(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(Pointql,Pointq2):a(ql),b(q2)

{doubledx=double(a.GetX()-b.GetX());

doubledy=double(a.GetY()-b.GetY());

dist=sqrt(dx*dx+dy*dy);

)

intmain()

{Pointpl(3.0,5.0),p2(4.0,6.0);

Distancedis(pl,p2);

cout<<"Thisdistanceis:〃<<dis.Getdist()<<endl;

return0;}

5.定義點(diǎn)類(lèi)(Point),

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論