4 C++面向对象程序设计A卷 和参考答案.docx
座位号:C+面向对象程序设计(甲)(A )卷参考答案注意:答案直接写在答题纸上,答在试卷上无效,考试后答题纸和试卷一同上交 一、单项选择题(每题2分,总计20分)2.阅读该程序,给出程序的输出结果。题号12345答案CDADA题号678910答案CCCBD二、程序填空题(每空2分,总计20分)(1) date d,int day(2) day>dys(d.mo-l|(3) temp.n=n+s.n(4) s3.disp()(5) A(i+10) 或 A (6普)(6) dispaO(7) (virtual void f()(8) f()(9) bool opcrator()(int x) (10) count_if(a.beRin(),a.end(),Pass()三、阅读程序题(每题6分,总计30分)1.阅读该程序,给出程序的输出结果。n=10.k=3n=20,k=3n=30,k=33.阅读该程序,给出程序的输出结果。54.12China4.阅读该程序,给出程序的输出结果。anihCevoL5.阅读该程序,给出程序的输出结果。8/3 = 26/0= 6 is divided by zero!That is ok.座位号:四、编程题(每题15分,总计30分)1.按照要求,编写程序。评分标准:要求采用抽象类和类继承,假设不采用那么扣分。#include <iostream>using namespace std;雇员类class Employee protected :int _id;double .salary;string _namc;public :Employee (int id, string name) :_id(id), _name (name) virtual -Employee () virtual double CalcSalary () = 0;);经理类class Manager : public Employee public :Manager (int id, string name): Employee (id.namc) virtual double CalcSalary () _salary = 800(); return _salary;h ; 兼职技术员类 class Technician : public Employee int _workHour;public :Technician (int id, string name): Employee (id,nanie) void SctWorkHour (int workHour) _workllour = workHour; virtual double CalcSalary () _salary = 100 * _workHour; return _salary;;销售经理类 class Salesman : public Employee double _salesMount;public :Salesman (int id, string name): Employee (id,name) void SetSalesMount (double salesMount) _salesMount = salesMount;Ivirtual double CalcSalary () _salary = 0.04 * _salcsMount; return _salary;I;class SalesManager : public Employee double _salesTotalMount;public :SalesManager (int id, string name): Employee (id,name) void SetSalesTotalMount (double salesTotalMount) _salcsTotalMount = salesTotalMount;)virtual double CalcSalary () _salary = 5000 +0.005 * _salesTotalMount; return _salary;;int main()Manager zhang (1000, “张三“);Technician li (1000, “李四”);Salesman wang (1000.”王五”);SalesManager zhao (1000,"赵六”);li.SetVorkHour(50);wang.SetSalesMount( 175000);zhao.SetSalesTotalMount( 1500000);Employee *pEmployee? | = &zhang, &li, &wang, &zhao;for (int i = 0; i <4; i+) (cout « pEmployeeA i ->CalcSaIary() « endl;I座位号:2.按照要求,编写程序。评分标准:不可存在内存泄露。/include <iostream>include <vector>#includc <cstdlib>using namespace std;class Matrix public:Matrix (int row, int col); 构造 row x col 大小矩阵Matrix operator + (const Matrix &rhs) const"/相同大小矩阵相加Matrix operator - (const Matrix &rhs) const;相同大小矩阵相减 void Display () const;显示矩阵矩阵元素,可作为右值const int & at (int row, int col) const return _datas rowcol;矩阵元素,可作为左值int&at (int row, int col) return _datas rowcol;矩阵行数intGetRowCount () const return _datas.size ();I矩阵列数intGetColCount () const return _datas OJ.size ();private:存放矩阵元素,本类没有直接动态分配,无需重载拷贝构造和赋值 vector<vector<int> > _datas;;Matrix:Matrix (int row, int col)for (int i = 0;i< row; i+) vcctor<int> dataRow (col);_datas.push_back (dataRow);Matrix Matrix:operator + (const Matrix &rhs) const (Matrix result (this->GetRowCount(), this->GetColCount();for (unsigned int i = 0; i < _datas.sizc (); i+) for (unsigned int j = 0; j < _datas i.sizc (); j+) result.at(ij) = this->at(i, j) + rhs.at (i, j);return result;Matrix Matrix:operator - (const Matrix &rhs) const (Matrix result (this->GctRowCount(), this->GctColCount();for (unsigned int i = 0; i < _datas.sizc (); i+) for (unsigned int j = 0; j < _datas i.size (); j+) result.at(ij) = this->at(i, j) - rhs.at (i, j);return result;void Matrix:Display () const(for (unsigned int i = 0; i < _datas.sizc (); i+) for (unsigned int j = 0; j < _datas i.size (); j+) cout.width (5);cout« at (ij);cout« endl;)cout« endl;int main()(Matrix A (5,6), B (5,6);int i,j;for (i = 0; i < A,GetRowCount(); i+)for (j = 0; j < A.GetColCountO; j+)A.at(i, j) = rand () % 100 ;for (i = 0; i < A.GctRowCountO; i+)for (j = 0; j < A.GctColCountO; j+) B.at(i, j ) = rand () % 100;A. Display。;B.DisplayO;Matrix C = A + B;C.DisplayO;Matrix D = A- B;D.DisplayO;return 0;ch面向对象程序设计(甲)(A )卷注意:答案直接写在答题纸上,答在试卷上无效,考试后答题纸和试卷一同上交一、单项选择题(每题2分,总计20分)1 . 假设定义语句为const char *ptr;,ptr应该是。A.指向字符变量的指针B.指向字符的常量指针C.指向字符串常量的指针 1).指向字符串的常量指针2 .对定义重载函数的以下要求中,是错误的。A.要求参数的个数不同B.要求参数中至少有一个类型不同C.要求参数个数相同时,参数类型不同D.要求函数的返回值不同3 .有以下类的说明,请指出A、B、C、D四个语句错误的选项是oclass CSample int a=2.5;/ACSamplef );/Bpublic: CSamplefint val);/C-CSample();/D);4 .一个类的友元函数能够访问该类的»A.只有私有成员 B.只有保护成员 C.只有公有成员 D.所有成员5 .以下运算符中,运算符在C+中不能重载。A. ?:B. +C.-I). <=6 .以下对模板的说明,正确的选项是.A. teniplate<T>B. template<class T1,T2>C. teniplate<class Tl,class T2>D. template<class T1;class T2>7 .关于多继承二义性的描述中,是错误的。A. 一个派生类的两个基类中都有某个同名成员,在派生类中对这个成员的访问可能出现 二义性B.解决二义性的最常用的方法是对成员名的限定 C.基类和派生类中出现同名函数,也存在二义性问题 D. 一个派生类是从两个基类派生来的,而这两个基类又有一个共同的基类,对该基类成员进行访问时,也可能出现二义性 8.以下基类中的成员函数,图个表示纯虚函数。A. virtual void vf(int);B. void vf(int)=0;C. virtual void vf()=0;D. virtual void vf(int) 9.在C+中,翻开一个文件就是将这个文件与一个建立关联;关闭一个文件就取消这种关联。 A.类B.流C.对象D.结构10 现有语句 int iarray = 0,1,2,3,4,5,6, 6, 6, 7, 8 ; vector<int> ivector(iarray, iarray + sizeof(iarray) / sizeof(int); 请问要找出ivector之中小于7的元素个数,*用下面哪个算法。A. find_ifB. countC. sortI). count_if二、程序填空题(每空2分,总计20分)1.请在下面程序的横线处境上适当内容,以使程序完整,并使运行结果为:2/10/2004 #include <i()stream.h> static int dys|=31,283,30,31,303,31,30JI刖,31; class date int mo,da,yr; public:date(int m,int d,int y) mo=m;da=d;yr=y; date() void dispO cout«mo«,7"«da«,7"«yr«cndl; void leapt int a) if(a%4=0&&a%100!=0|a%400=0) dysl=29; else dysl=28; friend date operator+(I) day+=d.da; d.leap(d.yr); while(2) d.lcap(d.yr); day-=dys(i.mo-l; if(+d.mo=13) d.mo=l; d.yr+; d.da=day; return d;); int main() dated 1(2,10,2003)/2;int n;cin»n; 从键盘输入365 whi!e(n<O)cin»n;d2=dl+n;d2.disp();.请在下面程序的横线处境上适当内容,以使程序完整,并使程序的输出为:n=30 /include <iostream.h>template <class T> class Sample Tn;public:Sample()Sample。i)n=i;Sample<T> <)perator+(c<)nst Sample<T> &); void disp()(cout«,'n="«n«endl;;templateclass T>Sample<T> Sample<T>:operator+(const Sample<T> &s) ( Sample<T> temp;(3); return temp;) int mainO Sample<int> sl(10)2(20)s3;s3=sl+s2;(4);.请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为 A Constructor2B ConstructorZxl=12x2=2#include <iostream.h>class A(public:A()cout«nA Constructorr'«cndl;A(int i)(xl=i;cout«"A Constructor2,'«endi;void dispa()cout«"xl=',«xl«endl; private:int xl;;class B:public A (public:B()cout«"B Constructor!"«endl;JB(int i):(5)x2=i;cout«"B Constructor2"«cndl; void dispb() (6);cout«nx2=M«x2«endl;private:int 2;); int main()( B b(2); b.dispbO;).请在下面程序的横线处熄上适当字句,以便程序完整,并使程序的输出为 构造base对象 调用 base:f()构造derive对象 调用 derive: :f()#include <iostream.h>class basepublic:base()a.push_back(78);cout«”构造 base 对象”<<endl;a.push_back(92);f();a.push_back(52);count =(10);(7)cout<<“调用 base:f()M«endl;cout«Mcount=M«count«eiKlI;);)class dcrivc:public base(三、程序阅读题(每题6分,总计30分)public:1.#includc <iostrcam.h>derive()class CSample cout«”构造 derive 对象”<<cndl;(8);private:int n;void f()coul«”调用 derive:f()"«endl;Jstatic int k;);public:int main()CSamplefint i)n=i;k+;void disp();derive d;);)void CSamplc:disp()5.下面程序用STL的条件计数算法和自定义的函数对象对一个存放在整数向量类对象中的学cout«"n="«n«",k="«k«cndl;生成绩进行统计及格人数并显示结果。请在下面程序的横线处填上适当字句,以使程序完整。int CSample: :k=0;#include<iostream>int niain()#include<vector> CSample a(10),b(20),c(30);#include<agorithm>a.dispO;#include<functional>b.dispO;using namespace std;c.dispO;class Pass f)Ipublic:2.#include <iostream.h>(9)class Sample( int n;return x>=60;public:Sample。;Samp!e(int m)n=m;Sample operator-(int)int main() Sample old=*this;n-;vector<int> a;return old;int count;void disp()(cout«"n="«n«endl;); int main() Sample s(10); s-; s.dispO;3. #include <iostream.h>#includc <string.h> template <class T> T max(T x, T y) return (x > y ? x : y);) char *max(char *x, char *y) if (strcnip(x, y) >= 0) return x;else return y; int main() cout « max(2, 5) « cndl; cout « max(3.14,4.12) « endl;cout« max("China", Beijing1') « endl; )4. #includc <stack>#include <iostream> #include <string> using namespace std; int main() stack<char> s; string str;cln » str;从键盘输入字符串IiveChinafor (string:iterator iter = str.begin(); iter != str.endO; +iter) s.push(*iter);while (!s.empty() cout« s.topO;s.pop();cout« endl;)5. #includc <iostrcam>using namespace std;int divide(int x, int y) if(y = 0)throw x;return x / y;int main() try cout « M8/3 = " « divide(8.3) « endl; cout « M6/0 = " « divide(6,0) « endl; cout « "9/2 = " « dividc(9, 2) « cndl: catch (int c) cout « e « ° is divided by zero!" « cndl;)cout« "That is ok." « endl;四、编程题(每题15分,总计30分)1 .编程实现小型公司的工资管理。该公司主要有4类人员:经理(manager)、兼职技术人员 (technician).销售员(salesman)和销售经理(salesmanager),要求存储这些人员的编号、 姓名和月工资,计算月工资并显示全部信息。月工资计算方法是:经理拿固定月薪8000元, 兼职技术人员按每小时1。0元领取月薪,销售员按当月销售额的4%提成,销售经理既拿固定 月工资也领取销售提成,固定月工资为5000元,销售提成为所管辖部门当月销售总颖的千分 之五。兼职技术人员一个月工作小时数、销售员一个月销售额、销售经理所管辖部门一个月销 售总额由各个类的成员函数完成设置。(要求用抽象类和类继承)2 .定义一个矩阵类(Matrix),设计相关的构造函数、析构函数等,采用运算符重载方式实现 矩阵的加、减运算。编写main函数,对以上所有功能进行测试。注意:按照实际情况大小使 用内存空间,矩阵的加、减运算不要造成内存浪费。