c_c++语言程序设计笔试面试题22

c_c++语言程序设计笔试面试题22

ID:12860433

大小:82.50 KB

页数:12页

时间:2018-07-19

c_c++语言程序设计笔试面试题22_第1页
c_c++语言程序设计笔试面试题22_第2页
c_c++语言程序设计笔试面试题22_第3页
c_c++语言程序设计笔试面试题22_第4页
c_c++语言程序设计笔试面试题22_第5页
资源描述:

《c_c++语言程序设计笔试面试题22》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、下面是微软的两道笔试题....3.ImplementastringclassinC++withbasicfunctionalitylikecomparison,concatenation,input andoutput.Pleasealsoprovidesometestcasesandusingscenarios(samplecodeofusingthis class).PleasedonotuseMFC,STLandotherlibrariesinyourimplementation.我的实现方案如下,这道题真地对c++的主要特性都进行了较好地考察.String

2、.h:#ifndefSTRING_H#defineSTRING_H#includeusingnamespacestd;classString{public:String();String(intn,charc);String(constchar*source);String(constString&s);//String&operator=(char*s);String&operator=(constString&s);~String();char&operator[](inti){returna[i];}constchar&operator[

3、](inti)const{returna[i];}//对常量的索引.String&operator+=(constString&s);intlength();friendistream&operator>>(istream&is,String&s);//搞清为什么将>>设置为友元函数的原因.//friendbooloperator<(constString&left,constString&right);friendbooloperator>(constString&left,constString&right);//下面三个运算符都没必要设成友元 函数,这里是为

4、了简单.friendbooloperator==(constString&left,constString&right);friendbooloperator!=(constString&left,constString&right);private:char*a;intsize;};#endifString.cpp:#include"String.h"#include#includeString::String(){a=newchar[1];a[0]='';size=0;}String::String(intn,charc

5、){a=newchar[n+1];memset(a,c,n);a[n]='';size=n;}String::String(constchar*source){if(source==NULL){a=newchar[1];a[0]='';size=0;}else{size=strlen(source);a=newchar[size+1];strcpy(a,source);}}String::String(constString&s){size=strlen(s.a);//可以访问私有变量.a=newchar[size+1];//if(a==NULL)strc

6、py(a,s.a);}String&String::operator=(constString&s){if(this==&s)return*this;else{delete[]a;size=strlen(s.a);a=newchar[size+1];strcpy(a,s.a);return*this;}}String::~String(){delete[]a;//}String&String::operator+=(constString&s){intj=strlen(a);intsize=j+strlen(s.a);char*tmp=newchar[size+1

7、];strcpy(tmp,a);strcpy(tmp+j,s.a);delete[]a;a=tmp;return*this;}intString::length(){returnstrlen(a);}main.cpp:#include#include"String.h"usingnamespacestd;booloperator==(constString&left,constString&right){inta=strcmp(left.a,right.a);if(a==0)returntrue;elsereturnfalse;}boolope

8、rator

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。