1、最新上海大学1516级C+试题上海大学20152016学年度秋季学期试卷(A卷)课程名: 面向对象程序设计 课程号: 08305121 学分: 5 应试人声明: 我保证遵守上海大学学生手册中的上海大学考场规则,如有考试违纪、作弊行为,愿意接受上海大学学生考试违纪、作弊行为界定及处分规定的纪律处分。应试人 应试人学号 应试人所在院系 题号一(20)二(20)三(20)四(40)得分一、判断题(每小题2分,共20分)1. 类的构造函数的函数名与类名相同,可以重载构造函数。 ()2. 类的析构函数可以被重载。 ()3. 重载运算符函数不能改变运算符的操作数个数、优先级和结合方向。 ()4. 引用在声
2、明时必须对其初始化,以绑定某个已经存在的变量(或对象),在该引用的生命期内,该绑定不能被更改。 ()5. 指针变量在定义时必须对其初始化,以锁定某个已经存在的目标变量(或对象),在该指针变量的生命期内,该指向不能被更改。 ()6. 类的非静态成员函数均有一个隐含的形式参数this指针常量,用于指向调用该函数的对象。函数体中不能改变该指针常量的指向(即锁定调用该函数的对象)。 ()7. 派生类继承了基类的所有数据成员,并且在派生类的成员函数中都能直接访问基类的访问属性为private的成员。 ()8. 构造派生类对象时,先调用基类的构造函数,后执行派生类的构造函数。析构派生类对象时,先调用基类的
3、析构函数,后执行派生类的析构函数。 ()9. 含纯虚函数的类称为抽象类,不能创建抽象类的对象,不能定义抽象类的指针变量,不能声明抽象类的引用。 ()10. 引用返回的函数可以作左值,也避免了函数值返回时创建与返回类型相同的临时无名对象。 ()二、填空题(每空2分,共20分)如下设计了一个字符串类String,请根据运行结果,完成程序。#include #include #include using namespace std ;class Stringpublic: String(const char *str=) size = strlen( str ); x = size0 ? new c
4、harsize : NULL; if(x=NULL) size = 0; for(int i=0; isize; i+) xi = stri; String(const String &s) : x( NULL ) *this = s; / 直接利用深赋值运算符函数 virtual String() 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 charsiz
5、e; if(x=NULL) size = 0; for(int i=0; isize; i+) xi = s.xi; return *this ; char & operator(int index) return xindex; friend ostream & operator(ostream &out, const String &s) for(int i=0; is.size; i+) out (istream &in, String &s) string str; in str; / 利用C+字符串 s = String(str.c_str(); / 利用深赋值运算符 return
6、in; friend int Compare(const String &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; else return 0; friend bool operator(const String &s1, const String &s2)
7、 return Compare(s1, s2) 0; friend bool operator=(const String &s1, const String &s2) return Compare(s1, s2) (const String &s1, const String &s2) return Compare(s1, s2) 0; friend bool operator=(const String &s1, const String &s2) return Compare(s1, s2) = 0; friend bool operator=(const String &s1, con
8、st String &s2) return Compare(s1, s2) = 0; friend bool operator!=(const String &s1, const String &s2) return Compare(s1, s2) != 0;protected: char *x; int size;void display(const String &s1, const String &s2) char *str = 小于, 等于, 大于; cout s1 str1+Compare(s1, s2) s2 t endl;int main() String s1(Hello wo
9、rld!), s2(s1);运行结果Hello world! 等于 Hello world!Hello world! 小于 hello world!Hello world! 大于 Hello world Hello world! 大于 Hello world 等于 display(s1, s2); s20 = h; display(s1, s2); s2 = Hello world ; display(s1, s2); s2 = Hello world; display(s1, s2); s1 = ; s2 = ; display(s1, s2); return 0;三、阅读程序写出运行结果(
10、每行1分,共20分)3.1(10分)本题所涉及的Time类,相关头文件和源程序文件如下。/ MyTime.h 头文件#ifndef MYTIME_H#define MYTIME_H#include #include using namespace std;class Timepublic: Time(int hour=0, int minute=0, int second=0); Time & operator+(); Time operator+(int); friend Time operator+(const Time &t, int n); friend ostream & opera
11、tor(istream &in, Time &t);protected: int h, m, s;#endif/ MyTime.cpp 源程序文件#include MyTime.hTime:Time(int hour, int minute, int second) : h(hour), m(minute), s(second) / 构造函数Time & Time:operator+() s+; if(s=60) s = 0; m+; if(m=60) m = 0; h+; if(h=24) h = 0; return *this;Time Time:operator+(int) Time t
12、emp(*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;ostream & operator(ostream &out, const Time &t) out setfill(0)
13、 setw(2) t.h : setw(2)t.m : setw(2)t.s (istream &in, Time &t) char str200; in.getline(str, 200, :); t.h = atoi(str); in.getline(str, 200, :); t.m = atoi(str); in.getline(str, 200); t.s = atoi(str); return in;运行结果(3.1)23:59:51 23:59:51 23:59:52 23:59:50 23:59:51 23:59:51 22:59:50 请输入时间:23:59:5923:59:
14、59 00:00:00 10:20:30 / main.cpp 源程序文件(测试程序)int main() Time t0(23,59,50), t; t=t0; cout +t endl; t=t0; +t; cout t endl; t=t0; +t; cout t endl; t=t0; cout t+ endl; t=t0; t+; cout t endl; t=t0; t+; cout t endl; t=t0; t=t+(-3600); cout t endl; cout t; cout t endl; cout +t endl; cout t + (10*60+20)*60+30
15、 endl; return 0;3.2(10分)以下4小题所涉及的Test1类,相关头文件和源程序文件如下。/ test03.h 头文件#ifndef TEST03_H#define TEST03_H#include using namespace std;class Test1public: Test1(int a=0); Test1(const Test1 &t); virtual Test1(); Test1 & operator=(const Test1 &t); static int Num(); static int Sum(); friend ostream & operator
16、(istream &in, Test1 &t);protected: static int num, sum; int x;void Show(); / 普通的C+函数声明#endif/ Test03.cpp 源程序文件#include Test03.hint Test1:num=0, Test1:sum=0; /静态数据成员定义及初始化Test1:Test1(int a):x(a) / 构造函数 num+; sum+=x;Test1:Test1(const Test1 &t):x(t.x) / 拷贝构造函数 num+; sum+=x;Test1:Test1() num-; sum-=x;Te
17、st1 & Test1:operator=(const Test1 &t) / 赋值运算符函数 sum += t.x - x; x = t.x; return *this;int Test1:Num() return num;int Test1:Sum() return sum;ostream & operator(ostream &out, const Test1 &t) out (istream &in, Test1 &t) int temp; in temp; Test1:sum += temp - t.x; t.x = temp; return in;void Show() / 普通的
18、C+函数 cout Num = Test1:Num() ,tSum = Test1:Sum() endl;/ 3.2.1 测试程序之一#include Test03.h运行结果(3.2.1)Num = 0 , Sum = 0 int main() Show(); return 0;/ 3.2.2 测试程序之二#include Test03.hTest1 x(100); / 创建一个全局对象void f(Test1 t) Show();运行结果(3.2.2)Num = 1 , Sum = 100 Num = 2 , Sum = 200 Num = 1 , Sum = 100 int main()
19、 Show(); f(x); Show(); return 0;/ 3.2.3 测试程序之三#include Test03.hvoid f(Test1 &t) Show();int main()运行结果(3.2.3)Num = 1 , Sum = 100 Num = 1 , Sum = 100 Num = 1 , Sum = 100 Test1 x(100); / 创建一个自动对象 Show(); f(x); Show(); return 0;/ 3.2.4 测试程序之四#include Test03.hint main()运行结果(3.2.4)Num = 4 , Sum = 60 Num =
20、 5 , Sum = 90 Num = 4 , Sum = 60 Test1 x(10),y(20),a2=x,y Show(); Test1 *p = new Test1; *p = 30; Show(); delete p; Show(); return 0;四、(40分)设计复数类。要求运行时得到指定的输出结果。 实现如下测试程序中用到的9个函数(每个函数3分。无须定义拷贝构造函数、析构函数及赋值运算符函数); 自选3个运算符,并实现运算符函数重载(每个函数3分。注意复数不能比较大小); 数据成员、类设计的其他部分(4分)。【注意:数学函数double atan2(double y, d
21、ouble x);当x0时返回y/x的反正切值,当x=0时返回/2或-/2(正负号与y同号)】。#include Complex.hint main() Complex x(3,4), y(x), z; / 创建对象 cout x , y , z endl; / 输出复数 cout x.Abs() t; / 计算复数的模长 z.Real() = z.Imag() = 1; / 设置复数的实部、虚部 cout z.Angle()*180/M_PI endl; / 计算复数的角度 cout x+z t; / 复数的算术运算: +、*,迭代赋值 *=运行结果(4.1)(3, 4), (3, 4),
22、(0, 0)5 45(4, 5) (-1, 7) (-7, 24) cout x*z t; cout (x*=x) endl; return 0;/ Complex.h#include #include using namespace std;class Complexpublic: Complex(double real=0, double imag=0):re(real),im(imag) double & Real() return re; double & Imag() return im; double Angle() const return atan2(im, re); doub
23、le 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; friend Complex operator*(const Complex &c1, const Complex &c2) Complex result; result.re = c1.re*c2.re - c1.im*c2.im; r
24、esult.im = c1.re*c2.im + c1.im*c2.re; return result; Complex & operator*=(const Complex &c) double x = re*c.re - im*c.im; im = re*c.im + im*c.re; re = x; return *this; friend ostream & operator(ostream &out, const Complex &c) out ( c.re , c.im ); return out; protected: double re, im;(2) 缺乏经营经验月生活费 人
25、数(频率) 百分比我们长期呆在校园里,对社会缺乏了解,在与生意合作伙伴应酬方面往往会遇上困难,更不用说商业上所需经历的一系列繁琐手续。他们我们可能会在工商局、税务局等部门的手续中迷失方向。对具体的市场开拓缺乏经验与相关的知识,缺乏从职业角度整合资源、实行管理的能力;我们大学生没有固定的经济来源,但我们也不乏缺少潮流时尚的理念,没有哪个女生是不喜欢琳琅满目的小饰品,珠光宝气、穿金戴银便是时尚的时代早已被推出轨道,简洁、个性化的饰品成为现代时尚女性的钟爱。因此饰品这一行总是吸引很多投资者的目光。然而我们女生更注重的是感性消费,我们的消费欲望往往建立在潮流、时尚和产品的新颖性上,所以要想在饰品行业有立足之地,又尚未具备雄厚的资金条件的话,就有必要与传统首饰区别开来,自制饰品就是近一两年来沿海城市最新流行的一种。图1-3 大学生偏爱的手工艺品种类分布(1) 专业知识限制附件(一):公司成功地创造了这样一种气氛:商店和顾客不再是单纯的买卖关系,营业员只是起着参谋的作用,顾客成为商品或者说是作品的作参与者,营业员和顾客互相交流切磋,成为一个共同的创作体月生活费 人数(频率) 百分比“碧芝”最吸引人的是那些小巧的珠子、亮片等,都是平日里不常见的。店长梁小姐介绍,店内的饰珠有威尼斯印第安的玻璃珠、秘鲁的陶珠、奥利的施华洛世奇水晶、法国
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1