问答题

请使用VC6或使用[答题]菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋值给myArray。类intArray重载了“=”运算符。程序中给出了一个测试数据文件input,不超过300个的整数。程序的输出是:
10
11
13
16
20
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”之间。实现重载赋值运算符函数,并将赋值结果在屏幕输出。格式不限。不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为obj文件,并且在本程序中调用。
//intArray.h
class intArray

private:
int*array;
int length;
public:
intArray (char*filename);
intArray ();
intArray & operator=(const intArray & src);
~intArray ();
void show ();
;
void writeToFile (const char*path);
//main.cpp
#include<iostream>
#include<fstream>
#include<cstring>
#include "intArray.h"
using namespace std;
intArray::intArray ()

length=10;
array=new int[length];
intArray::intArray(char*filename)

ifstream myFile(filename);
array=new int[300];
length=0;
while (myFile>>array[length++])
length--;
myFile.close();

intArray& intArray::operator=(const intArray & src)

if (arrayl=NUll) delete [] array;
length=src.length;
array=new int[length];
return*this;
intArray::~intArray ()

delete [] array;

void intArray::show ()

int step =0;
for(int i=0;i<length;i=i+step)

cout<<array[i]<<endl;
step++;


void main ()

intArray*arrayP=new intArray("input.dat");
intArray myArray;
myArray=*arrayP;
(*arrayP).show();
delete arrayP;
writeToFile(" ");

【参考答案】

for(int i=0;i<length;i++)
//遍历对象src中的数组array,然后依次把值放进数组......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,该工程中含有一个源程序文件proj2.cpp,其中定义了CharShape类、Triangle类和Rectangle类。 CharShape是一个抽象基类,它表示由字符组成的图形(简称字符图形),纯虚函数Show用于显示不同字符图形的相同操作接口。Triangle和Rectangle是CharShape的派生类,它们分别用于表示字符三角形和字符矩形,并且都定义了成员函数Show,用于实现各自的显示操作。程序的正确输出结果应为: * *** ***** ******* ######## ######## ######## 请阅读程序,分析输出结果,然后根据以下要求在横线处填写适当的代码并删除横线。 (1)将Triangle类的成员函数Show补充完整,使字符三角形的显示符合输出结果。 (2)将Rectangle类的成员函数Show补充完整,使字符矩形的显示符合输出结果。 (3)为类外函数fun添加合适的形参。 注意:只在指定位置编写适当代码,不要改动程序中的其他内容,也不要删除或移动“ ****found****”。 proj2.cpp #include<iostream> using namespace std; class CharShape public: CharShape (char ch):ch (ch)); virtual void Show()=0; protected: char ch; 组成图形的字符; class Triangle:public CharShape public: Triangle(char ch,int r):Char-Shape(ch),_rows (r) void Show(); private: int rows; 行数 ; class Rectangle: public CharShape public: Rectangle (char ch,int r,int c): CharShape (ch),_rows(r),_cols(c) void Show(); private: int rows,cols; 行数和列数 ); void Triangle::Show() 输出字符组成的三角形 for (int i=1;i<=rows;i++) ********found******** for (int j=1;j<=______;j++) cout<<_ch; cout<<endl; void Rectangle::Show() 输出字符组成的矩形 ********found******** for (int i=1;i<=______;i++) ********found******** for(int j=1;j<=______;j++) cout<<_ch; cout<<endl; ********found********为fun函数添加形参 void fun(______)cs.Show();int main() Triangle tri(’*’,4); Rectangle rect(’#’,3,8); fun (tri); fun (rect); return 0;