首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋
请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intArray类的对象中,然后建立另一对象myArray,将对象内容赋
admin
2017-07-05
55
问题
请使用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
clas S intArray
{
private:
int*array;
int length;
public:
intArray(char*filename);
intArray();
intArray&operator=(cons t intAr
ray&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[1ength++])
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(array!=NULL)delete[]array;
length=src.1ength;
array=new int[1ength];
第一条语句是把原来动态数组释放,第二条语句是把形参SYC的成员length赋值给变量length,第三条语句是给指针array分配内存。接下来要把动态数组中的值逐个赋给array数组,在这里使用for循环语句,循环变量i的范围是0一length,并进行赋值操作。
转载请注明原文地址:https://www.kaotiyun.com/show/gLAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
下列程序的运行结果是【 】。#include<iostream.h>#include<string.h>voidmain(){char*a[5]={"stuent","worker","tea
根据输出结果填空完成下面程序。#include<iostream.h>classTest{private:staticintval;inta;public:
关于类和对象不正确的说法是( )。
以下不是数据库系统组成部分的是( )。
以下程序的执行结果是()。#include<iostream>usingnamespacestd;classsample{private:intx;public:
若有以下程序:#include<iostream>usingnamespacestd;classpoint{private:intx,y;public:point(
类istream的成员函数______从指定流中读取一个字符,成员函数______和______从指定流中读取多个字符。
在C++中的实型数据分为三种类型,即单精度float、双精度double和【 】。
read函数的功能是从输人流中读取( )。
以下选项中合法的用户标识符是
随机试题
欲设计一个二十四进制计数器,至少需用【】个触发器。
y〞=sin(-x)的通解为()
A.三臂卡B.RPI卡C.杆卡D.圈形卡E.对半卡用于游离缺失末端基牙,可减小扭力的是
投资者进行房地产投资的主要目的是为了实现其财富价值的最大化。()
督察长由董事长提名,总经理聘任,并应经全体独立董事同意。( )
根据《支付结算办法》的规定,汇款人委托银行将其款项支付给收款人的结算方式是()。
习近平新时代中国特色社会主义思想明确()是中国特色社会主义最本质的特征、中国特色社会主义制度的最大优势。
打击是落实综合治理的关键。()
Auctionsarepublicsalesofgoods,conductedbyanofficiallyapprovedauctioneer.Heasksthecrowdassembledintheauctionr
Commoncoldisaviralinfectionthatstartsintheupperrespiratorytract,sometimesspreadstothelowerstructures,andmay
最新回复
(
0
)