首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
使用VC6打开考生文件夹proj3下的工程proj3,其中定义了一个字符串变量类StringVar。类成员的说明在程序注释中。请在//********333********和********666********之间填写StringVar成员函数和友元函数
使用VC6打开考生文件夹proj3下的工程proj3,其中定义了一个字符串变量类StringVar。类成员的说明在程序注释中。请在//********333********和********666********之间填写StringVar成员函数和友元函数
admin
2017-02-21
84
问题
使用VC6打开考生文件夹proj3下的工程proj3,其中定义了一个字符串变量类StringVar。类成员的说明在程序注释中。请在//********333********和********666********之间填写StringVar成员函数和友元函数的实现代码。在main函数中给出了一组测试数据,运行时输入:
Hello Kitty
此情况下程序的输出应该是:
Hello Kitty
Borg
Borg
注意:只需在//********333********和//********666********之间填入所编写的若干语句,不要改动程序中的其他内容。
//StringVar.h
#include<iostream>
#include<cstdlib>
#include<cstddef>
#include<cstring>
using namespace std;
void writeToFile(const char *
path);
class StringVar
{
public:
StringVar(int size);//构造函数,size为字符串长度(字符个数)初始值;字符串内容初始化为空串
StringVar (const char a[]);//构造函数,用参数数组a的内容初始化当前对象
StringVar(const StringVar&strobj);//复制构造函数
~StringVar(){delete[]value;};//析构函数
int length() const{return strlen(value);}
//从输入流ins输入一个字符串,其中可以包括空格
void input_line(istream&ins);
//返回字符串首地址
char * getValue()const{return value;}
private:
char * value;//字符串首地址
int max_length;//字符串最大长度(字符个数最大值)
};
//将the_string通过输出流outs输出
ostream&operator<<(ostream&outs,const StringVar the_string);
//main.cpp
#include<iostream>
#include<string>
#include"StringVar.h"
//*********333*********
//*********666*********
int main()
{
StringVar namel(30),name2("Borg");
name1.input line(cin);
StringVar name3(name2);
cout<<name1<<end1;
cout<<name2<<end1;
cout<<name3<<end1;
writeToFile(".、\\");
return 0;
}
//writeToFile.cpp
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
#include"StringVar.h"
void writeToFile(const char *
path)
{
char filename[30];
strcpy(filename,path);
strcat(filename,"out.dat");
ofstream fout(filename);
istringstream is(string("Jenny Zheng"));
StringVar namel(40),name2("John");
name1.input_line(is);
StringVar name3(name2);
fout<<name1<<name2<<name3;
fout.close();
}
选项
答案
StringVar∷StringVar(int size):max_length(size)//使用成员初始化列表初始化max_length { value=new char[size]; value[0]=’\0’; } StringVar∷StringVar(constchar a[]) { max_length=strlen(a)+1; value=new char[max_length]; strcpy(value,a); } StringVar∷StringVar(const StringVar&strobj) { max_length=strobj.max_length; value=new char[strlen(strobj.value)+1]; strcpy(value,strobj.value); } void StringVar∷input_line(istream&ins) { int i=0; char ch; while(i<max length-1&&(ch=ins.get())!=’\n’) { value[i++]=ch; } value[i]=’\O’; } ostream&operator<<(ostream&outs, const StringVar the_string) { outs<<the string.getValue()<<end1; return outs; }
解析
转载请注明原文地址:https://kaotiyun.com/show/ABAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
【】是数据库设计的核心。
软件危机出现于60年代末,为了解决软件危机,入们提出了【】的原理来设计软件,这就是软件工程诞生的基础。
有以下程序#include<string.h>#include<iostream.h>voidmain(){char*p="abcde\0fghjik\0";co
栈底至栈顶依次存放元素A、B、C、D,在第五个元素E入栈前,栈中元素可以出栈,则出栈序列可能是
函数重载要求重载的函数必须有和原函数一样的【】。
通过使用new和delete两个运算符进行的分配为【】存储分配。
下面关于虚函数的描述中正确的是
数据流图有两种典型的结构形式,它们分别是【 】和事务型。
下列程序时类D代码段出现编译错误,原因是【 】。#include<iostream.h>classA{public:A(charc){cout<<"A’sconstructor."<<c<<endl
执行完下列代码段之后:boolx=true,y=false,z=false;x=x&&y||z;y=x||y&&z;z=!(x!=y)||(y==z);则x=false,y=false,z=(
随机试题
Peoplewhousuallychowdownonchillipeppersmayliveforlongerandhaveasignificantlyreducedriskofdyingfromcardiovas
1789年美国联邦政府成立之初下设的部有()
癌前病变:
某患者因右侧舌下腺囊肿接受右舌下腺及囊肿摘除术后出现右颌下区胀痛,进食时症状加重,最可能的原因是
甘草中甘草甜素含量最高的时期为
图示三铰拱y=4f/l2(l-x),l=16m,D右侧截面的弯矩值为()。
利率互换采用()的方式。
2008年一季度企业景气指数最高的行业是()。2008年一季度企业家信心指数比上季度上升最少的行业是()。
A、 B、 C、 D、 B
【S1】【S3】
最新回复
(
0
)