收藏 分销(赏)

2016-2017年度面向对象程序设计试卷.doc

上传人:a199****6536 文档编号:1655463 上传时间:2024-05-07 格式:DOC 页数:13 大小:87.50KB 下载积分:8 金币
下载 相关 举报
2016-2017年度面向对象程序设计试卷.doc_第1页
第1页 / 共13页
2016-2017年度面向对象程序设计试卷.doc_第2页
第2页 / 共13页


点击查看更多>>
资源描述
华中科技大学研究生课程考试试卷 面向对象程序设计 √ √ □开卷 □闭卷 □公共课 □专业课 课程名称: 课程类别 考核形式 2017年1月9日 学生类别______________考试日期______________ 院系_______________ 杨卫东、左峥嵘 学号__________________姓名__________________任课教师___________________ 一、填空(共15分,每空1分) 1、编译时的多态性通过__重载__函数实现。 2、面向对象的四个基本特性是 抽象 、 多态 、 继承 和 封装 。 3、假定AB为一个类,则执行“AB a(10), *b=&a, &p=a;”语句时调用该类构造函数的次数为 2 。 4、C++类成员访问属性 public 、 private 、 protected 三种类型。 5、非成员函数应声明为类的__友元___函数才能访问这个类的private成员。 6、。要实现动态联编必须通过对象指针或引用来调用_虚函数_实现。 7、类B是由类A以保护方式派生的,则类A中私有访问属性的数据成员在类B中的访问属性为 不可访问 。 8、+、=、[]、->等四种运算符中,可采用友元函数重载的运算符是 + 。 9、抽象类是指具有 纯虚函数 的类,它只能作为 基类 来使用。 二、问答题(共30分,每小题6分) 1.简述运算符重载的实现方式有哪几种?给出对应的声明语法形式。 答案: (1)类外定义的运算符重载函数 格式为: friend <返回类型> operator <op>(<类型> 参数1,<类型> 参数2) { <函数体> } (2)成员运算符重载函数 <返回类型> <类名>::operator <op> (<类型> 参数) { <函数体> } 2.什么是多继承?多继承时,构造函数和析构函数执行顺序是怎样的? 答案: 多继承是指派生类具有多个基类,派生类与每个基类之间的关系仍可看作是一个单继承。 派生类构造函数的执行顺序是先执行所有基类的构造函数(顺序按照定义派生类时指定的各基类顺序),再执行派生类的构造函数,析构函数执行顺序,与构造函数完全相反。 3.写出下面程序的运行结果 #include <iostream> #include <string> using namespace std; class Person { public: Person(char *nam,int ag) { strcpy(name,nam); age = ag; cout<<"Person类构造函数---"<<name<<endl; } Person(char *nam) { strcpy(name,nam); cout<<"Person类构造函数(char *nam)---"<<name<<endl; } ~Person() { cout<<"Person类析构函数---"<<name<<endl; } void display() { cout<<"姓名:"<<name<<endl; } protected: char name[100]; int age; }; class Teacher: public Person // 声明Teacher(教师)类 { public: // 公用部分 Teacher(char *nam,int a,char *t):Person(nam,a) // 构造函数 { strcpy(title,t); cout<<"Teacher类构造函数"<<endl; } ~Teacher() { cout<<"Teacher类析构函数"<<endl; } void display1() // 输出教师有关数据 { cout<<"姓名:"<<name<<endl; cout<<"年龄"<<age<<endl; cout<<"职称:"<<title<<endl; } protected: // 保护部分 char title[10]; // 职称 }; class Student: public Person { public: Student(char *nam,char s,float sco):Person(nam) { sex=s; score=sco; cout<<"Student类构造函数---"<<name<<endl; } Student(char *nam, char s):Person(nam) { sex=s; cout<<"Student类构造函数---班长:"<<name<<endl; } ~Student() { cout<<"Student类析构函数---"<<name<<endl; } void display2() // 输出学生有关数据 { cout<<"姓名:"<<name<<endl; cout<<"性别:"<<sex<<endl; cout<<"成绩:"<<score<<endl; } char *get_name() { return name; } protected: // 保护部分 char sex; float score; // 成绩 }; class Graduate: public Teacher, public Student { public: Graduate(char *nam,int a,char s,char *t,float sco,float w):Teacher(nam,a,t),Student(nam,s,sco),wage(w),monitor("Li Ming",'m') { cout<<"Graduate类构造函数"<<endl; } ~Graduate() { cout<<"Graduate类析构函数"<<endl; } void show( ) // 输出人员的有关数据 { cout<<"name:"<<Student::name<<endl; cout<<"age:"<<Student::age<<endl; cout<<"sex:"<<sex<<endl; cout<<"score:"<<score<<endl; cout<<"title:"<<title<<endl; cout<<"wages:"<<wage<<endl; cout<<"monitor:"<<monitor.get_name()<<endl; } Student monitor; private: float wage; // 工资 }; class Doctor:public Graduate { public: Doctor(char *nam,int a,char s,char *t,float sco,float w,char *tutor): Graduate(nam,a,s,t,sco,w) { strcpy(tutor_name, tutor); cout<<tutor_name<<endl; } private: char tutor_name[20]; }; int main() { Doctor d("Wang-li",24,'f',"assistant",89.5,1234.5,"Wang-wu"); return 0; } 答案: Person类构造函数---Wang-li Teacher类构造函数 Person类构造函数(char *nam)---Wang-li Student类构造函数---Wang-li Person类构造函数(char *nam)---Li Ming Student类构造函数---班长:Li Ming Graduate类构造函数 Wang-wu Graduate类析构函数 Student类析构函数---Li Ming Person类析构函数---Li Ming Student类析构函数---Wang-li Person类析构函数---Wang-li Teacher类析构函数 Person类析构函数---Wang-li 4.写出下面程序的运行结果 #include <iostream> #include <string.h> using namespace std; class A { char string[80]; public : void show(); A(char * st); ~A( ); }; A::A(char * st) { strcpy(string, st); cout << string << "[构造]" << endl; } A::~A( ) { cout << string << "[析构]" << endl; } void A::show() { cout << string << endl; } void fun( ) { cout << "1 fun内" << endl; A fun_Obj("2 fun内自动对象fun_Obj"); static A fun_sta_Obj("3 fun内静态对象fun_sta_Obj"); } void main( ) { A *ptrA = new A("4 main内动态分配对象m_all_Obj"); if(ptrA==NULL) return; ptrA->show(); cout<<"5 main内调用fun函数"<< endl; fun( ); delete ptrA; } A g_glb_Obj("6 外部对象g_glb_Obj"); static A g_sta_Obj("7 外部静态对象g_sta_Obj"); 答案:6 外部对象g_glb_Obj[构造] 7 外部静态对象g_sta_Obj[构造] 4 main内动态分配对象m_all_Obj[构造] 4 main内动态分配对象m_all_Obj 5 main内调用fun函数 1 fun内 2 fun内自动对象fun_Obj[构造] 3 fun内静态对象fun_sta_Obj[构造] 2 fun内自动对象fun_Obj[析构] 4 main内动态分配对象m_all_Obj[析构] 3 fun内静态对象fun_sta_Obj[析构] 5.写出下面程序的输出结果 #include <iostream> using namespace std; class A{ public: virtual void Print(int a, int b=5) { cout << "a = " << a << " , b = " << b << endl; } }; class B : public A { public: virtual void Print(int a) { cout << "a = " << a << endl; } virtual void Print(int a, double d) { cout << "a = " << a << " , d = " << d << endl; } }; void Show(A * p) { p -> Print( 2, 6.9 ); p -> Print( 2 ); } void main( ) { A * pa = new A; B * pb = new B; Show(pa); Show(pb); delete pa; delete pb; } 答案:a = 2 , b = 6 a = 2 , b = 5 a = 2 , b = 6 a = 2 , b = 5 6. 以下程序代码是否有错,如有请改正并写出运行结果 #include <iostream.h> class Test { private: int x,y=20; public: Test(int i,int j) {x=i,y=j;} int getx(){return x;} int gety(){return y;} }; void main() { Test mt(10,20); cout<<mt.getx()<<endl; cout<<mt.gety()<<endl; } 答案: int x,y=20;在类内部不能对数据成员直接赋值。 [修改]int x,y; 三、完成程序题(共20分,每小题5分) 1. 完成下面类中成员函数的定义。 ________________________ #include <iostream> using namespace std; class Counter { unsigned value; ______________ public : Counter( ) { value = 0; } ___________ friend Counter _____________________operator ++(Counter &a); //前置增量运算符函数。 void Print( ); }; Counter _________________________operator ++(Counter &a ) { a.value++; //把Counter类对象的value值增1 cout << " 调用前置增量运算符函数 !\n"; return a; //该函数的返回值是value值增1后的对象 } void Counter::Print( ) { static int i = 0; ++i; cout << "(" << i << ") 对象的value值 = " << value << endl; } void main( ) { Counter c ; for(int i = 0; i < 3; i++) ++c; c.Print( ); } 2. 完成下面类中成员函数的定义。 #include <iostream> #include <string> using namespace std; _____________________ class str { private: char *st; public: _________________ {set(a); } str(char *a) str & operator=(_________) str &a { delete st; set(a.st); return *this; } void set(______________) //初始化st char *s { ____________________ st=new char[strlen(s)+1]; strcpy(st,s); } }; 3. 在下面程序横线处填上适当内容,使程序执行结果为: S=2 S=5 S=9 #include <iostream.h> ___________________ int sum(int i) { static int s; __________________ s=s+i+2; __________________ return s; } void main (void) { int S; for (i=0;____________________) i<3,i++ { S = sum(i); ____________________ cout<<"S="<<S<<" "; } cout<<endl; } 4. 下面是根据边长计算三角形面积的C++程序,在下划线处填上正确的语句。 #include <iostream.h> #include <math.h> _____________________________ double area(double a, double b, double c); void main() { double a,b,c,s; cout<<"Input a b c:"; ______________________ cin>>a>>b>>c; _____________________ //判断是否三角形,若是,则执行面积计算代码if(a+b>c&&a+c>b&&c+b>a) { ______________________ s=area(a,b,c); cout<<"The area is:"<<s<<endl; } else cout<<"Error"<<endl; } double area(double a, double b, double c) { double s; s = (a+b+c)/2.0; s = sqrt(s*(s-a)*(s-b)*(s-c)); _____________________ return s; } 四、写程序题(共35分) 1、建立一个字符串类用于存储和处理字符串,采用友元函数对操作符‘+’进行重载,以实现两个字符串的合并功能。(15分) 2、写一个具有动态多态性的学生和教师信息输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名、输入和显示设计成一个抽象类person的成员,并将person类作为学生数据操作类student和教师操作类teacher的基类。程序应能根据待输入人员的类别完成对应人员信息的输入和显示功能。(20分) 13 / 8
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服