1、郑州大学现代远程教育《 面向对象程序设计》课程考核要求
说明:本课程考核形式为提交作业,完成后请保存为WORD 2003版本格式的文档,登陆学习平台提交,并检查和确认提交成功(能够下载,并且内容无误即为提交成功)。
一. 作业要求
1.请独立自主完成作业内容。
二. 作业内容
一)、简答题:(每题 5 分,共30 分)
1.什么是抽象类?它有什么特点?
答:
抽象类与接口紧密相关,它们不能示例化,并且常常部分实现或根本不实现。
特点:
1、抽象类不能直接实例化
2、允许(但不要求)抽象类包含抽象成员。
3、抽象类不能被密封
2. C++中使用多态的前提条件
2、是什么?
答:动态多态的前提条件:
1.通过调用虚函数实现多态
2.通过基类的指针或引用调用虚函数
3. 什么是类?类与结构有和不同?
答:类是引用类型在堆上分配,类的实例进行赋值只是复制了引用,都指向同一段实际对象分配的内存
不同:类有构造和析构函数,类可以继承和被继承
结构没有构造函数,但可以添加。结构没有析构函数,结构不可以继承自另一个结构或被继承,但和类一样可以继承自接口。
4. 面向对象的特征有哪些?
答:封装,继承,多态
5. 简述this指针的含义是什么?
答:this
3、指针是一个隐含于每一个成员函数中的特殊指针。它是一个指向正在被该成员函数操作的对象,也就是要操作该成员函数的对象。
6. 友元关系具有什么特点?
答:友元函数的特点是能够访问类中的私有成员的非成员函数。友元函数从语法上看,它与普通函数一样,即在定义上和调用上与普通函数一样。
二)、分析下面的代码,回答问题(每题 10 分,共 40 分)
1. 分析下面的代码,指出其中的错误,说明理由并改正
class C {
public:
int fn1( int x ) {
dm = x ;
}
private:
int dm;
};
void
4、 main ( ) {
C c;
c.fn1(16 );
cout << c.dm << ‘\n’;
}
答://dm为私有变量,是不能通过对象直接访问的,正确的做法是添加一个public类型的方法,如public void Show(){cout<5、nt m;
public:
B(int i,int j):a(i){m=j;}
~B(){cout<<"m="<6、{cout<<"n="<7、
~A(){};
};
class B:virtual public A
{
public:
B(char *s1,char*s2):A(s1)
{
cout<8、A(s1)
{
cout<9、x{
public:
complex(int r=0,int i=0);//构造
complex operator+(const complex& other);
complex Complex::operator+(const int &a);
void complex::input()
protected:
int real,image;
};
void complex::input()
{
int real,image;
cout << "please enter a complex: " << endl;
cin>>r>>m;
real =
10、 r;
image = m;
}
complex::complex(int r,int i)
{
real=r;
image=i;
return;
}
complex complex::operator+(const complex& other)
{
complex temp;
temp.real=real+other.real;
temp.image=image+other.image;
return temp;
}
complex complex::operator+(const int &a)
{
complex
11、 temp;
temp.real= real +a;
temp.image = image;
return temp;
}
int main(int argc,char* argv[])
{
complex a,b,c;
a.input();
b.input();
c = a +b;
//输出
c = 5 +a;
//输出
c = b +5;
//输出
}
}
2. 设计High类,其数据成员为高h,定义虚函数disp()。由High派生出长方体类Cuboid与圆柱体类Cylinder。在主函数中用基类指针变量p调用虚函数disp()显示长方体与圆柱体的体
12、积。(15分)
答:class High{
public:
unsigned int h;
virtual void disp(){};
};
class Cuboid:public High{
Public:
unsigned int length;
unsigned int width;
virtual void disp(){cout<disp();
objB.r=5;
objB.h=10;
p=&objB;
p->disp();
}