ImageVerifierCode 换一换
格式:DOCX , 页数:21 ,大小:29.71KB ,
资源ID:14900790      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-14900790.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(11级OOP试题.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

11级OOP试题.docx

1、11级OOP试题上海大学20122013学年度秋季学期试卷课程名: 面向对象程序设计 课程号: 08305121 学分: 5 应试人声明: 我保证遵守上海大学学生手册中的上海大学考场规则,如有考试违纪、作弊行为,愿意接受上海大学学生考试违纪、作弊行为界定及处分规定的纪律处分。应试人 应试人学号 应试人所在院系 题号一(20)二(30)三(26)四(24)得分一、判断题(每小题2分,共20分)1. 任何类都一定有默认的构造函数(即调用时无须实参的构造函数)。 ()2. 任何类都至少有一个赋值运算符函数。 ()3. 执行赋值运算时由系统自动调用复制构造函数。 ()4. 对象的空间通常由其基本空间和

2、资源空间构成。 ()5. 类的静态数据成员占用具体对象的存储空间。 ()6. 类的成员函数对同类的所有对象的所有数据成员有无限制的访问能力。 ()7. 设函数f是类A的友元函数,类A是类B的友类,此时函数f却不一定是类B的友元函数。 ()8. 基类中的私有成员(访问属性为private)在派生类中不可直接访问,须通过基类的非私有成员函数进行访问。 ()9. 设有类class A; 有指针变量A *p;,语句 p = new A; 或语句p=(A*)calloc(1, sizeof(A); 都将引起类A的默认构造函数调用。 ()10. 类的构造函数、析构函数都可以为虚函数。 ()二、填空及简答(

3、填空20分、简答题10分,共30分)如下矩阵类的设计。(1) 请根据如下程序及其运行结果填空(每空2分)。#include #include / 为了控制I/O的格式using namespace std;template class Matrixprotected: int row, col; T *x;public: Matrix(int Row=0, int Col=0, const T *mat=NULL) : row(Row), col(Col), x( NULL ) if(Row=0 | Col=0; i-) xi = (mat!=NULL) ? mati : T(0); Matr

4、ix(const Matrix &mat) : x(NULL) *this = mat; virtual Matrix() if(x!=NULL) delete x ; Matrix & operator=(const Matrix &mat) if( &mat = this ) return *this; if(x!=NULL) delete x; row = mat.row; col = mat.col ; if(row=0 | col=0; i-) xi = mat.xi; return *this ; int Row() const return row; int Col() cons

5、t return col; friend ostream & operator(ostream &out, const Matrix &mat) for(int i=0; imat.row; i+) out ; for(int j=0; jmat.col; j+) out setw(6) mat.xi*mat.col+j; out endl; return out ; T * operator(int index) return x + index*col; ;int main() Matrix a(3, 4); Matrix c(2, 5, HelloWorld);运行结果 H e l l

6、o W o r l d 0 0 0 0 0 0 0 0 0 0 0 0 0 1 2 3 1 2 3 4 2 3 4 5 cout c endl; cout a endl; for(int i=0; ia.Row(); i+) for(int j=0; ja.Col(); j+) aij = i+j; cout a endl; return 0;(2) 请根据上述程序简要回答问题。(2.1) (3分)复制构造函数中为什么首先要将指针x初始化为NULL?与其后执行的语句有什么关系?复制构造函数体中使用了赋值运算符函数,而赋值运算符函数中若x不为NULL,则执行delete操作。若不将x初始化为NU

7、LL,其值可能不可预知,且可能不为NULL,直接执行delete将出错。(2.2) (3分)为什么将运算符函数operator重载为友元函数?因为运算符函数operator为双目运算,且第一个操作数不是本类的对象,不能将其定义为成员函数,而将其定义成类的友元函数。(2.3) (4分)在主函数测试时,为什么可以使用双方括号(如aij),请分别解释其中第一个方括号、第二个方括号的理由。表达式aij中,第一个方括号为该类重载的方括号运算符函数,它返回所谓的“行地址”的地址值;第二个方括号为系统提供的根据地址访问目标变量运算。三、阅读程序写出运行结果及修改函数(共26分)(1) 有如下头文件Compl

8、ex.h,其中设计了一个复数类#ifndef COMPLEX_H / Complex.h 头文件#define COMPLEX_H#include using namespace std;class Complexprivate: double re, im; / 复数的实部、虚部public: Complex(double real=0, double imag=0) : re(real), im(imag) double Real()const return re; double Imag()const return im; void Set(double real, double ima

9、g) re=real; im=imag; friend ostream & operator(ostream &out, const Complex &c) out ( c.re , c.im (istream &in, Complex &c) double real, imag; char str100; in.getline(str, 100, (); / 读到左圆括号为止 in.getline(str, 100, ,); / 读到逗号为止 real = atof(str); / 将C-字符串转换成浮点数 in.getline(str, 100, ); / 读到右圆括号为止 imag =

10、atof(str); / 将C-字符串转换成浮点数 c.Set(real, imag); / 设置复数 return in; Complex operator+(const Complex &c) Complex temp; temp.re = re + c.re; temp.im = im + c.im; return temp; Complex & operator+() re+; / 复数增1(仅实部增1) return *this; Complex operator+(int) Complex temp(*this); re+; return temp; Complex & opera

11、tor*=(const Complex &c) Complex temp(*this); re = temp.re * c.re - temp.im * c.im; im = temp.re * c.im + temp.im * c.re;运行结果(1.1)(0, 0) (2, 0) (2, 3)(4, 3) (4, 0) (-5, -9) return *this; ;#endif(1.1) (4分)请指出下面的程序的运行结果#include Complex.hint main() Complex c1, c2 = 2, c3(2, 3); cout c1 t c2 t c3 endl; c

12、1 = c2 + c3; c2 *= c2; / 请严格按上述 operator*= 函数计算 c3 *= c3; / 请注意(23i)(23i) =512i 并不等于 59i cout c1 endl; cout c2 endl; cout c3 endl; / 输出(-5, -9)表示c3为59i return 0;(1.2)(4分)从上述运行结果可见,重载运算符*=函数的定义中存在计算错误,请改正。由此可见重载双目运算符时,需要特别注意两个操作数为同一对象时的情形。 Complex & operator*=(const Complex &c) Complex temp; temp.re

13、= re * c.re - im * c.im; temp.im = re * c.im + im * c.re; *this = temp; return *this; (1.3) (4分)请指出下面的程序的运行结果#include Complex.hint main() Complex c(2, 3), d(c);运行结果(1.3)(3, 3) (3, 3) (2, 3) (3, 3) (4, 3) (4, 3) (2, 3) (3, 3) cout +c t; / 此处无换行 cout c endl; cout d+ t; / 此处无换行 cout d endl; c.Set(2, 3)

14、; d.Set(2, 3); cout +c t; / 此处无换行 cout c endl; cout d+ t; / 此处无换行 cout d endl; return 0;(1.4) (4分)请指出下面的程序的运行结果#include Complex.hint main()运行结果(1.4)(2, 3) (2, 3) (4, 5) (2, 3) Complex c1, c2(2, 3), *p; cout (c1=c2) endl; cout c2 endl; p = new Complex(4, 5); cout *p endl; delete p; p = &c1; cout *p e

15、ndl; return 0;(2) 有如下程序#include using namespace std;class Baseprotected: int x, y;public: Base(int a, int b=1) : x(a), y(b) virtual Base() virtual void Show(ostream &out) const = 0;ostream &operator(ostream &out, const Base &b) b.Show(out); return out;class Derived : public Baseprivate: int z;public

16、: Derived(int a, int b, int c) : Base(a, b), z(c) Derived &operator+=(const Derived &d) x += d.x; y += d.y; z += d.z; return *this; void Show(ostream &out) const out ( x , y ), z ; ;class RationalNumber : public Base / 派生有理数类public: RationalNumber(int a, int b=1) : Base(a, b) RationalNumber &operato

17、r+=(const RationalNumber &r) / 有理数(分数)相加 int temp = x*r.y + y*r.x; y *= r.y; x = temp; return *this; void Show(ostream &out) const out x / y; ;运行结果(3, 2), 1 (4, 5), 6 (7, 7), 7 3/2 2/3 13/6 int main() Derived d1(3, 2, 1), d2(4, 5, 6); RationalNumber r1(3, 2), r2(2, 3); cout d1 endl; cout d2 endl; d1

18、 += d2; cout d1 endl; cout r1 endl; cout r2 endl; r1 += r2; cout r1 endl; return 0;(2.1) (6分)请指出上述程序的运行结果(填入上面的表格中)。(2.2) (2分)简要回答。基类Base中有纯虚函数,故Base为抽象类。不能创建Base类的对象。能否声明Base类的引用(例如用派生类的对象初始化引用)?能否定义目标数据类型为基类的指针变量(例如用派生类对象的地址对其初始化或赋值)? 答:皆能。(2.3) (2分)简要回答。派生类Derived或RationalNumber类的构造函数中为什么要显式地使用冒号

19、语法规则对基类的数据成员进行初始化。 答:因为基类Base没有默认的构造函数。四、设计类(每个函数4分,共24分)一个IPv4(Internet Protocol version 4)地址用一个32位(即unsigned long型)整数表示。常用点分十进制(dotted-decimal notation)的形式,即其4个字节被分开用十进制写出,中间用点分隔。例如某IPv4地址的点分十进制表示为192.168.100.1,则其对应的32位整数为(c0a86401)16即3232261121=(192*256+168)*256+100)*256+1。请完成如下的成员函数或友元函数的定义,使程序输

20、出指定的结果。#include #include using namespace std;class IPv4private: int a, b, c, d; / 利用4个整数记录被点分的数public: IPv4(char *ip=127.0.0.1) / 构造函数 int i; a = b = c = d = 0; for(i=0; ipi!=.; i+) if(ipi=0 & ipi=9) a = 10*a + ipi-0; for(i+; ipi!=.; i+) / 请注意此处为i+ if(ipi=0 & ipi=0 & ipi=0 & ipi (const IPv4 &ip1, co

21、nst IPv4 &ip2) return ip1.get() ip2.get(); / get函数见后 friend bool operator=(const IPv4 &ip1, const IPv4 &ip2) return ip1.get() = ip2.get(); friend bool operator (const IPv4 &ip1, const IPv4 &ip2) return ip1.get() ip2.get(); friend bool operator=(const IPv4 &ip1, const IPv4 &ip2) return ip1.get() = ip

22、2.get(); friend bool operator=(const IPv4 &ip1, const IPv4 &ip2) return ip1.get() = ip2.get(); friend bool operator!=(const IPv4 &ip1, const IPv4 &ip2) return ip1.get() != ip2.get(); IPv4 & operator-() / 重载前减量运算符 d-; if(d=-1) d=255; c-; if(c=-1) c=255; b-; if(b=-1) b=255; a-; if(a=-1) a=255; return

23、*this; IPv4 operator-(int) / 重载后减量运算符 IPv4 temp(*this); -(*this); return temp; IPv4 operator+(int n) / 重载加号运算符,实现IP地址与整数相加 IPv4 temp; temp.set(get()+n); return temp; IPv4 & operator+=(int n) / IPv4对象与整数迭代相加 *this = *this + n; return *this; IPv4 operator-(int n) / 重载减号运算符,实现IP地址与整数相减 return *this+(-n

24、); IPv4 & operator-=(int n) / IPv4对象与整数迭代相减 *this = *this + (-n); return *this; int operator-(const IPv4 &ip) / 两个IPv4对象相减 return get() - ip.get(); / 请根据要求定义成员函数或友元函数(请注意:这是在类体中)。/ 请定义如下函数,返回由a,b,c,d构成的一个32位的无符号长整型数值,/ 该函数是前面诸多成员函数的基础。 unsigned long get() const unsigned long int n; n = d + 256*(c + 256*(b + 256UL*a); return n; / 请定义如下函数,根据给定的32位无符号长整型数值设置数据成员a,b,c,d。 void set(unsigned long n) d = n % 256; c = n/256 % 256; b = n/256/256 % 256; a = n/256/256/256 % 256; / 请重载前增量运算符(+)使其为下一个IP(参见测试程序及运行结果) IPv4 & operat

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2