1、 上海大学14-15级C++试题-DOC 精品文档 成 绩 上海大学2014~2015学年度秋季学期试卷(A卷) 课程名:面向对象程序设计课程号:08305121学分:5 应试人声明: 我保证遵守《上海大学学生手册》中的《上海大学考场规则》,如有考试违纪、作弊行为,愿意接受《上海大学学生考试违纪、作弊行为界定及处分规定》的纪律处分。 应试人应试人学号应试人所在院系 题号 一(20) 二(30) 三(25) 四(25) 得分 得 分 —————————————————————————————————————— 一、判断题(每
2、小题2分,共20分) 1. 引用在声明时必须对其初始化,以绑定某个已经存在的变量(或对象), 在该引用的生命期内,该绑定不能被更改。 (√) 2. 指针变量在定义时必须对其初始化,以锁定某个已经存在的目标变量(或 对象),在该指针变量的生命期内,该指向不能被更改。 (×) 3. 值返回的函数(如:doublesqrt(double);)的调用表达式(如: sqrt(2.0))代表一个无名的临时变量(或对象),一般不将其用作左值。 (√) 4. 引用返回的函数,可以返回该函数中值传递的形参变量(或对象)。 (×) 5. 任何类都有构造函数、复制构造函数、析构函数、赋值运算符函数。 (√
3、) 6. 有静态数据成员的类,一般地应该考虑为其设计复制构造函数、析构函数。 (√) 7. 将用于输出的插入运算符函数operator<<设计成友元函数的根本原因是 因为进行输出操作时需要访问对象的内部数据成员。 (×) 8. 在C++程序中,操作符new的功能与calloc函数的功能完全一样。 (×) 9. 创建一个C++字符串对象(如:stringstr;),则sizeof(str)的值 等于str.length()的值。其中成员函数length为返回字符串的长度。 (×) 10. 基类的私有数据成员在派生类中是存在的,但不可直接访问,需要用从基类 继承下来的函数访问。 (√)
4、
得
分
二、填空题(每空2分,共30分)如下设计了结点类Node、链表类LinkList,并将链表类作为结点类的友类,请根据运行结果,完成程序。
#include
5、ublic: LinkList(intn=0,int*array=NULL) { Node*p; head=NULL; for(inti=④n-1;i>=0;i--) { p=newNode; p->data=(array!=NULL)?array[i]:0; p->next=head; ⑤head=p; } } LinkList(constLinkList&link) { head=⑥NULL; *this=link; } virtual~LinkList() { FreeList
6、); } ⑦LinkList&operator=(constLinkList&link) { if(⑧&link==this) return*this; FreeList(); Node*p,*q; for(p=link.head;⑨p!=NULL;p=p->next) { if(head==NULL) head=q=newNode(p->data); else { q->next=newNode(p->data); q=q->next; } } ⑩q->next=NU
7、LL;
return*this;
}
voidFreeList()
{
Node*p;
while(⑪head!=NULL)
{
p=head;
⑫head=head->next;
deletep;
}
}
voidShowList(ostream&out)⑬const
{
out<<"head";
for(Node*p=head;p!=NULL;p=p->next)
out<<"->"<
8、ostream&out,constLinkList&link)
{
运行结果
head->3->7->2->1->NULL
head->3->7->2->1->NULL
head->NULL
head->3->7->2->1->NULL
link.ShowList(⑭out);
return⑮out;
}
intmain()
{
intn,a[]={3,7,2,1};
n=sizeof(a)/sizeof(*a);
LinkListlinkA(n,a),linkB(linkA),linkC;
cout< 9、'< 10、不同,分别为"< 11、 Testt2(2,3);
cout<<*p<<'\n'
< 12、onst=0;
protected:
double_x,_y;
};
ostream&operator<<(ostream&out,constBASE&x)
{
x.Show(out);
returnout;
}
classComplex:publicBASE
{
public:
Complex(doublex=0,doubley=0):BASE(x,y)
{
}
voidShow(ostream&out)const
{
if(_x!=0)
{
out<<_x;
if(_y>0) out<<'+'<<_y<<'i'; 13、
elseif(_y<0) out<<'-'<<-_y<<'i';
}
else
{
if(_y!=0)
cout<<_y<<'i';
else
cout<<_x;
}
}
friendComplexoperator+(constComplex&a,constComplex&b)
{
Complexc;
c._x=a._x+b._x;
c._y=a._y+b._y;
returnc;
}
friendComplexoperator*(constComplex&a,constCompl 14、ex&b)
{
Complexc;
c._x=a._x*b._x-a._y*b._y;
c._y=a._x*b._y+a._y*b._x;
returnc;
}
doubleabs()
{
returnsqrt(_x*_x+_y*_y);
}
};
classPoint:publicBASE
{
public:
Point(doublex=0,doubley=0):BASE(x,y)
{
}
voidShow(ostream&out)const
{
out<<'('<<_x<<","<<_y<<')';
} 15、
friendPointoperator+(constPoint&a,constPoint&b)
{
Pointc;
c._x=a._x+b._x;
c._y=a._y+b._y;
returnc;
}
};
运行结果(2.1)
1+2i
3+4i
1i
10
y.abs():5
4+6i
-5+10i
(1,2)
(3,4)
(4,6)
2.1(10分)测试程序
#include"test.h"
intmain()
{
Complexx(1,2),y(3,4),z1(0,1),z2(10);
cout< 16、'< 17、)
(4,8)
Complexx(1,2),y,z;
y=5+x;
z=5*x;
cout< 18、09551615)。为了进一步扩展非负整数的取值范围设计了如下的类。该类数据可精确计算至,可处理36~37位非负十进制整数。请在类的声明体外实现5个尚未定义的成员函数或友元函数。最后写出程序的运行结果(每个函数定义4分,运行结果5分)。
//LLINT.h 头文件
#ifndefLLINT_H
#defineLLINT_H
#include 19、数为低位
LLINT(constchar*str);
LLINT&operator++();
LLINToperator++(int);
friendLLINToperator+(constLLINT&x1,constLLINT&x2);
LLINT&operator+=(constLLINT&x);
friendostream&operator<<(ostream&out,constLLINT&x);
friendLLINTatoLLINT(constchar*str);
friendistream&operator>>(istream&in,LLINT&x) 20、
friendbooloperator>(constLLINT&x1,constLLINT&x2);
friendbooloperator>=(constLLINT&x1,constLLINT&x2);
friendbooloperator<(constLLINT&x1,constLLINT&x2);
friendbooloperator<=(constLLINT&x1,constLLINT&x2);
friendbooloperator==(constLLINT&x1,constLLINT&x2);
friendbooloperator!=(constLLINT&x 21、1,constLLINT&x2);
protected:
staticconstunsignedlonglongBBILLION;
unsignedlonglonga1,a0;
//a1*1000000000000000000+a0可表示36~37位十进制非负整数
};
#endif
//LLINT.cpp 源程序文件
#include"LLINT.h"
#include 22、nedlonglongx0,unsignedlonglongx1)
{ //构造函数
unsignedlonglongx=x0/BBILLION;
a0=x0%BBILLION;
a1=x1+x;
}
LLINT::LLINT(constchar*str) //转换构造函数(从C-字符串转换)
{
*this=atoLLINT(str); //直接利用成员函数实现转换构造
}
LLINTLLINT::operator++(int) //后增量运算符函数
{
LLINTtemp(*this);
++(*this);
returntemp;
23、}
LLINToperator+(constLLINT&x1,constLLINT&x2)
{
LLINTs;
unsignedlonglongc=x1.a0+x2.a0;
s.a0=c%LLINT::BBILLION;
s.a1=x1.a1+x2.a1+c/LLINT::BBILLION;
returns;
}
ostream&operator<<(ostream&out,constLLINT&x)
{
if(x.a1!=0)
out< 24、 out< 25、nstLLINT&x1,constLLINT&x2)
{
if(x1.a1 26、T(constchar*str)
{
LLINTx;
inti,j=0,n;
unsignedlonglongp0=1,p1=1;
for(n=0;str[n];n++)
;
if(n==0)returnx;
for(i=n-1;i>=0;i--)
{
if('0'<=str[i]&&str[i]<='9')
{
if(j<18)
{
x.a0+=p0*(str[i]-'0');
p0*=10;
}
elseif(j<36)
{
x.a1+=p1*(str[i]-'0');
27、 p1*=10;
}
j++;
}
}
returnx;
}
//LLINT_test.cpp 测试程序
#include"LLINT.h"
intmain()
{
LLINTx("888777666555444333234567890987654321"),
y(100),z;
cout< 28、
}
//请在类模板体外定义成员函数及友元函数。【提示】可充分利用已有的函数。
//①(4分)前增量运算符函数重载
LLINT&LLINT::operator++()
{
a0++;
if(a0==BBILLION)
{
a0=0;
a1++;
}
return*this;
}
//②(4分)加赋值运算符函数重载
LLINT&LLINT::operator+=(constLLINT&x)
{
*this=*this+x;
return*this;
}
//③(4分)关系运算符(大于或等于)函数重载
booloperator>=(c 29、onstLLINT&x1,constLLINT&x2)
{
if(x1.a1>x2.a1)
returntrue;
elseif(x1.a1==x2.a1)
returnx1.a0>=x2.a0;
else
returnfalse;
}
//④(4分)关系运算符(等于)函数重载
booloperator==(constLLINT&x1,constLLINT&x2)
{
return(x1.a1==x2.a1)&&(x1.a0==x2.a0);
}
//⑤(4分)关系运算符(不等于)函数重载
booloperator!=(constLLINT&x1,constLLINT&x2)
{
return(x1.a1!=x2.a1)||(x1.a0!=x2.a0);
}
收集于网络,如有侵权请联系管理员删除






