C++程序设计试题及答案(二)
四、编程(根据要求填上程序缺少的部分)
1.完成如下的程序,使得输出为:
1234
#include
#include
class A
{
private:
________(1)___________
protected:
________(2)__________
public:
A(int a, int b, int c)
{
X=a;
Y=b;
Z=c;
}
int GetX()
{
return X;
}
int GetY()
{
return Y;
}
int GetZ()
{
return Z;
}
};
class B _____(3)_______
{
private:
int K;
public:
B(int a, int b, int c, int d)_______(4)_________
{
K=d;
}
void Show()
{
cout << GetX() << GetY() << Z << K << endl;
}
};
void main()
{
B b(1,2,3,4);
b.Show();
}
2.在主函数中定义有30个元素的数组s,函数func1()的功能是将2、4、6、…、56、58、60分别赋给数组元素s[0]、s[1]、s[2]、…、s[27] 、s[28] 、s[29]。函数func2()的功能是按顺序将数组的每5个元素求平均值并存入数组w中(即将s[0]、s[1]、s[2] 、s[3]、s[4]的平均值存入w[0]中,s[5]、s[6]、s[7] 、s[8]、s[9]的平均值存入w[1]中,…)。请填空使程序正确运行。
#include
#include
void func1(double s[])
{
int i,k;
for(k=2,i=0; i< 30; i++)
{
s[i]=k;
______(5)_________
}
}
void func2(double s[], double w[])
{
double sum;
int i,k;
for(k=0,i=0,sum=0; i<30; i++)
{
_________(6)___________
if( (i+1)%5==0)
{
w[k] = sum/5;
_________(7)__________
k++;
}
}
}
void main()
{
double s[30], w[6];
func1(s);
func2(s,w);
}
C++程序设计试题答案(二)
一、单项选择题
1.C
2. D
3. A
4. A
5. B
6. D
7. D
8.A
9. c
10. b
二、填空题
1. 执行, 成员
2. 类中, 私有
3. class, struct
4. 2
5. 2, 6
6. 数据成员, 函数成员
7. public, private, protect, 私有, 公共
8. .cpp .h
9. 0.5
三、写出程序的运行结果
1.矩形左上角坐标为50,50
矩形长和宽分别为100,100
矩形的颜色是Blue
2.x= 0,y=0
x=2,y=3
x=y
x!=y
3,n=3
n=4
4, 5
四、编程(根据要求填上程序缺少的部分)
1. int X,Y;
2. int Z;
3.public A
4.: A(a,b,c)
5.k+=2;
6.sum += s[i];
7.sum = 0;
【 C++程序设计试题及答案(二)】相关文章: