首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj3,此工程中包含一个源程序文件proj3.epp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋值给myArray。类intAr
请打开考生文件夹下的解决方案文件proj3,此工程中包含一个源程序文件proj3.epp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋值给myArray。类intAr
admin
2019-04-24
37
问题
请打开考生文件夹下的解决方案文件proj3,此工程中包含一个源程序文件proj3.epp,其功能是从文本文件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
#include
#include
#include "intArray.h"
using namespace std;
intArray::ihtArray()
{
lencsth=10:
array=new int[lenc3th];
intArray:: intArray (char*filename)
{
ifstream myFile(filename);
array=new int[300];
length=0;
while(myFile>>array[lenc3th++])
length--;
myFile.close();
}
intArray& intArray::operator
=(const intArray&src)
{
if(array!=NULL)delete[]
array;
length=src.length;
array=new int[length];
//*************333*************
//*************666*************
return*this;
}
intArray::~intArray()
{
delete[]array;
}
void intArray::show()
{
int step=0;
for(int i=0 ; i
{
cout<
<
step++;
}
}
void main ()
{
intArray*arrayP=new in
tArray("input.dat");
int;Array myArray;
myArray=*arrayP;
(*arrayP).show();
delete arcayP:
writeToFile("");
}
选项
答案
l for(int i=0;i
解析
主要考查考生对运算符重载的掌握,该函数要重载运算符“:”,该运算符的意思是赋值。先看该函数的其他语句:
1 if(array!=NULL)delete[]array;
2 length=src.length;
3 array=new int[length];
第一条语句是把原来动态数组释放,第二条语句是把形参src的成员length赋值给变量lengh,第三条语句是给指针array分配内存。接下来要把动态数组中的值逐个赋给array数组,在这里使用for循环语句,循环变量i的范围是0~length,并进行赋值操作。
转载请注明原文地址:https://kaotiyun.com/show/ROAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列程序的输出结果是()。#include<iostream>usingnamespacestd;template<typenameT>Tfun(Ta,Tb){return(a<=b)?a:b;)
下面关于构造函数和析构函数的描述中,错误的是()
有以下程序,输出结果()。#include<iostream>usingnamespacestd;classComplex{public:Complex(doubler=0,doublei=
下列叙述中错误的是()。
表达式x.operator+(y.operator++(0))还可以写成______。
执行以下程序后,输出“*”号的个数是______。#include<iostream.h>main(){inti,j;for(i=1;i<5;i++)for(j=2;j<=i;j++)cout<<’
有如下类声明:classFoo{intbar;};则Foo类的成员bar是()
不能作为函数重载的判断依据的是()。
插入排序算法的主要思想是:每次从未排序序列中取出一个数据,插入已排序序列中的正确位置。Insert类的成员函数sort()实现了插入排序算法,请填空。classInsert{public:Insert(int*b0,intn0)
有如下定义:classMA{intvalue;public:MA(intn=0):value(n){}};MA*ta,tb;其中MA类的对象名标识符是_____
随机试题
外伤后胸壁软化是由于()
下列哪一项不符合毛细血管瘤的特征
理论换算法的换算依据是( )。
商业秘密:指不为公众所知悉、能为权利人(商业秘密的所有人以及所有人许可的使用人)带来经济利益、具有实用性并经权利人采取保密措施的技术信息和经营信息。下列不涉及商业秘密的一项是()。
1,7,8,57,()。
________从“美是生活”的论断出发,认为艺术是对生活的“再现”。
()seektoperformrootcauseinvestigationastowhatisIeadingidentifiedtrends.
下列关于栈的叙述中正确的是( )。
Whatarethespeakersmainlydiscussing?Listenagaintopartoftheconversation.Thenanswerthefollowingquestion.Whydoes
PASSAGETHREEWhatfinallyledtothetragedyoftheyoungman?
最新回复
(
0
)