面向对象程序设计 C++上机考试 试卷及答案 共2套_第1页
面向对象程序设计 C++上机考试 试卷及答案 共2套_第2页
面向对象程序设计 C++上机考试 试卷及答案 共2套_第3页
面向对象程序设计 C++上机考试 试卷及答案 共2套_第4页
面向对象程序设计 C++上机考试 试卷及答案 共2套_第5页
已阅读5页,还剩40页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PAGE1面向对象程序设计上机考试A班级:学号:姓名:时间120分钟说明:所有程序都来自教材中的例题,并对个别地方进行修改,务必仔细阅读程序。评分原则:除了特别说明外,每个题目如能得到正确结果,就得满分,否则得零分。要求:集成编码环境指定为VS2013,分析设计工具为POWERDESIGNER,要求将结果截屏粘贴在题目后面,并保存到一个PDF文件,其文件名格式为【学号_姓名.pdf】,最终提交这个文件。第一部分编码调试(每题20分,共60分)1、编辑运行程序编辑、编译、连接下面的程序,最后运行。答案:结果正确2结果正确20分2、调试程序调通下面的程序,只能完善Tdate类,不能修改main函数。#include<iostream>usingnamespacestd;classTdate{public: Tdate(inty){ init(y); } Tdate(inty,intm){ init(y,m); } Tdate(inty,intm,intd){ init(y,m,d); } voidprint(void){ cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl; } ~Tdate(){ cout<<"析构:"; print(); }private: voidinit(inty=2024,intm=1,intd=1){ year=y; month=m; day=d; cout<<"构造:"; print(); } intyear; intmonth; intday;};intmain(){ Tdatea; Tdateb(2023); Tdatec(2024,8); Tdated(2024,8,8);}答案:Tdate未定义无参构造函数,加上后编译通过。Tdate(){ init();}加上无参构造函数,且能运行加上无参构造函数,且能运行,20分结果正确20分3、编写代码根据下面的类图和时序图编写类代码和main函数代码,并调试通过。答案:类正确10分类正确10分,main()正确5分,有运行截图5分第二部分设计程序(每题20分,共40分)1、使用类图描述程序的静态结构1)阅读下面的程序,画出程序的类图,建立程序的静态模型。#include<iostream>usingnamespacestd;classTdate{public: Tdate(intyear,intmonth,intday){ this->year=year; this->month=month; this->day=day; cout<<"构造Tdate:"; print(); } Tdate(constTdate&oldTdate){ memcpy(this,&oldTdate,sizeof(Tdate)); cout<<"拷贝构造Tdate:"; print(); } ~Tdate(){ cout<<"析构Tdate:"; print(); } voidprint(){ cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl; }private: intyear; intmonth; intday;};classPerson{public: Person(stringname,constTdate&d){ this->name=name; birthday=newTdate(d); cout<<"构造Person:"; print(); } ~Person(){ cout<<"析构Person:"; print(); deletebirthday; } voidprint(){ cout<<name<<","; birthday->print(); }private: stringname; Tdate*birthday;};intmain(){ Tdated1(2001,6,1); Tdated2(2004,9,1); Personp1("张三",d1); Personp2("李四",d2);}类图各2分类图各2分,连接1分2)生成代码请将题中所有成员函数的实现代码复制到其静态模型,采用正向工程生成程序的源代码,并与原来的源代码进行比较。5分5分1.将构造函数Person(stringname,constTdate&d)的复制界面截图;4个文件各2分2分24个文件各2分2分3.贴出运行结果图。答案:1.2./************************************************************************Module:Tdate.h*Author:MrWong*Modified:2023年6月10日23:55:01*Purpose:DeclarationoftheclassTdate***********************************************************************/#if!defined(__上机_Tdate_h)#define__上机_Tdate_hclassTdate{public:Tdate(intyear,intmonth,intday);Tdate(constTdate&oldTdate);~Tdate();voidprint(void);protected:private:intyear;intmonth;intday;};#endif/************************************************************************Module:Person.h*Author:MrWong*Modified:2023年6月10日23:55:01*Purpose:DeclarationoftheclassPerson***********************************************************************/#if!defined(__上机_Person_h)#define__上机_Person_h单独添加单独添加#include<string>#include"Tdate.h"usingnamespacestd;classPerson{public:Person(stringname,constTdate&d);~Person();voidprint(void);protected:private:stringname;Tdate*birthday;};#endif/************************************************************************Module:Tdate.cpp*Author:MrWong*Modified:2023年6月10日23:55:01*Purpose:ImplementationoftheclassTdate***********************************************************************/单独添加单独添加#include"Tdate.h"#include<iostream>usingnamespacestd;//////////////////////////////////////////////////////////////////////////Name:Tdate::Tdate(intyear,intmonth,intday)//Purpose:ImplementationofTdate::Tdate()//Parameters://-year//-month//-day//Return:////////////////////////////////////////////////////////////////////////Tdate::Tdate(intyear,intmonth,intday){this->year=year; this->month=month; this->day=day; cout<<"构造Tdate:"; print();}//////////////////////////////////////////////////////////////////////////Name:Tdate::Tdate(constTdate&oldTdate)//Purpose:ImplementationofTdate::Tdate()//Parameters://-oldTdate//Return:////////////////////////////////////////////////////////////////////////Tdate::Tdate(constTdate&oldTdate){memcpy(this,&oldTdate,sizeof(Tdate)); cout<<"拷贝构造Tdate:"; print();}//////////////////////////////////////////////////////////////////////////Name:Tdate::~Tdate()//Purpose:ImplementationofTdate::~Tdate()//Return:////////////////////////////////////////////////////////////////////////Tdate::~Tdate(){cout<<"析构Tdate:"; print();}//////////////////////////////////////////////////////////////////////////Name:Tdate::print()//Purpose:ImplementationofTdate::print()//Return:void////////////////////////////////////////////////////////////////////////voidTdate::print(void){cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;}/************************************************************************Module:Person.cpp*Author:MrWong*Modified:2023年6月10日23:55:01*Purpose:ImplementationoftheclassPerson***********************************************************************/单独添加单独添加#include"Person.h"#include<iostream>usingnamespacestd;//////////////////////////////////////////////////////////////////////////Name:Person::Person(stringname,constTdate&d)//Purpose:ImplementationofPerson::Person()//Parameters://-name//-d//Return:////////////////////////////////////////////////////////////////////////Person::Person(stringname,constTdate&d){this->name=name; birthday=newTdate(d); cout<<"构造Person:"; print();}//////////////////////////////////////////////////////////////////////////Name:Person::~Person()//Purpose:ImplementationofPerson::~Person()//Return:////////////////////////////////////////////////////////////////////////Person::~Person(){cout<<"析构Person:"; print(); deletebirthday;}//////////////////////////////////////////////////////////////////////////Name:Person::print()//Purpose:ImplementationofPerson::print()//Return:void////////////////////////////////////////////////////////////////////////voidPerson::print(void){cout<<name<<","; birthday->print();} 3.2、使用时序图描述程序的执行过程阅读下面的程序,画出程序的时序图,并建立程序的动态模型。要求:时序图要同时截屏消息Base(1)属性的Detail页。classBase{public: Base(intbaseAttribute):baseAttribute(baseAttribute){}protected: intbaseAttribute;};classDerived:publicBase{public: Derived(intbaseAttribut,intderivedAttribute) :Base(baseAttribut),derivedAttribute(derivedAttribute){};protected: intderivedAttribute;};intmain(){ Derivedderived(1,2);}答案:时序图(绘制时序图之前,先建立Base和Derived的继承类图):5分5分derived和父类derived和父类对象前后位置正确2分;定义在main()之内1分derived分配内存1分,调用构造函数2分;derived对象描述1分;父类对象调用构造函数2分;父类对象对象描述1分;其余不计分。对象图:对象各2分,连接1分对象各2分,连接1分面向对象程序设计上机考试B卷班级:学号:姓名:时间120分钟说明:所有程序都来自教材中的例题,并对个别地方进行修改,务必仔细阅读程序。评分原则:除了特别说明外,每个题目如能得到正确结果,就得满分,否则得零分。要求:集成编码环境指定为VS2013,分析设计工具为POWERDESIGNER,要求将结果截屏粘贴在题目后面,并保存到一个PDF文件,其文件名格式为【学号_姓名.pdf】,最终提交这个文件。第一部分编码调试(每题20分,共60分)1、编辑运行程序编辑、编译、连接下面的程序,最后运行。答案:结果正确2结果正确20分2、调试程序修复下面程序的Bug。#include<iostream>usingnamespacestd;classPoint{public: Point(intx,inty){ cout<<"构造Point("<<x<<","<<y<<")"<<endl; x=x; y=y; } Point(constPoint&oldPoint){ cout<<"拷贝构造Point("<<oldPoint.x<<","<<oldPoint.y<<")"<<endl; memcpy(this,&oldPoint,sizeof(Point)); } ~Point(){ cout<<"析构Point("<<x<<","<<y<<")"<<endl; }private: intx; inty;};intmain(){ Pointp1(2,3); Pointp2(p1);}答案:构造函数Point(intx,inty)对成员变量的赋值方式不正确,改成this->x=x;this->y=y;答案不一,能正确赋值即可,20分答案不一,能正确赋值即可,20分结果正确20分3、编写代码根据下面的类图和时序图编写矩形类Rectangle代码和main函数代码,贴出代码截图和运行结果截图。答案:类正确10分类正确10分,main()正确5分,有运行截图5分第二部分设计程序(每题20分,共40分)1、使用类图描述程序的静态结构1)阅读下面的程序,画出程序的类图,建立程序的静态模型。#include<iostream>#include<string>usingnamespacestd;classDepartment{public: Department(stringname){ this->name=name; } voidprint(){ cout<<name<<endl; }private: stringname;};classEmployee{public: Employee(stringname){ this->name=name; } Employee(stringname,Department&department){ this->name=name; this->department=&department; } voidbelongTo(Department&department){ this->department=&department; } voidprint(){ cout<<"我是:"<<name<<",我所在的部门是:"; department->print(); }private: stringname; Department*department=NULL;};intmain(){ Departmentdepartment1("研发部"); Employeeemployee1("张三"); employee1.belongTo(department1); employee1.print(); Departmentdepartment2("财务部"); Employeeemployee2("李四",department2); employee2.print();}类图各2分类图各2分,连接1分2)生成代码请将题中所有成员函数的实现代码复制到其静态模型,采用正向工程生成程序的源代码,并与原来的源代码进行比较。5分5分1.将函数Employee(stringname,Department&department)的复制界面截图;2分4个文件各2分22分4个文件各2分3.贴出运行结果图。答案:1.2./************************************************************************Module:Department.h*Author:MrWong*Modified:2023年6月28日11:09:36*Purpose:DeclarationoftheclassDepartment***********************************************************************/#if!defined(__上机B_Department_h)#define__上机B_Department_h单独添加#include<string>单独添加classDepartment{public:Department(std::stringname);voidprint(void)const;protected:private:std::stringname;};#endif/************************************************************************Module:Employee.h*Author:MrWong*Modified:2023年6月28日11:09:36*Purpose:DeclarationoftheclassEmployee***********************************************************************/#if!defined(__上机B_Employee_h)#define__上机B_Employee_h单独添加单独添加#include<string>#include"Department.h"classEmployee{public:Employee(std::stringname);Employee(std::stringname,Department&department);~Employee();voidbelongTo(Department&department);voidprint(void);protected:private:std::stringname;Department*department;};#endif/************************************************************************Module:Department.cpp*Author:MrWong*Modified:2023年6月28日11:09:36*Purpose:ImplementationoftheclassDepartment***********************************************************************/单独添加单独添加#include"Department.h"#include<iostream>usingnamespacestd;//////////////////////////////////////////////////////////////////////////Name:Department::Department(std::stringname)//Purpose:ImplementationofDepartment::Department()//Parameters://-name//Return:////////////////////////////////////////////////////////////////////////Department::Department(std::stringname){ this->name=name;}//////////////////////////////////////////////////////////////////////////Name:Department::print()//Purpose:ImplementationofDepartment::print()//Return:void////////////////////////////////////////////////////////////////////////voidDepartment::print(void)const{ cout<<name<<endl;}/************************************************************************Module:Employee.cpp*Author:MrWong*Modified:2023年6月28日11:09:36*Purpose:ImplementationoftheclassEmployee***********************************************************************/单独添加单独添加#include"Employee.h"#include<iostream>usingnamespacestd;//////////////////////////////////////////////////////////////////////////Name:Employee::Employee(std::stringname)//Purpose:ImplementationofEmployee::Employee()//Parameters://-name//Return:////////////////////////////////////////////////////////////////////////Employee::Employee(std::stringname){ this->name=name;}//////////////////////////////////////////////////////////////////////////Name:Employee::Employee(std::stringname,Department&department)//Purpose:ImplementationofEmployee::Employee()//Parameters://-name//-department//Return:////////////////////////////////////////////////////////////////////////Employee::Employee(std::stringname,Department&department){ this->name=name; this->department=&department;}//////////////////////////////////////////////////////////////////////////Name:Employee::~Employee()//Purpose:Implementation

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论