问答题

请使用VC6或使用[答题] 菜单打开考生文件夹proj3下的工程文件proj3。本题创建一个小型字符串类,字符串长度不超过100。程序文件包括proj3. h、proj3. cpp、writeToFile. obj。补充完成重载赋值运算符函数,完成深复制功能。
屏幕上输出的正确结果应该是:
Hello!
Happy new year!
要求:
补充编制的内容写在“//**********333**********”与“//**********666**********”两行之间。不得修改程序的其他部分。
注意:
程序最后调用writeToFile函数,使用另一组不同的测试数据,将不同的运行结果输出到文件out. dat中。输出函数writeToFile已经编译为obj文件。
//proj3. h
#inelude < iostream >
#include < iomanip >
using namespaee std;
class MiniString

public :
friend ostream &operator << ( ostream &output, const MiniString &s ) //重载流插入运算符
output << s. sPtr; return output;
friend istream &operator >> ( istrearn &input, MiniString &s )//重载流提取运算符
char temp [100]; //用于输入的临时数组
temp[0] = ’\0’; //初始为空字符串
input >> setw( 100 ) >> temp;
int inLen = strlen(temp) ;//输入字符串长度
if( inLen != 0)

s. length = inLen; //赋长度
if( s. sPtr! = 0) delete []s. sPtr; //避免内存泄漏
s. sPtr = new char [s. length + 1] ;
strepy( s. sPtr, temp ) ; //如果s不是空指针,则复制内容

else s. sPtr [0] = ’\0’; //如果s是空指针,则为空字符串
return input ;

void modString( const char * string2 ) //更改字符串内容

if ( string2 ! = 0 ) // 如果string2不是空指针,则复制内容

if ( strlen(string2) ! = length)

length = strlen(string2) ;
delete [] sPtr;
sPtr = new char [length + 1]; //分配内存

strepy( sPtr, string2 );
else sPtr [0] = ’\0’;//如果string2是空指针,则为空字符串

MiniString& operator = ( const MiniString &otherString) ;
MiniString( constchar *s = " "):length(( s ! = 0 ) strlen( s ) : 0 )//构造函数

sPtr = 0 ;
if ( length ! = O)
setString( s );

~MiniString()//析构函数
delete [] sPtr;
private :
int length; //字符串长度
char * sPtr; //指向字符串起始位置
void setString( const char * string2 ) //辅助函数

sPtr = new char [strlen(string2) + 1]; //分配内存
if ( string2 ! = 0 ) strcpy( sPtr, string2 ) ; //如果string2不是空指针,则复制内容
else sPtr [0] = ’\0’; //如果string2是空指针,则为空字符串

;
//proj3. cpp
#include < iostream >
#include < iomanip >
using namespace std;
#include "proj3. h"
MiniString& MiniString: :operator = (const MiniString &otherString)
//重载赋值运算符函数。提示:可以调用辅助函数setString
//************* 333 **********
//************* 666 **********
int main()

MiniString str1 ( "Hello !" ), str2 ;
void writeToFile (const char * ) ;
str2=str1; //使用重载的赋值运算符
str2. modString( "Happy new year!" ) ;
cout << str1 <<’\n’;
cout << str2 <<’\n’;
writeToFile (" ") ;
return 0 ;

【参考答案】

length=otherString. length;
//把对象字符串otherString的长度赋值给变量......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
请使用VC6或使用[答题] 菜单打开考生文件夹proj2下的工程proj2。此工程中包含一个源程序文件main. cpp,其中有“房间”类Room及其派生出的“办公室”类Office的定义,还有主函数main的定义。请在程序中“ ****found****”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为: 办公室房间号:308 办公室长度:5.6 办公室宽度:4.8 办公室面积:26.88 办公室所属部门:会计科 注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“ ****found****”。 #include<iostream> using namespace std; class Room 房间 int room_no; 房间号 double length; 房间长度(m) double width; 房间宽度(m) public : Room( int the_room_no, double the length, double the_width ) : roomno (the_room_no) , length ( the_length) , width ( the_width ) int theRoomNo() const return roomno ; 返回房间号 double theLength()const return length; 返回房间长度 double theWidth()const return width; 返回房间宽度 ********** found ********** double theArea () const ______ 返回房间面积(矩形面积) ; class Office: public Room 办公室 类 char * depart; 所属部门 public : Office(int the_room_no, double the_length, double the_width, const char * the_depart) ********** found ********** :______ depart = new char[ strlen(the_depart) + 1 ] ; ********** found ********** strcpy (______) ; ~Office() delete []depart; const char * theDepartment() const return depart; 返回所属部门 ; int main() ********** found ********** Office ______; cout << 办公室房间号: << an_office. theRoomNo() << endl << 办公室长度: << an_office. theLength() << endl << 办公室宽度: << an_office. theWidth() << endl << 办公室面积: << an_office. theArea() << endl << 办公室所属部门 << an_office. theDepartment() << endl; return 0 ;