1、华中科技大学研究生课程考试试卷 面向对象程序设计 √ √ □开卷 □闭卷 □公共课 □专业课 课程名称: 课程类别 考核形式 2017年1月9日 学生类别______________考试日期______________ 院系_______________ 杨卫东、左峥嵘 学号__________________姓名__________________任课教师___________________ 一、填空(共15分,每空1分) 1、编译时的多态性通过__重载__函数实现。 2、面向对象的四个基
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、+、=、[]、->等四种运算符中,可采用友元函数重载
3、的运算符是 + 。
9、抽象类是指具有 纯虚函数 的类,它只能作为 基类 来使用。
二、问答题(共30分,每小题6分)
1.简述运算符重载的实现方式有哪几种?给出对应的声明语法形式。
答案:
(1)类外定义的运算符重载函数
格式为:
friend <返回类型> operator
4、 }
2.什么是多继承?多继承时,构造函数和析构函数执行顺序是怎样的?
答案:
多继承是指派生类具有多个基类,派生类与每个基类之间的关系仍可看作是一个单继承。
派生类构造函数的执行顺序是先执行所有基类的构造函数(顺序按照定义派生类时指定的各基类顺序),再执行派生类的构造函数,析构函数执行顺序,与构造函数完全相反。
3.写出下面程序的运行结果
#include
5、r *nam,int ag)
{ strcpy(name,nam); age = ag;
cout<<"Person类构造函数---"< 6、 void display()
{
cout<<"姓名:"< 7、 { strcpy(title,t);
cout<<"Teacher类构造函数"< 8、}
protected: // 保护部分
char title[10]; // 职称
};
class Student: public Person
{
public:
Student(char *nam,char s,float sco):Person(nam)
{ sex=s; score=sco;
cout<<"Student类构造函数---"< 9、
Student(char *nam, char s):Person(nam)
{ sex=s;
cout<<"Student类构造函数---班长:"< 10、ame< 11、tudent
{
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类构造函数"< 12、 // 输出人员的有关数据
{
cout<<"name:"< 13、name()< 14、utor);
cout< 15、类构造函数(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 16、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( )
{
co 17、ut << "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( );
delet 18、e 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 19、main内动态分配对象m_all_Obj[析构]
3 fun内静态对象fun_sta_Obj[析构]
5.写出下面程序的输出结果
#include 20、 "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);
dele 21、te pa;
delete pb;
}
答案:a = 2 , b = 6
a = 2 , b = 5
a = 2 , b = 6
a = 2 , b = 5
6. 以下程序代码是否有错,如有请改正并写出运行结果
#include 22、turn y;}
};
void main()
{
Test mt(10,20);
cout< 23、 unsigned value;
______________ public :
Counter( ) { value = 0; }
___________ friend Counter _____________________operator ++(Counter &a); //前置增量运算符函数。
void Print( );
};
Counter _________________________operator ++(Counter &a )
{
a.value++; 24、 //把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 25、) ++c;
c.Print( );
}
2. 完成下面类中成员函数的定义。
#include 26、te 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 27、atic int s;
__________________ s=s+i+2;
__________________ return s;
}
void main (void)
{
int S;
for (i=0;____________________) i<3,i++
{
S = sum(i);
____________________ cout<<"S="< 28、处填上正确的语句。
#include 29、b>c&&a+c>b&&c+b>a)
{
______________________ s=area(a,b,c);
cout<<"The area is:"< 30、 return s;
}
四、写程序题(共35分)
1、建立一个字符串类用于存储和处理字符串,采用友元函数对操作符‘+’进行重载,以实现两个字符串的合并功能。(15分)
2、写一个具有动态多态性的学生和教师信息输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名、输入和显示设计成一个抽象类person的成员,并将person类作为学生数据操作类student和教师操作类teacher的基类。程序应能根据待输入人员的类别完成对应人员信息的输入和显示功能。(20分)
13 / 8






