问答题

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

public:
friend ostream &operator<< ostream &output,const MiniString &s)
//重载流插入运算符
output <<s.sPtr; return output;
friend istream &operator>>(istream &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];
strcpy(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];
//分配内存

strcpy(sPtr,string2);

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

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

sPtr=0;
if(length!=0)
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 "proj 3.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的长度赋值给变量l......

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

问答题
请使用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):room_no(the_room_no),length(the_length),width(the width) int theRoomNo()constreturn room_no; 返回房间号 double theLength()const returnlength;) 返回房间长度 double theWidth()const returnwidth; 返回房间宽度 **********found********** double theArea()const______ 返回房间面积(矩形面积) ; class Office:public Room “办公室”类char*depart; 所属部门 public: Office (int the_room_no.double the_length,double the_width,constchar*the_depart) **********found********** :______ depart=new char[strlen(the depart)+1]; **********found********** strcpy(______); ~Office()delete[]depart; const char*theDepartment()constreturn depart;) 返回所属部门 ; int main() **********found********** Office______; cout<< 办公室房间号: <<an office.theRoomNo()<<endl << 办公室长度: <<an office.the-Length()<<endl << 办公室宽度: <<an office.the-Width()<<endl << 办公室面积: <<an_office.theArea()<<endl << 办公室所属部门: <<an office.theDepartment()<<endl; return 0;
问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中包含一个源程序文件main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个“ ERROR****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是: 书名:C++语句程序设计总页数:299 已把“C++语言程序设计”翻到第50页 已把“C++语言程序设计”翻到第51页 已把“C++语言程序设计”翻到第52页 已把“C++语言程序设计”翻到第51页 已把书合上。 当前页:0 注意:只修改每个“ ERROR ****found****”下的那一行,不要改动程序中的其他内容。 #include<iostream> using namespace std; class Book char*title; int num_pages; 页数 int cur_page; 当前打开页面的页码,0表示书未打开 public: ERROR **********found********** Book(const char*theTitle,int pages)num_pages(pages) title=new char[strlen(theTitle)+1]; strcpy(title,theTitle); cout<<endl<< 书名: <<title<< 总页数: <<num_pages; ~Book()delete[]title;) bool isClosed()constreturn cur_page==0; 书合上时返回true,否则返回falsebool isOpen()constreturn! isClosed();) 书打开时返回true,否则返回falseint numOfPages()const return num_pages;) 返回书的页数 int currentPage()const return cur_page;) 返回打开页面的页码 ERROR **********found********** void openAtPage (int page_no) const( 把书翻到指定页 cout<<endl; if(page_no<1 ||page_no>num_pages) cout<< 无法翻到第 <<cur_page<< 页。 ; close(); else cur_page=page_no; cout<< 已把“ <<title<< ”翻到第 <<cur_page<< 页 ; void openAtPrevPage()openAtPage(cur_page-1); 把书翻到上一页 void openAtNextPage()openAtPage(cur_page+1); 把书翻到下一页 void close() 把书合上 cout<<endl; if(isClosed()) cout<< 书是合上的。 ; else ERROR**********found********** num_pages=0; cout<< 已把书合上。 ; cout<<endl; ; int main() Book book( C++语言程序设计 ,299); book.openAtPage(50); book.openAtNextPage(); book.openAtNextPage(); book.openAtPrevPage(); book.close(); cout<< 当前页: <<book.current-Page()<<endl; return 0;