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

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

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

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

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

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

Thevolumeofbox1is"

box1.volume()<

Boxbox2(12,10,20);

Thevolumeofbox2is"

box2.volume()<

return0;

3.有两个长方柱,其长、宽、高分别为:

〔1〕12,20,25;

〔2〕10,30,20。

编一个基于对象的程序,且定义两个构造函数,其中一个有参数,一个无参数。

usingnamespacestd;

classBox

Box();

Box(intlen,intw,inth):

length(len),width(w),height(h){}

intheight;

intmain()

Boxbox1(10,20,25);

Boxbox2(10,30,20);

4.声明一个类模板,利用它分别实现两个整数、浮点数和字符的比拟,求出大数和小数。

#include<

template<

classnumtype>

//声明一个类模板

classpare

pare(numtypea,numtypeb)

{x=a;

y=b;

numtypemax()

{return(x>

y)?

x:

y;

numtypemin()

{return(x<

numtypex,y;

{pare<

int>

cmp1(3,7);

cmp1.max()<

istheMaximumoftwointedernumbers."

cmp1.min()<

istheMinimumoftwointedernumbers."

endl<

pare<

float>

cmp2(45.78,93.6);

cmp2.max()<

istheMaximumoftwofloatnumbers."

cmp2.min()<

istheMinimumoftwofloatnumbers."

char>

cmp3('

a'

'

A'

);

cmp3.max()<

istheMaximumoftwocharacters."

cmp3.min()<

istheMinimumoftwocharacters."

5.建立一个对象数组,放5个学生的数据〔学号、成绩〕,用指针指向数组首元素,输出第1,3,5个学生的数据。

初值自拟。

classStudent

Student(intn,doubles):

num(n),score(s){}

voiddisplay();

intnum;

doublescore;

voidStudent:

display()

num<

"

score<

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

6.建立一个对象数组,放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<

7.用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;

8.定义一个复数类plex,重载运算符“+〞,使之能用于复数的加法运算。

将运算符函数重载为非成员、非友元的普通函数。

编写程序,求两个复数之和。

classplex

plex(){real=0;

imag=0;

plex(doubler,doublei){real=r;

imag=i;

doubleget_real();

doubleget_imag();

doublereal;

doubleimag;

doubleplex:

get_real()

{returnreal;

get_imag()

{returnimag;

voidplex:

{cout<

("

real<

"

imag<

i)"

plexoperator+(plex&

c1,plex&

c2)

returnplex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());

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

c3=c1+c2;

c3="

c3.display();

9.定义一个复数类plex,重载运算符“+〞,“—〞,使之能用于复数的加,减运算,运算符重载函数作为plex类的成员函数。

编程序,分别求出两个复数之和,差。

plexoperator+(plex&

c2);

plexoperator-(plex&

plexplex:

operator+(plex&

{plexc;

c.real=real+c2.real;

c.imag=imag+c2.imag;

returnc;

operator-(plex&

c.real=real-c2.real;

c.imag=imag-c2.imag;

c1+c2="

c3=c1-c2;

c1-c2="

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。

plexoperator*(plex&

plexoperator/(plex&

operator*(plex&

c.real=real*c2.real-imag*c2.imag;

c.imag=imag*c2.real+real*c2.imag;

operator/(plex&

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

c3=c1*c2;

c1*c2="

c3=c1/c2;

c1/c2="

11.定义一个复数类plex,重载运算符“+〞,使之能用于复数的加法运算。

参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。

例如:

c1+c2,i+c1,c1+i均合法〔设i为整数,c1,c2为复数〕。

编程序,分别求两个复数之和、整数和复数之和。

iostream.h>

plexoperator+(int&

i);

friendplexoperator+(int&

plex&

c)

{returnplex(real+c.real,imag+c.imag);

operator+(int&

i)

{returnplex(real+i,imag);

plexoperator+(int&

i,plex&

{returnplex(i+c.real,c.imag);

inti=5;

c3=i+c1;

i+c1="

c3=c1+i;

c1+i="

12.有两个矩阵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;

{c.mat[i][j]=a.mat[i][j]+b.mat[i][j];

}

voidMatrix:

input()

inputvalueofmatrix:

cin>

>

mat[i][j];

display()

{for(inti=0;

{for(intj=0;

mat[i][j]<

{Matrixa,b,c;

a.input();

b.input();

Matrixa:

a.display();

Matrixb:

b.display();

c=a+b;

Matrixc=Matrixa+Matrixb:

c.display();

13.将运算符“+〞重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为plex类的友元函数。

plex(doubler){real=r;

friendplexoperator+(plex&

plexoperator+(plex&

returnplex(c1.real+c2.real,c1.imag+c2.imag);

c1="

c1.display();

c2="

c2.display();

14.定义一个字符串类String,用来存放不定长的字符串,重载运算符“==〞,,用于两个字符串的等于比拟运算。

string.h>

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(string1.p,string2.p)==0)

returntrue;

else

returnfalse;

voidpare(String&

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

{string1.display();

cout<

="

string2.display();

{Stringstring1("

Hello"

),string2("

pare(string1,string2);

15.定义一个字符串类String,用来存放不定长的字符串,重载运算符"

,用于两个字符串的小于的比拟运算。

friendbooloperator<

(String&

booloperator<

{if(strcmp(string1.p,string2.p)<

0)

string1,Str

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

当前位置:首页 > 幼儿教育 > 幼儿读物

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

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