C++面向对象程序设计上机考试题库.docx
《C++面向对象程序设计上机考试题库.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计上机考试题库.docx(64页珍藏版)》请在冰豆网上搜索。
C++面向对象程序设计上机考试题库
C++面向对象程序设计上机考试题库
一、第一类题目〔20道,每题7分,在word中保存代码并将输出结果窗口保存〕
1.定义盒子Box类,要求具有以下成员:
长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的外表积。
#include
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="<cout<<"s="<
}
};
voidmain()
{Boxa;
a.init(2,3,4);
a.volue();
a.area();
a.show();
}
2.有两个长方柱,其长、宽、高分别为:
〔1〕30,20,10;〔2〕12,10,20。
分别求他们的体积。
编一个基于对象的程序,在类中用带参数的构造函数。
#include
usingnamespacestd;
classBox
{public:
Box(int,int,int);//带参数的构造函数
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()
{
Boxbox1(30,20,10);
cout<<"Thevolumeofbox1is"<Boxbox2(12,10,20);
cout<<"Thevolumeofbox2is"<return0;
}
3.有两个长方柱,其长、宽、高分别为:
〔1〕12,20,25;〔2〕10,30,20。
分别求他们的体积。
编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。
#include
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()
{
Boxbox1(10,20,25);
cout<<"Thevolumeofbox1is"<Boxbox2(10,30,20);
cout<<"Thevolumeofbox2is"<return0;
}
4.声明一个类模板,利用它分别实现两个整数、浮点数和字符的比拟,求出大数和小数。
#include
usingnamespacestd;
template//声明一个类模板
classpare
{public:
pare(numtypea,numtypeb)
{x=a;y=b;}
numtypemax()
{return(x>y)?
x:
y;}
numtypemin()
{return(xx:
y;}
private:
numtypex,y;
};
intmain()
{parecmp1(3,7);
cout<cout<parecmp2(45.78,93.6);
cout<cout<parecmp3('a','A');
cout<cout<return0;
}
5.建立一个对象数组,放5个学生的数据〔学号、成绩〕,用指针指向数组首元素,输出第1,3,5个学生的数据。
初值自拟。
#include
usingnamespacestd;
classStudent
{public:
Student(intn,doubles):
num(n),score(s){}
voiddisplay();
private:
intnum;
doublescore;
};
voidStudent:
:
display()
{cout<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.建立一个对象数组,放5个学生的数据〔学号、成绩〕,设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
初值自拟。
#include
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<}
7.用new建立一个动态一维数组,并初始化int[10]={1,2,3,4,5,6,7,8,9,10},用指针输出,最后销毁数组所占空间。
#include
#include
usingnamespacestd;
voidmain(){
int*p;
p=newint[10];
for(inti=1;i<=10;i++)
{
*(p+i-1)=i;
cout<<*(p+i-1)<<"";
}
cout<delete[]p;
return;
}
8.定义一个复数类plex,重载运算符“+〞,使之能用于复数的加法运算。
将运算符函数重载为非成员、非友元的普通函数。
编写程序,求两个复数之和。
初值自拟。
#include
usingnamespacestd;
classplex
{public:
plex(){real=0;imag=0;}
plex(doubler,doublei){real=r;imag=i;}
doubleget_real();
doubleget_imag();
voiddisplay();
private:
doublereal;
doubleimag;
};
doubleplex:
:
get_real()
{returnreal;}
doubleplex:
:
get_imag()
{returnimag;}
voidplex:
:
display()
{cout<<"("<plexoperator+(plex&c1,plex&c2)
{
returnplex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
}
intmain()
{plexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c3=";
c3.display();
return0;
}
9.定义一个复数类plex,重载运算符“+〞,“—〞,使之能用于复数的加,减运算,运算符重载函数作为plex类的成员函数。
编程序,分别求出两个复数之和,差。
初值自拟。
usingnamespacestd;
classplex
{public:
plex(){real=0;imag=0;}
plex(doubler,doublei){real=r;imag=i;}
plexoperator+(plex&c2);
plexoperator-(plex&c2);
voiddisplay();
private:
doublereal;
doubleimag;
};
plexplex:
:
operator+(plex&c2)
{plexc;
c.real=real+c2.real;
c.imag=imag+c2.imag;
returnc;}
plexplex:
:
operator-(plex&c2)
{plexc;
c.real=real-c2.real;
c.imag=imag-c2.imag;
returnc;}
voidplex:
:
display()
{cout<<"("<intmain()
{plexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1+c2=";
c3.display();
c3=c1-c2;
cout<<"c1-c2=";
c3.display();
return0;
}
10.定义一个复数类plex,重载运算符“*〞,“/〞,使之能用于复数的乘,除。
运算符重载函数作为plex类的成员函数。
编程序,分别求出两个复数之积和商。
初值自拟。
提示:
两复数相乘的计算公式为:
(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。
#include
usingnamespacestd;
classplex
{public:
plex(){real=0;imag=0;}
plex(doubler,doublei){real=r;imag=i;}
plexoperator*(plex&c2);
plexoperator/(plex&c2);
voiddisplay();
private:
doublereal;
doubleimag;
};
plexplex:
:
operator*(plex&c2)
{plexc;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
returnc;}
plexplex:
:
operator/(plex&c2)
{plexc;
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;}
voidplex:
:
display()
{cout<<"("<intmain()
{plexc1(3,4),c2(5,-10),c3;
c3=c1*c2;
cout<<"c1*c2=";
c3.display();
c3=c1/c2;
cout<<"c1/c2=";
c3.display();
return0;
}
11.定义一个复数类plex,重载运算符“+〞,使之能用于复数的加法运算。
参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。
例如:
c1+c2,i+c1,c1+i均合法〔设i为整数,c1,c2为复数〕。
编程序,分别求两个复数之和、整数和复数之和。
初值自拟。
#include
classplex
{public:
plex(){real=0;imag=0;}
plex(doubler,doublei){real=r;imag=i;}
plexoperator+(plex&c2);
plexoperator+(int&i);
friendplexoperator+(int&,plex&);
voiddisplay();
private:
doublereal;
doubleimag;
};
plexplex:
:
operator+(plex&c)
{returnplex(real+c.real,imag+c.imag);}
plexplex:
:
operator+(int&i)
{returnplex(real+i,imag);}
voidplex:
:
display()
{cout<<"("<plexoperator+(int&i,plex&c)
{returnplex(i+c.real,c.imag);}
intmain()
{plexc1(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;
}
12.有两个矩阵a和b,均为2行3列。
求两个矩阵之和。
重载运算符“+〞,使之能用于矩阵相加。
如c=a+b。
初值自拟。
#include
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:
"<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<cout<}
intmain()
{Matrixa,b,c;
a.input();
b.input();
cout<"<a.display();
cout<"<b.display();
c=a+b;
cout<"<c.display();
return0;
}
13.将运算符“+〞重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为plex类的友元函数。
初值自拟。
#include
classplex
{public:
plex(){real=0;imag=0;}
plex(doubler){real=r;imag=0;}
plex(doubler,doublei){real=r;imag=i;}
friendplexoperator+(plex&c1,plex&c2);
voiddisplay();
private:
doublereal;
doubleimag;
};
plexoperator+(plex&c1,plex&c2)
{
returnplex(c1.real+c2.real,c1.imag+c2.imag);
}
voidplex:
:
display()
{cout<<"("<intmain()
{plexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
return0;
}
14.定义一个字符串类String,用来存放不定长的字符串,重载运算符“==〞,,用于两个字符串的等于比拟运算。
初值自拟。
#include
#include
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<
booloperator==(String&string1,String&string2)
{if(strcmp(string1.p,string2.p)==0)
returntrue;
else
returnfalse;
}
voidpare(String&string1,String&string2)
{if(operator==(string1,string2)==1)
{string1.display();cout<<"=";string2.display();}
cout<}
intmain()
{Stringstring1("Hello"),string2("Hello");
pare(string1,string2);
return0;
}
15.定义一个字符串类String,用来存放不定长的字符串,重载运算符"<",用于两个字符串的小于的比拟运算。
初值自拟。
#include
#include
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<
booloperator<(String&string1,String&string2)
{if(strcmp(string1.p,string2.p)<0)
returntrue;
else
returnfalse;
}
voidpare(String&string1,Str