首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intAn’ay类的对象中,然后建立另一对象myArray,将对象内容赋
请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intAn’ay类的对象中,然后建立另一对象myArray,将对象内容赋
admin
2019-06-05
57
问题
请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,其功能是从文本文件in.dat中读取全部整数,将整数序列存放到intAn’ay类的对象中,然后建立另一对象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 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[1ength];
}
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.1ength;
array=new int[1ength];
//*********333*********
//*********666*********
return*this;
}
intArray::一intArray()
{
delete[]array;
}
void intArray::show()
{
int step=0;
for(int i=0;i
{
cout<
<
step++;
}
}
void main()
{t
intArray*arrayP=new intArray
(“input.dat”);
intArray myArray;
myArray=*arrayP;
(*arrayP).show();
delete arrayP;
writeToFile(””);
}
选项
答案
for(int i=0;i
解析
本题主要考查intAtray类,其中涉及动态数组、构造函数、运算符重载、析构函数及其他成员函数。
【解题思路】
主要考查考生对运算符重载的掌握,该函数要重载运算符“=”,该运算符的意思是赋值。先看该函数的其他语句:
if(array!=NULL)delete[]array;
length=src.1ength;
array=new int[1ength];
第一条语句是把原来动态数组释放,第二条语句是把形参$1rc的成员length赋值给变量length,第三条语句是给指针array分配内存。接下来要把动态数组中的值逐个赋给array数组,在这里使用for循环语句,循环变量i的范围是0~length,并进行赋值操作。
转载请注明原文地址:https://kaotiyun.com/show/S18p777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
执行下列语句:#include<iostream>usingnamespacestd;intmain(){intx=3;if(x=5)cout<<x++<<end1;elsec
有如下程序:#include<iostream>usingnamespacestd;classTestClass{staticinti;public:TestClass(){i++;
类中的一个字符指针成员指向具有m个字节的存储空间时,它所能存储的字符串的最大长度为()。A)m-2B)m-1C)mD)m+1
在公有派生时,派生类中定义的成员函数只能访问原基类的()。A)私有成员、保护成员和公有成员B)保护成员和私有成员C)公有成员和保护成员D)公有成员和私有成员
C++中将两个字符串连接起来组成一个字符串的函数是______。
函数Min的功能是返回具有n个元素的数组array中的最小值。请将横线处的缺失部分补充完整,使得程序的输出结果为1.24。#include<iostream>usingnamespacestd;template<typename
请在横线处填写派生类Derived的继承方式,使得程序的输出结果为Base。#include<iostream>usingnamespacestd;classBase{public:voidprint(){c
有如下程序:#include<iostream>usingnamespacestd;classSac{intn;public:Sac():n(4){cout<<n;}Sac(intk):n(
友元类的所有成员函数都是另一个类的______。
随机试题
某物品的年需求量为3000单位,物品的单位成本为12元,平均订货成本为20元/次,持有成本库存费用率为25%,则该物品的经济订购批量为()
调节血Ca2+浓度的激素有
痄腮热毒蕴结型的首选方是:
膀胱造影的方法不包括
短效口服避孕药含
教师的知识结构中,小学英语教师所拥有的英语学科知识属于()。
设f(x)有界,且f’(x)连续,对任意的x∈(-∞,+∞)有|f(x)+f’(x)|≤1.证明:|f(x)|≤1.
Inrecentyearsmanycountriesoftheworldhavebeenfacedwiththeproblemofhowtomaketheirworkersmoreproductive.Some
SheisoneofthefewpeoplehereIcanunderstandproperly;shepronouncesallherwordsso______.
Evenamodestamountofstepsisassociatedwithlowermortality.
最新回复
(
0
)