C++面向对象程序设计上机考试题库Word下载.docx
《C++面向对象程序设计上机考试题库Word下载.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计上机考试题库Word下载.docx(54页珍藏版)》请在冰点文库上搜索。
};
voidmain()
{Boxa;
(2,3,4);
();
2.有两个长方柱,其长、宽、高分别为:
(1)30,20,10;
(2)12,10,20。
分别求他们的体积。
编一个基于对象的程序,在类中用带参数的构造函数。
#include<
usingnamespacestd;
classBox
{public:
Box(int,int,int);
Boxbox1Boxbox2Boxbox1Boxbox2<
()<
istheMinimumoftwointedernumbers."
endl<
Compare<
float>
cmp2,;
istheMaximumoftwofloatnumbers."
istheMinimumoftwofloatnumbers."
char>
cmp3('
a'
'
A'
);
istheMaximumoftwocharacters."
istheMinimumoftwocharacters."
return0;
3.建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。
初值自拟。
#include<
usingnamespacestd;
classStudent
Student(intn,doubles):
num(n),score(s){}
voiddisplay();
private:
intnum;
doublescore;
};
voidStudent:
:
display()
num<
"
score<
intmain()
{Studentstud[5]={
Student(101,,Student(102,,Student(103,,
Student(104,,Student(105,};
Student*p=stud;
for(inti=0;
i<
=2;
p=p+2,i++)
p->
display();
4.建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
Student(intn,floats):
floatscore;
voidmax(Student*);
Student*p=&
stud[0];
max(p);
voidmax(Student*arr)
{floatmax_score=arr[0].score;
intk=0;
for(inti=1;
5;
i++)
if(arr[i].score>
max_score){max_score=arr[i].score;
k=i;
arr[k].num<
max_score<
5.用new建立一个动态一维数组,并初始化int[10]={1,2,3,4,5,6,7,8,9,10},用指针输出,最后销毁数组所占空间。
string>
voidmain(){
int*p;
p=newint[10];
=10;
{
*(p+i-1)=i;
*(p+i-1)<
;
delete[]p;
return;
6.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。
将运算符函数重载为非成员、非友元的普通函数。
编写程序,求两个复数之和。
classComplex
Complex(){real=0;
imag=0;
Complex(doubler,doublei){real=r;
imag=i;
doubleget_real();
doubleget_imag();
doublereal;
doubleimag;
doubleComplex:
get_real()
{returnreal;
get_imag()
{returnimag;
voidComplex:
{cout<
("
real<
"
imag<
i)"
Complexoperator+(Complex&
c1,Complex&
c2)
returnComplex()+(),()+());
{Complexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
c3="
();
7.定义一个复数类Complex,重载运算符“+”,“—”,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。
编程序,分别求出两个复数之和,差。
Complexoperator+(Complex&
c2);
Complexoperator-(Complex&
ComplexComplex:
operator+(Complex&
{Complexc;
=real+;
=imag+;
returnc;
operator-(Complex&
=;
c1+c2="
c3=c1-c2;
c1-c2="
8.定义一个复数类Complex,重载运算符“*”,“/”,使之能用于复数的乘,除。
运算符重载函数作为Complex类的成员函数。
编程序,分别求出两个复数之积和商。
提示:
两复数相乘的计算公式为:
(a+bi)*(c+di)=(ac-bd)+(ad+bc)i。
两复数相除的计算公式为:
(a+bi)/(c+di)=(ac+bd)/(c*c+d*d)+(bc-ad)/(c*c+d*d)i。
Complexoperator*(Complex&
Complexoperator/(Complex&
operator*(Complex&
=real**;
=imag*+real*;
operator/(Complex&
=(real*+imag*/*+*;
=(imag**/*+*;
c3=c1*c2;
c1*c2="
c3=c1/c2;
c1/c2="
9.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。
参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。
例如:
c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。
编程序,分别求两个复数之和、整数和复数之和。
>
Complexoperator+(int&
i);
friendComplexoperator+(int&
Complex&
c)
{returnComplex(real+,imag+;
operator+(int&
i)
{returnComplex(real+i,imag);
Complexoperator+(int&
i,Complex&
{returnComplex(i+,;
inti=5;
c3=i+c1;
i+c1="
c3=c1+i;
c1+i="
10.有两个矩阵a和b,均为2行3列。
求两个矩阵之和。
重载运算符“+”,使之能用于矩阵相加。
如c=a+b。
classMatrix
Matrix();
friendMatrixoperator+(Matrix&
Matrix&
voidinput();
intmat[2][3];
Matrix:
Matrix()
{for(inti=0;
2;
for(intj=0;
j<
3;
j++)
mat[i][j]=0;
Matrixoperator+(Matrix&
a,Matrix&
b)
{Matrixc;
{[i][j]=[i][j]+[i][j];
}
voidMatrix:
input()
inputvalueofmatrix:
cin>
mat[i][j];
display()
{for(inti=0;
{for(intj=0;
mat[i][j]<
{Matrixa,b,c;
Matrixa:
Matrixb:
c=a+b;
Matrixc=Matrixa+Matrixb:
11.将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的友元函数。
Complex(doubler){real=r;
friendComplexoperator+(Complex&
Complexoperator+(Complex&
{
returnComplex+,+;
c1="
c2="
14.定义一个字符串类String,用来存放不定长的字符串,重载运算符“==”,,用于两个字符串的等于比较运算。
classString
String(){p=NULL;
String(char*str);
friendbooloperator==(String&
string1,String&
string2);
char*p;
String:
String(char*str)
{p=str;
voidString:
p;
booloperator==(String&
string2)
{if(strcmp,==0)
returntrue;
else
returnfalse;
voidcompare(String&
{if(operator==(string1,string2)==1)
{();
cout<
="
{Stringstring1("
Hello"
),string2("
compare(string1,string2);
15.定义一个字符串类String,用来存放不定长的字符串,重载运算符"
,用于两个字符串的小于的比较运算。
friendbooloperator<
(String&
booloperator<
{if(strcmp,<
0)
{if(operator<
(string1,string2)==1)
Book"
Computer"
16.定义一个字符串类String,用来存放不定长的字符串,重载运算符"
,用于两个字符串的大于的比较运算。
friendbooloperator>
booloperator>
{if(strcmp,>
{if(operator>
17.定义一个描述学生基本情况的类,数据成员包括姓名、学号、C++成绩、英语和数学成绩,成员函数包括输出数据,求出总成绩和平均成绩。
数据自拟。
#include"
classCStuScore
{public:
charstrName[12];
charstrStuNO[9];
voidSetScore(charsname[12],charNO[9],floats0,floats1,floats2)
{
strcpy(strName,sname);
strcpy(strStuNO,NO);
fScore[0]=s0;
fScore[1]=s1;
fScore[2]=s2;
voidprint()
{cout<
cout<
姓名:
strName;
学号:
strStuNO;
C++成绩:
fScore[0]<
英语成绩:
fScore[1]<
数学成绩:
fScore[2]<
floatGetSUM()
{return(float)((fScore[0]+fScore[1]+fScore[2]));
floatGetAverage();
floatfScore[3];
floatCStuScore:
GetAverage()
{return(float)((fScore[0]+fScore[1]+fScore[2])/;
{CStuScoreone;
floata,b,c;
charName[12];
charStuNO[9];
cin>
Name;
StuNO;
成绩1:
成绩2:
成绩3:
\n"
a>
b>
c;
(