首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋
请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋
admin
2021-09-05
74
问题
请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋值给my-Array。类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::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(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 intArray("input.dat");
intArray myArray;
myArray=*arrayp;
(*arrayp).show();
delete arrayp;
writeToFile("");
}
选项
答案
for(int i=0;i
解析
主要考查考生对运算符重载的掌握,该函数要重载运算符“=”,该运算符的意思是赋值。先看该函数的其他语句:
if(atray!=NULL)delete[]array;
length=src.length;
array=new int[length];
第一条语句是把原来动态数组释放,第二条语句是把形参src的成员length赋值给变量length,第三条语句是给指针array分配内存。接下来要把动态数组中的值逐个赋给array数组,在这里使用for循环语句,循环变量i的范围是0~length,并进行赋值操作。
转载请注明原文地址:https://www.kaotiyun.com/show/0Zfp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
有如下程序:#inc1ude<iostream>usingnamespacestd;voidfunction2(intn);voidfunctionl(intn){if(n<=0)return;
有如下程序:#include<iostream>#include<iomanip>usingnamespacestd;intmain(){ints[]={123,234};cout<<ri
下列关于虚基类的描述中,错误的是()。
有如下程序:#inc1ude<iostream>usingnamespacestd;classMyclass{public:Myclass(intx):val(x){}voidSet(i
在软件设计中不使用的工具是( )。
有如下程序段:inti=0,j=1;int&r=i;//①r=j;//②int*p=&i://*p=&r;//④其中会产生编译错误的语句是()。
下列符号中,正确的C++标识符是()。
下列选项中,正确的C++标识符是()。
在容量为15的循环队列中,若头指针front=6,尾指针rear=9,则循环队列中的元素个数为
随机试题
矢状面
临床上治疗非胰岛素依赖型糖尿病(Ⅱ型糖尿病)药物可分为:磺酰脲类、双胍类、葡萄糖苷酶抑制剂和噻唑烷二酮类
下列关于急性坏死溃疡性龈炎的治疗中,错误的是
《中华人民共和国注册测绘师注册证》每一次注册有效期为()。
应付职工薪酬仅包括在职职工和离职后提供给职工的全部货币性薪酬和非货币性福利,不包括解除劳务关系给予的补偿。()
某校语文老师周某在未经学生本人同意的情况下,私自将学生的作文发表,且将作者署上自己的名字。此案例中的周某侵犯了学生的专利权。()
Nomatterwhatyoursituationis,oneofthegreatestdangersnowisthatyou’llstopdoingwhatyou’realreadydoingright.
Mercuryisthenearestplanettothesunanditsperiodof______is88days.
在中国,画家徐悲鸿的名字几乎家喻户晓,原因是他善于画马。徐悲鸿的马,独辟蹊径,无论奔马、立马、走马、饮马、群马,都被赋予了充沛的生命力。20世纪30年代,徐悲鸿先生所作水墨奔马,无羁绊,尚桀骜,发胸中之情怀,掘民族之精神,在写实的形体中充满着浪漫
Tosaythatthechildlearnsbyimitationandthatthewaytoteachistosetagoodexampleoversimplifies.Nochildimitatese
最新回复
(
0
)