C++面向对象程序设计上机考试题库.docx

上传人:b****8 文档编号:11287851 上传时间:2023-02-26 格式:DOCX 页数:77 大小:42.38KB
下载 相关 举报
C++面向对象程序设计上机考试题库.docx_第1页
第1页 / 共77页
C++面向对象程序设计上机考试题库.docx_第2页
第2页 / 共77页
C++面向对象程序设计上机考试题库.docx_第3页
第3页 / 共77页
C++面向对象程序设计上机考试题库.docx_第4页
第4页 / 共77页
C++面向对象程序设计上机考试题库.docx_第5页
第5页 / 共77页
点击查看更多>>
下载资源
资源描述

C++面向对象程序设计上机考试题库.docx

《C++面向对象程序设计上机考试题库.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计上机考试题库.docx(77页珍藏版)》请在冰豆网上搜索。

C++面向对象程序设计上机考试题库.docx

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//声明一个类模板

classCompare

{public:

Compare(numtypea,numtypeb)

{x=a;y=b;}

numtypemax()

{return(x>y)?

x:

y;}

numtypemin()

{return(x

x:

y;}

private:

numtypex,y;

};

intmain()

{Comparecmp1(3,7);

cout<

cout<

Comparecmp2(45.78,93.6);

cout<

cout<

Comparecmp3('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.定义一个复数类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(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;

}

9.定义一个复数类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;

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

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;

}

10.定义一个复数类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;

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

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;

}

11.定义一个复数类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+c.real,imag+c.imag);}

ComplexComplex:

:

operator+(int&i)

{returnComplex(real+i,imag);}

voidComplex:

:

display()

{cout<<"("<

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;

}

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.将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为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(c1.real+c2.real,c1.imag+c2.imag);

}

voidComplex:

:

display()

{cout<<"("<

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;

}

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;

}

voidcompare(String&string1,String&string2)

{if(operator==(string1,string2)==1)

{string1.display();cout<<"=";string2.display();}

cout<

}

intmain()

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

compare(string1,string2);

return0;

}

15.定义一个字符串类String,用来存放不定长的字符串,重载运算符"<",用于两个字符串的小于的比较运算。

初值自拟。

#include

#include

classString

{public:

String(){p=NULL;}

String(char*str);

friendboolop

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 成人教育 > 电大

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1