资源描述:
《面向对象程序设计A卷-答案》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、中国传媒大学200?—201Q学年第二一学期期末考试试卷A答案考试科目:面向对象程序设计(C++)考试班级:09计科考试方式:闭卷命题教师:冯爽一、单项选择题(每题2分,共20分)12345678910ACBCCCBAAB二、问答题(共30分)'/0'2.(4分)运行结果:(1,2)(6,9)5,6(6,9)3.(4分)1)在类屮的声明形式:staticvoidfunction();1)在类外定义时的函数头部:voidDemo::function()2)调用function函数的两种形式:Demo::function或者Demod;d.function();2.(4分)答:虚函数就是在基类屮被
2、关键字virtual说明,并在派生类屮重新定义的函数。虚函数是函数重载的另一种形式,普通函数重载时,参数个数或类型必须有所不同,函数返回值类型可以不同。当重载一个虚函数时,要求函数原型必须完全相同。如果仅返回值类型不同,其余均相同,系统会给出错误信息;若仅函数名相同,而参数个数、类型或顺序不同,系统将它作为普通的函数重载,这时虚函数的特性将丢失。3.(4分)1)voidDate::operator+=(int);或者Date&Date::operator+=(int);2)triendvoidoperator+=(Date&,int);或者friendDate&operator+=(Date&
3、,int);4.(4分)答:拷贝构造函数是只有一个参数,且参数是木类对象的引用,在对象之间赋值时需要调用拷贝构造函数。转换构造函数是只有一个参数,且参数是基本类型数据。当把一个基本类型数据转换成类类型时,需要调用转换构造函数。5.(2分)templateTGradeBook::getGrade()6.(2分)答:首先寻找一个参数完全匹配的非模板函数,如果找到就调用它,如果没找到,则寻找函数模板,将其实例化,产生一个匹配的模板函数,若找到,就调用它。7.(2分)1)当对象参数为”LiPing”,32时程序输出:Ageistooold2)当对象参数为“LiXiaoPing“,
4、24时程序输岀:Nameistoolong三、程序填空题(每空2分,共30分)1.(18分)1)name(na),dept(de)1)virtual~Employee()2)voidEmployee::print()const3)publicEmployee4)Employee(na,de),level(le)5)Employee::print();6)newManager(”SallySmith",“Sales”,2);7)emp->print();8)deleteemp;1.(12分)1)#include2)ifstreamcalendarFile(Hcalendar.t
5、xt");3)icalendarFile4)calendarFile»day;5)!calendarFile.eof()6)x<3四、程序设计题(共20分)评分标准:类的数据成员设计5分,类的成员函数设计10分,主函数流程5分一种可行方案代码如下:#ifndefBOOK_H#defineBOOK_H#includeusingnamespacestd;classBook{public://Book();Book(int=O,string=,H,,string=',H,string=””,double=0);intgetBooklD(){returnbookID;}stringge
6、tBookName(){returnbookName;}stringgetBookAuthor(){returnbookAuthor;}stringgetBookPublish(){returnbookPublish;}doublegetBookPrice(){returnbookPrice;}voidbookDisp();private:intbookID;stringbookName;stringbookAuthor;stringbookPublish;doublebookPrice;};#endif#include"Book.h"#includeusingnamesp
7、acestd;Book::Book(intblD,std::stringbName,std::stringbAuthor,std::stringbPub,doublebPrice):booklD(blD),bookName(bName),bookAuthor(bAuthor),bookPublish(bPub),bookPrice(bPrice){}voidBook::bookDisp(){cou