C++面向对象程序设计上机考试题库.docx
- 文档编号:6875711
- 上传时间:2023-05-10
- 格式:DOCX
- 页数:46
- 大小:35.68KB
C++面向对象程序设计上机考试题库.docx
《C++面向对象程序设计上机考试题库.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计上机考试题库.docx(46页珍藏版)》请在冰点文库上搜索。
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; (2,3,4); (); (); (); } 2.有两个长方柱,其长、宽、高分别为: (1)30,20,10; (2)12,10,20。 分别求他们的体积。 编一个基于对象的程序,在类中用带参数的构造函数。 #include usingnamespacestd; classBox {public: Box(int,int,int);Boxbox1Boxbox2Boxbox1Boxbox2< cout<<()<<"istheMinimumoftwointedernumbers."< Compare cout<<()<<"istheMaximumoftwofloatnumbers."< cout<<()<<"istheMinimumoftwofloatnumbers."< Compare cout<<()<<"istheMaximumoftwocharacters."< cout<<()<<"istheMinimumoftwocharacters."< return0; } 3.建立一个对象数组,内放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,,Student(102,,Student(103,, Student(104,,Student(105,}; Student*p=stud; for(inti=0;i<=2;p=p+2,i++) p->display(); return0; } 4.建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。 初值自拟。 #include usingnamespacestd; classStudent {public: Student(intn,floats): num(n),score(s){} intnum; floatscore; }; voidmain() {Studentstud[5]={ Student(101,,Student(102,,Student(103,, Student(104,,Student(105,}; 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< } 5.用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; } 6.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。 将运算符函数重载为非成员、非友元的普通函数。 编写程序,求两个复数之和。 初值自拟。 #include 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<<"("< Complexoperator+(Complex&c1,Complex&c2) { returnComplex()+(),()+()); } intmain() {Complexc1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c3="; (); return0; } 7.定义一个复数类Complex,重载运算符“+”,“—”,使之能用于复数的加,减运算,运算符重载函数作为Complex类的成员函数。 编程序,分别求出两个复数之和,差。 初值自拟。 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; =real+; =imag+; returnc;} ComplexComplex: : operator-(Complex&c2) {Complexc; =; =; returnc;} voidComplex: : display() {cout<<"("< intmain() {Complexc1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c1+c2="; (); c3=c1-c2; cout<<"c1-c2="; (); return0; } 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。 #include 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; =real**; =imag*+real*; returnc;} ComplexComplex: : operator/(Complex&c2) {Complexc; =(real*+imag*/*+*; =(imag**/*+*; returnc;} voidComplex: : display() {cout<<"("< intmain() {Complexc1(3,4),c2(5,-10),c3; c3=c1*c2; cout<<"c1*c2="; (); c3=c1/c2; cout<<"c1/c2="; (); return0; } 9.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。 参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。 例如: c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。 编程序,分别求两个复数之和、整数和复数之和。 初值自拟。 #include<> 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+,imag+;} ComplexComplex: : operator+(int&i) {returnComplex(real+i,imag);} voidComplex: : display() {cout<<"("< Complexoperator+(int&i,Complex&c) {returnComplex(i+,;} intmain() {Complexc1(3,4),c2(5,-10),c3; inti=5; c3=c1+c2; cout<<"c1+c2="; (); c3=i+c1; cout<<"i+c1="; (); c3=c1+i; cout<<"c1+i="; (); return0; } 10.有两个矩阵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++) {[i][j]=[i][j]+[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; (); (); cout< "< (); cout< "< (); c=a+b; cout< "< (); return0; } 11.将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的友元函数。 初值自拟。 #include<> 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+,+; } voidComplex: : display() {cout<<"("< intmain() {Complexc1(3,4),c2(5,-10),c3; c3=c1+c2; cout<<"c1=";(); cout<<"c2=";(); cout<<"c1+c2=";(); 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,==0) returntrue; else returnfalse; } voidcompare(String&string1,String&string2) {if(operator==(string1,string2)==1) {();cout<<"=";();} cout< } intmain() {Stringstring1("Hello"),string2("Hello"); compare(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,<0) returntrue; else returnfalse; } voidcompare(String&string1,String&string2) {if(operator<(string1,string2)==1) {();cout<<"<";();} cout< } intmain() {Stringstring1("Book"),string2("Computer"); compare(string1,string2); return0; } 16.定义一个字符串类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,>0) returntrue; else returnfalse; } voidcompare(String&string1,String&string2) {if(operator>(string1,string2)==1) {();cout<<">";();} cout< } intmain() {Stringstring1("Hello"),string2("Book"); compare(string1,string2); return0;} 17.定义一个描述学生基本情况的类,数据成员包括姓名、学号、C++成绩、英语和数学成绩,成员函数包括输出数据,求出总成绩和平均成绩。 数据自拟。 #include"" #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<<"姓名: "< cout<<"学号: "< cout<<"C++成绩: "< "< "< floatGetSUM() {return(float)((fScore[0]+fScore[1]+fScore[2]));} floatGetAverage(); private: floatfScore[3]; }; floatCStuScore: : GetAverage() {return(float)((fScore[0]+fScore[1]+fScore[2])/;} voidmain() {CStuScoreone; floata,b,c; charName[12]; charStuNO[9]; cout<<"姓名: "; cin>>Name; cout<<"学号: "; cin>>StuNO; cout<<"成绩1: "<<"成绩2: "<<"成绩3: "<<"\n"; cin>>a>>b>>c; (
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 面向 对象 程序设计 上机 考试 题库