首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h.proj3.cpp、writeToFile.obj。 补充完成proj3.h,重载复合赋值运算符+=
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h.proj3.cpp、writeToFile.obj。 补充完成proj3.h,重载复合赋值运算符+=
admin
2015-06-27
27
问题
请使用VC6或使用【答题】菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3.h.proj3.cpp、writeToFile.obj。
补充完成proj3.h,重载复合赋值运算符+=。
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFih已经编译为obj文件,并且在本程序中调用。
//proj3.h
#include
#include
usingnamespacestd;
classMiniString
{public:
friendostream&operator<<(ostream&output,constMiniString&s)
//重栽流插入运算符
{output<
}
friendistream&operator>>(istream&input,MiniString&s)
//重载流提取运算符
{chartemp[i00];//用于输入的临时数组
temp[0]=’\0’;
input>>setw(100)>>temp;
intinLen=strlenktemp);//输入字符串长度
if(inLen!=0)
{
s.length=inLen;//赋长度
if(s.sPtr!=0)delete[]s.sPtr;
//避免内存泄漏
s.sPtr=newchar[s.length+1];
strcpy(s.sPtr,temp);
//如果s不是空指针,则复制内容
}
elses.sPtr[0]=’\0’;
//如果s是空指针,则为空字符串
returninput;
}
MiniString(constchar*S="":length((s!=0)?strlen(S):0)(setString(S);}
~MiniString()fdelete[]sPtr;)//析构函数
//*************333***********
//+=运算符重载
//*************666***********
private:
intlength;//字符串长度
char*sPtr;//指向字符串起始位置
voidsetString(constchar*string2
}//辅助函数
{
sPtr=newchar[length+1];
//分配内存
if(string2!=0)
strcpy(sPtr,string2);
//如果string2不是空指针,则复制内容
elsesPtr[0]=’\0’;
//如果string2是空指针,则为空字符串
}
};
//proj3.cpp
#include
#include
usingnamespacestd;
#include"proj3.h"
intmain()
{
MiniStringstrl("World"),str2("Hello");
voidwriteToFile(char*);
str2+=strl;//使用重载的+=运算符
cout<
writeToFile("");
return0;
}
选项
答案
MiniString& operator+=(const MiniString& s) { char *pt = new char [length+1]; strcpy(pt, sPtr); int blength = length; length += s.length; delete []sPtr; sPtr = new char[length+1]; strcpy(sPtr,pt); delete []pt; for (int i = 0; i < length; ++i) sPtr[blength+i] = s.sPtr[i]; return *this; }
解析
主要考查考生对运算符重载的掌握,因为有动态数组,所以要使用new语句来重新分配空间。
转载请注明原文地址:https://kaotiyun.com/show/oBBD777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
以秦汉、隋唐和明清为例,分析我们古代国家的历史特征。(北京大学2000年中国通史真题)
一个双因素方差分析实验,A因素为数材,共3种;B囚素为教法,共4种。交叉分析后得到12种处理,假定每个处理中有2名被试。教学后进行测验,得到方差分析表。请补充下表的空白。
次数分布有两个基本特点,分别是()
在一组原始数据中,各个Z分数的标准差为()
以下选项中属于法律创制的主要方式的是()
f(x)=dt在区间(-∞,+∞)内零点的个数为()
下列叙述中,正确的是()。
下列关于Applet的叙述中,正确的是()。
在读字符文件Employee.dat时,使用该文件作为参数的类是()。
随机试题
在神经纤维,Na+通道失活的时间在
消化油腻肉食积滞的要药是( )。
某综合性企业2003年度有关资料如下:(1)8月与外商订立加工承揽合同一份,合同中分别记载加工费金额20万美元,原材料100万美元,签订合同日国家外汇管理局公布的人民币外汇牌价为1:8.27。(2)10月与甲公司签订转让技术合同,转让收
某房地产开发公司开发建设的甲住宅小区已临近竣工,该房地产开发公司通过电视广告的形式发布了招标公告,并在中国物业管理协会网站发布了招标公告,拟通过此方式选聘物业服务企业来管理该小区。根据以上资料,回答以下问题:评标委员会的下列做法中,错误的有(
仓储规划包括()。
谈谈你对服务好“两个健康”的认识。
在教育实际运行中出现的预期的教育结果称作()。
历史上有各种关于人的发展动因的理论和假设。下列学说中,支持“外铄论”观点的是()。
LittleLadyStartsBigWarHarrietBeecherStowehadpouredherheartintoheranti-slaverybook"UncleTom’sCabin."(46)Thepu
Managementjargoncanalienatestaffandleavebosseslookinguntrustworthyandweak,accordingtoasurveypublishedonMonday
最新回复
(
0
)