打开解决方案文件proj3,此工程文件包含源程序文件main.cpp,其中有类Array(“数组”类)、IntArray(“整型数组”类)和CharArray(“字符数组”类)的定义,以及主函数main的定义。在主函数中定义了2个对象,实现了整型数组和字符数组的输入与输出。请在程序中“//**********found**********”下的横线处填写适当的代码,然后删除横线,以实现上述类定义。
此程序的正确输出结果应为(下划线部分为键盘输入内容):
设置整型数组大小: 8
输入整型数组: 1 2 3 4 5 6 7 8
整型数组为:1,2,3,4,5,6,7,8
设置字符数组大小: 16
输入字符数组: I am a student.
字符数组为:I am a student.
[程序]
//main.cpp
#include<iostream>
#include<cstring>
using namespace std;
class Array{ // 数组类
public:
Array(int n):length(n){ }
//**********found**********
___________________________ ; // 输入数组
virtual void output()const=0; // 输出数组
protected:
int length; // 数组大小(元素个数)
};
class IntArray:public Array{ // 整型数组类
public:
IntArray(int n):Array(n)
{
arr=new int[length];
}
//**********found**********
___________________________
void input()
{
int i;
cout<<输入整型数组:;
for(i=0;i<length;i++)
cin>>arr[i];
}
void output()const
{
int i;
cout<<整型数组为:;
for(i=0;i<length;i++)
if(i==length-1)cout<<arr[i]<<endl;
//**********found**********
else ___________________________;
}
private:
int *arr;
};
class CharArray:public Array{ // 字符数组类
public:
//**********found**********
CharArray(int n): _______________
{
arr=new char[length];
}
~CharArray(){ delete []arr; }
void input()
{
char c;
cin.get(c); // 清除缓存中的换行符
cout<<输入字符数组:;
cin.getline(arr,length);
}
void output()const
{
cout<<字符数组为:;
cout<<arr<<endl;
}
private:
char *arr;
};
void fun(Array *p)
{
p->input();
p->output();
}
void fun(Array *p)
{
p->input();
p->output();
}
int main(void )
{
int n;
cout<<设置整型数组大小:;
cin>>n;
IntArray test1(n);
fun(&test1);
cout<<设置字符数组大小:;
cin>>n;
CharArray test2(n);
fun(&test2);
system(pause);
return 0;
}
解题说明:
1、将下列压缩文件“proj3.zip”下载到计算机中,解压后用VC2010对程序进行调试运行,即找到并双击文件“proj3.sln”打开解决方案,然后操作。
(1)源程序文件在解决方案管理器的Source Files文件夹中,头文件在Header Files文件夹中。
(2)为了避免运行结果闪退,方便调试,可在主函数“return 0;”语句前添加语句“system(“pause”);”,调试结束后再删除添加的该语句。
2、操作完成后,将答案(横线处所填写的内容)依次填写到作业系统中。
(含图)<a href=/ueditorupload/read?objectId=ef2702c4669f707a3f2a9214f59e6887&fileOriName=proj3.zip target=_blank type=zip>proj3.zip</a>