资源描述
上海大学14-15级C++试题-DOC
精品文档
成
绩
上海大学2014~2015学年度秋季学期试卷(A卷)
课程名:面向对象程序设计课程号:08305121学分:5
应试人声明:
我保证遵守《上海大学学生手册》中的《上海大学考场规则》,如有考试违纪、作弊行为,愿意接受《上海大学学生考试违纪、作弊行为界定及处分规定》的纪律处分。
应试人应试人学号应试人所在院系
题号
一(20)
二(30)
三(25)
四(25)
得分
得
分
——————————————————————————————————————
一、判断题(每小题2分,共20分)
1. 引用在声明时必须对其初始化,以绑定某个已经存在的变量(或对象),
在该引用的生命期内,该绑定不能被更改。 (√)
2. 指针变量在定义时必须对其初始化,以锁定某个已经存在的目标变量(或
对象),在该指针变量的生命期内,该指向不能被更改。 (×)
3. 值返回的函数(如:doublesqrt(double);)的调用表达式(如:
sqrt(2.0))代表一个无名的临时变量(或对象),一般不将其用作左值。 (√)
4. 引用返回的函数,可以返回该函数中值传递的形参变量(或对象)。 (×)
5. 任何类都有构造函数、复制构造函数、析构函数、赋值运算符函数。 (√)
6. 有静态数据成员的类,一般地应该考虑为其设计复制构造函数、析构函数。 (√)
7. 将用于输出的插入运算符函数operator<<设计成友元函数的根本原因是
因为进行输出操作时需要访问对象的内部数据成员。 (×)
8. 在C++程序中,操作符new的功能与calloc函数的功能完全一样。 (×)
9. 创建一个C++字符串对象(如:stringstr;),则sizeof(str)的值
等于str.length()的值。其中成员函数length为返回字符串的长度。 (×)
10. 基类的私有数据成员在派生类中是存在的,但不可直接访问,需要用从基类
继承下来的函数访问。 (√)
得
分
二、填空题(每空2分,共30分)如下设计了结点类Node、链表类LinkList,并将链表类作为结点类的友类,请根据运行结果,完成程序。
#include<iostream>
usingnamespacestd;
class①LinkList; //提前声明
classNode
{
public:
Node(intx=0):data(x){}
②friendclassLinkList;
private:
intdata;
Node③*next;
};
classLinkList
{
private:
Node*head;
public:
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();
}
⑦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=NULL;
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<<"->"<<p->data;
out<<"->NULL";
}
};
ostream&operator<<(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<<linkA<<'\n'<<linkB<<'\n'<<linkC<<endl;
linkC=linkA;
cout<<linkC<<endl;
return0;
}
得
分
三、阅读程序写出运行结果(每行1分,共25分)
1.(7分)有关构造与析构的顺序
#include<iostream>
usingnamespacestd;
classTest
{
public:
Test(inta=0,intb=0):x(a),y(b){}
~Test()
{
if(x==y)
cout<<"数据成员的值相同,都等于"<<x<<endl;
else
cout<<"数据成员的值不同,分别为"<<x<<","<<y<<endl;
}
friendostream&operator<<(ostream&out,constTest&t)
{
out<<"("<<t.x<<","<<t.y<<")";
returnout;
运行结果(1)
(10,0)
(0,0)
(2,3)
数据成员的值不同,分别为10,0
退出程序,返回操作系统
数据成员的值不同,分别为2,3
数据成员的值相同,都等于0
}
private:
intx,y;
};
intmain()
{
Test*p,t1;
p=newTest(10);
Testt2(2,3);
cout<<*p<<'\n'
<<t1<<'\n'
<<t2<<endl;
deletep;
cout<<"退出程序,返回操作系统"<<endl;
return0;
}
2.以下两小题所涉及的类设计,头文件如下。
//test.h
#include<iostream>
#include<cmath>
usingnamespacestd;
classBASE
{
public:
BASE(doublex=0,doubley=0):_x(x),_y(y){}
virtualvoidShow(ostream&out)const=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';
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,constComplex&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<<')';
}
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<<x<<'\n'<<y<<'\n'
<<z1<<'\n'<<z2<<endl;
cout<<"y.abs():"<<y.abs()<<endl;
z1=x+y;
z2=x*y;
cout<<z1<<'\n'<<z2<<endl;
Pointa(1,2),b(3,4);
cout<<a<<'\n'<<b<<endl;
cout<<a+b<<endl;
return0;
}
2.2(8分)测试程序
#include"test.h"
intmain()
{
运行结果(2.2)
1+2i
6+2i
5+10i
2+4i
(1,2)
(2,2)
(2,2)
(4,8)
Complexx(1,2),y,z;
y=5+x;
z=5*x;
cout<<x<<'\n'<<y<<'\n'<<z<<endl;
cout<<x+x<<endl;
Pointa(1,2),b,c;
b=a+1;
c=1+a;
cout<<a<<'\n'<<b<<'\n'<<c<<endl;
a=a+a;
cout<<a+a<<endl;
return0;
}
得
分
四、完成如下类的设计(25分)在GCC编译系统中,unsignedlonglong数据类型使整型数的取值范围得到扩展(,即0~18446744073709551615)。为了进一步扩展非负整数的取值范围设计了如下的类。该类数据可精确计算至,可处理36~37位非负十进制整数。请在类的声明体外实现5个尚未定义的成员函数或友元函数。最后写出程序的运行结果(每个函数定义4分,运行结果5分)。
//LLINT.h 头文件
#ifndefLLINT_H
#defineLLINT_H
#include<iostream>
usingnamespacestd;
classLLINT
{
public:
LLINT(unsignedlonglongx0=0,unsignedlonglongx1=0);
//第一参数为低位
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);
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&x1,constLLINT&x2);
protected:
staticconstunsignedlonglongBBILLION;
unsignedlonglonga1,a0;
//a1*1000000000000000000+a0可表示36~37位十进制非负整数
};
#endif
//LLINT.cpp 源程序文件
#include"LLINT.h"
#include<iomanip>
constunsignedlonglongLLINT::BBILLION=
//静态常量数据成员的定义及初始化(10^18)
LLINT::LLINT(unsignedlonglongx0,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;
}
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<<x.a1<<setfill('0')<<setw(18)<<x.a0<<setfill('');
else
out<<x.a0;
returnout;
}
istream&operator>>(istream&in,LLINT&x)
{
charstr[200];
in>>str;
x=atoLLINT(str);
returnin;
}
booloperator>(constLLINT&x1,constLLINT&x2)
{
if(x1.a1>x2.a1)
returntrue;
elseif(x1.a1==x2.a1)
returnx1.a0>x2.a0;
else
returnfalse;
}
booloperator<(constLLINT&x1,constLLINT&x2)
{
if(x1.a1<x2.a1)
returntrue;
elseif(x1.a1==x2.a1)
returnx1.a0<x2.a0;
else
returnfalse;
}
booloperator<=(constLLINT&x1,constLLINT&x2)
{
if(x1.a1<x2.a1)
returntrue;
elseif(x1.a1==x2.a1)
returnx1.a0<=x2.a0;
else
returnfalse;
}
LLINTatoLLINT(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');
p1*=10;
}
j++;
}
}
returnx;
}
//LLINT_test.cpp 测试程序
#include"LLINT.h"
intmain()
{
LLINTx("888777666555444333234567890987654321"),
y(100),z;
cout<<x<<'\n'<<y<<'\n'<<z<<endl;
z="999999999999999999";
cout<<z++<<endl;
cout<<z<<endl;
运行结果
100
0
return0;
}
//请在类模板体外定义成员函数及友元函数。【提示】可充分利用已有的函数。
//①(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>=(constLLINT&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);
}
收集于网络,如有侵权请联系管理员删除
展开阅读全文