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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

上海大学级C++试题.docx

1、上海大学级C+试题应试人(V)(x)(V)(V)(x)(V)(x)(x)上海大学20152016学年度秋季学期试卷(A卷)绩课程名:面向对象程序设计 课程号:08305121学分:5应试人声明:我保证遵守上海大学学生手册中的上海大学考场规则,如有考试违纪、作 弊行为,愿意接受上海大学学生考试违纪、作弊行为界定及处分规定的纪律处分。题号一 (20)二(20)三(20)四(40)得分应试人学号应试人所在院系2分,共20分)1.类的构造函数的函数名与类名相同,可以重载构造函数。2.类的析构函数可以被重载。3.重载运算符函数不能改变运算符的操作数个数、优先级和结合方向4.引用在声明时必须对其初始化,以

2、绑定某个已经存在的变量(或对象),在该引用的生命期内,该绑定不能被更改5.指针变量在定义时必须对其初始化,以锁定某个已经存在的目标变量(或 对象),在该指针变量的生命期内,该指向不能被更改。6.类的非静态成员函数均有一个隐含的形式参数 this 指针常量,用于指向调用该函数的对象。函数体中不能改变该指针常量的指向(即锁定调用该函数的对象)。7.派生类继承了基类的所有数据成员,并且在派生类的成员函数中都能直接访问基类的访问属性为private 的成员。8.构造派生类对象时,先调用基类的构造函数,后执行派生类的构造函数 析构派生类对象时,先调用基类的析构函数,后执行派生类的析构函数。9.含纯虚函数

3、的类称为抽象类,不能创建抽象类的对象,不能定义抽象类的 指针变量,不能声明抽象类的引用。10.引用返回的函数可以作左值,也避免了函数值返回时创建与返回类型相同11.得 分的临时无名对象二、填空题(每空2分,共20分)如下设计了一个字符串类String 请根据运行结果,完成程序。#in elude #in elude using n amespaee std ; class Stringpublic :String( const char *str=) size = strle n( x = size0 ? str );new char size : NULL;if (x=NULL) size

4、= 0; for (int i=0; isize; i+) xi = stri; NULL )/直接利用深赋值运算符函数String( const String &s) : x( * this = s;virtual Stri ng()if (x!=NULL) delete x;size = 0;String & operator =( const String &s)if (this = &s ) return * this if (x!=NULL) delete_ x;size = s.size;x = new char size;if (x=NULL) size = 0;for (int

5、 i=0; isize; i+)xi = s.xi;return *this ; char & operator ( int index) return xindex; friend ostream & operator (ostream &out, const Stri ng &s)for (int i=0; ivs.size; i+)out (istream &n, Stri ng &s)string str;in str; /利用C+字符串s = Stri ng(str.c_str(); /利用深赋值运算符return in;friend int Compare( const Strin

6、g &s1, const String &s2) int i;for (i=0; is1.size & is2.size & s1.xi=s2.xi; i+)if (is1.size & i s2.xi ? 1 : -1;else if (is1.size & i=s2.size)return 1;else if (i=s1.size & is2.size)return -1;elsereturn 0; protected :char *x;int size;void display( const String &s1, const String &s2)char *str = 小于, 等于,

7、 大于;cout si str1+Compare(s1, s2) s2 t endl; int3.1 (10分)本题所涉及的Time类,相关头文件和源程序文件如下/ MyTime.h 头文件#ifndef MYTIME_H#defi ne MYTIME_H#in clude #in clude using namespace std;class Timepublic :Time( int hour=0, int minu te=0, int sec on d=0);Time & operator +();Time operator +( int );friend Time operator +

8、( const Time &t, int n);frie nd ostream & operator (istream &n. Time & t);protected :int h, m, s;#en dif/ MyTime.cpp 源程序文件#in clude MyTime.hTime:Time(i nt hour, i nt minu te, int sec ond):h(hour), m(minu te), s(sec ond) / 构造函数Time & Time: operator +()s+;if (s=60) s = 0; m+; if (m=60) m = 0; h+; if (

9、h=24) h = 0;return * this ;Time Time: operator +( int )Time temp(* this );+(* this );return temp;Time operator +(const Time &t, int n)Time result(t);int x = (t.h*60 + t.m)*60 + t.s + n;while (x0)x += 24*60*60;x %= 24*60*60;result.s = x % 60;result.m = x/60 % 60;result.h = x/3600;return result;ostrea

10、m & operator (ostream &out, const Time &t)out setfill(0) setw(2) t.h : setw(2)t.m : setw(2)t.s (istream &n. Time &t)char str200;in .getli ne(str, 200, :);t.h = atoi(str);in .getli ne(str, 200, :);t.m = atoi(str);in.getline(str, 200);t.s = atoi(str);return in; 运行结果(3.1)/ mai n.cpp 源程序文件(测试程序)int mai

11、n()Time t0(23,59,50), t; t=tO; cout +t en dl; t=tO; +t; cout t en dl;t=t0; +t; cout t en dl; t=t0; cout t+ en dl;t=t0; t+; cout t en dl;t=t0; t+; cout t en dl;t=t0; t=t+(-3600); cout t endl; cout t;cout t en dl;cout +t en dl;cout t + (10*60+20)*60+30 endl; return 0;3.2 (10分)以下4小题所涉及的Test1类,相关头文件和源程序

12、文件如下。/ test03.h头文件#ifndefTEST03_H#defi neTEST03_H#in cludeusing namespace std;class Test1publicTest1( int a=0);Test1(const Test1&t);virtualTest1();Test1 &operator=(const Test1 &t)staticint Num();staticint Sum();frie nd ostream & operator (istream &in, Test1 &t);protected :static int num, sum;int x;v

13、oid Show(); /普通的C+函数声明#en dif/ Test03.cpp 源程序文件#in clude Test03.hint Testi: num=O, Test1:sum=0;Test1:Test1( int a):x(a)nu m+; sum+=x;Test1:Test1( const Testi & t):x(t.x) nu m+; sum+=x;Test1:Test1()nu m-; sum-=x;Testi & Testi: operatorsum += t.x - x;x = t.x; return * this ;int Test1:Num() int Test1:S

14、um() ostream & operatorout temp;Test1:sum += temp - t.x;t.x = temp; return in;void Show()cout Num = ,tSum =/ 3.2.1测试程序之一#in clude Test03.h/静态数据成员定义及初始化/构造函数/拷贝构造函数/普通的C+函数运行结果(3.2.1)Num = 0 , Sum = 0 Test1:Num() Test1:Sum() endl;int mai n()Show(); return 0;/ 322测试程序之二#in elude Test03.hTesti x(100);

15、/创建一个全局对象void f(Test1 t)Show();int mai n()Show(); f(x);Show(); return 0;/ 3.2.3测试程序之三#in elude Test03.hvoid f(Test1 &t) Show();int mai n()运行结果(3.2.2)Num = i , Sum = i00Num = 2 , Sum = 200Num = i , Sum = i00Testi x(100); / 创建一个自动对象Show();f(x);Show();return 0;/ 3.2.4测试程序之四#in elude Test03.hint mai n()

16、Testi x(10),y(20),a2=x,yShow();Testi *p = new Testi;*p = 30;Show();delete p;Show();运行结果(3.2.3)Num = i , Sum = i00Num = i , Sum = i00Num = i , Sum = i00运行结果(3.2.4)Num = 4 , Sum = 60Num = 5 , Sum = 90Num = 4 , Sum = 60return 0;得 分(40分)设计复数类。要求运行时得到指定的输出结果 实现如下测试程序中用到的9个函数(每个函数3分。无须定义拷贝构造函数、析【注意:数学函数do

17、uble atan2( double y,doublex);当xM0时返回y/x的反正切值,当x=0时返回n2或-n2 (正负号与y同号)】。#in clude Complex.hint mai n() IComplex x(3,4), y(x),乙/创建对象cout x , y , z en dl;/输出复数cout x.Abs() t;/计算复数的模长z.Real() = z.lm ag() = 1;/设置复数的实部、虚部cout z.A ngle()*180/M_PI endl;/计算复数的角度构函数及赋值运算符函数);2自选3个运算符,并实现运算符函数重载(每个函数3分。注意复数不能比

18、较大小);3数据成员、类设计的其他部分(4分)。/复数的算术运算:+、*,迭代赋值*=cout X+Z t; cout x*z t;cout (x*=x) en dl; return 0;/ Complex.h#include #in clude 运行结果(4.1)(3, 4), (3, 4), (0, 0)5 45(4, 5) (-1, 7) (-7, 24)using n amespace std;class Complexpublic:Complex(double real=0, double imag=0):re(real),im(imag)double & Real() return

19、 re; double & lmag() return im; double An gle() const retur n ata n2(im, re); double Abs() const return sqrt(re*re + im*im); friend Complex operator+(const Complex &c1, const Complex &c2)Complex result(c1);result.re += c2.re;result.im += c2.im;return result;&c2)friend Complex operator*(const Complex

20、 &c1, const ComplexComplex result; result.re = c1.re*c2.re - c1.im*c2.im;result.im = c1.re*c2.im + c1.im*c2.re;return result;Complex & operator* onst Complex &c)double x = re*c.re - im*c.im;im = re*c.im + im*c.re;re = x;return *this;frie nd ostream & operator(ostream &out, const Complex &c)out ( c.re , c.im );return out;protected:double re, im;

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

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