文档介绍:C++语言程序设计
期末考试试题及答案
姓名____________ 学号____________ 班号___________
题号
一
二(1)
二(2)
三
总分
成绩
一、填空
,成员函数的实现部分可以写在类外。
,改变主调函数中实参变量的值,则函数的形参应该是引用类型或指针类型。
3. 抽象类只能作为基类使用,而不能声明它的对象。
,被重载的同名函数如果都没有用const修饰,则它们的形参个数或类型必须不同。
,不能调用其他成员函数。
。
。
二、阅读下列程序,写出其运行时的输出结果
如果程序运行时会出现错误,请简要描述错误原因。
,该题得分即为本小题得分。如两题都答,则取两题得分之平均值为本小题得分。
(1)程序:
#include <>
#include <>
class Base
{ private:
char msg[30];
protected:
int n;
public:
Base(char s[],int m=0):n(m)
{ strcpy(msg,s);
}
void output(void)
{ cout<<n<<endl<<msg<<endl;
}
};
class Derived1:public Base
{
private:
int n;
public:
Derived1(int m=1):
Base("Base",m-1)
{ n=m; }
void output(void)
{ cout<<n<<endl;
Base::output();
}
};
class Derived2:public Derived1
{
private:
int n;
public:
Derived2(int m=2):
Derived1(m-1)
{ n=m; }
void output(void)
{ cout<<n<<endl;
Derived1::output();
}
};
int main()
{
Base B("Base Class",1);
Derived2 D;
();
();
}
运行结果:
1
Base Class
2
1
0
Base
(2)程序:
#include <>
class Samp
{public:
void Setij(int a,int b){i=a,j=b;}
~Samp()
{ cout<<"Destroying.."<<i<<endl;
}
int GetMuti(){return i*j;}
protected:
int i;
int j;
};
int main()
{
Samp *p;
p=new Sa