首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请使用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
28
问题
请使用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://jikaoti.com/ti/TXXiFFFM
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
根据以往的经验,北京市大一新生患抑郁症的城市生源与农村生源比例是2:1,今年大一新生患抑郁症的情况是城市生源35人,农村生源是25人,那么fe城市为()
在教学中教师通过指导学生运用一定的仪器设备进行独立实验作业而获得知识和技能的方法是()
关于终身教育,下列说法错误的是
甲、乙签订购房合同,合同约定,甲通过向银行贷款方式购买乙的房屋,甲预先支付首付款,剩余款项待银行贷款发放后1个月内支付完毕。甲支付首付款后,由于国家调高了房屋贷款买房的银行利率,致使购房合同无法履行。对于甲、乙而言,银行利率调整这一事实属于
设z=χy+χF(),其中F为可微函数,则为().
已知三元二次型f(x1,x2,x3)=xTAx其矩阵A各行元素之和均为0,且满足AB+B=0,其中(Ⅰ)用正交变换把此二次型化为标准形,并写出所用正交变换;(Ⅱ)若A+kE正定,求k的取值.
如下图所示,函数f(x)是以2为周期的连续周期函数,它在[0,2]上的图形为分段直线.g(x)是线性函数,则_______
质量为M,长为L的均匀杆AB吸引着质量为m的质点C,C位于AB的延长线上并与近端距离为a.已求得杆对质点C的引力其中k为引力常数,现将质点C在杆的延长线上从距离近端r0处移至无穷远处时,其引力做的功为_______.
下列叙述中不属于三层B/S结构数据库应用系统特点和操作特征的是()。
在考生文件夹下的“职员管理”数据库中完成下列基本操作:(1)为“员工信息”表增加一个字段,字段名为“EMAIL”,类型为“字符型”,宽度为“20”。(2)设置“员工信息”表中“性别”字段的有效性规则,性别取“男”或“女”,默认值为“女”。
随机试题
求定积分
A.沙丁胺醇B.氨茶碱C.酮替芬D.色甘酸钠抑制肥大细胞释放介质
引起中性粒细胞减少的疾病是
狭窄性腱鞘炎疗效较好的方法是
女,48岁。反复双手近端指间关节、膝关节痛伴晨僵2年,肘部伸侧可触及皮下结节,质硬、无触痛。诊断首先考虑
中国的陈某和尼加拉瓜国籍的约克订立某种当地少数民族装饰品的大宗买卖合同,后因约克供货不符合中方要求,该案在中国法院涉诉。则关于该案的定性和法律查明问题,下列错误的有:
关于破坏社会主义市场经济秩序罪的认定,下列哪一选项是错误的?
确需延长的,必须经房屋拆迁主管部门批准,且延长暂停期不得超过()。
在施工测量控制中,工业建筑的测量复核通常包括()。
信用证项下单据应在信用证有效期和交单期内向银行提交,如果信用证对交单期未做规定,则交单期不得迟于运输单据日期后的15天,并且不得迟于信用证的有效期。()
最新回复
(
0
)