问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj1下的工程proj1,此工程中包含源程序文件main.cpp,其中有类Book(“书”)和主函数main的定义。程序中位于每个“//ERROR ****found****”之后的一行语句行有错误,请加以改正。改正后程序的输出结果应该是:
书名:C++语言程序设计 总页数:299
已把“C++语言程序设计”翻到第50页
已把“C++语言程序设计”翻到第51页
已把书合上。
书是合上的。
已把“C++语言程序设计”翻到第1页
注意:只修改每个“//ERROR ****found****”下的一行,不要改动程序中的其他内容。
#include <iostream>
using namespace std;
class Book{
char * title;
int num_pages; //页数
int cur page; //当前打开页面的页码,0表示书未打开
public:
Boek(const char * theTitle, int pages):num pages(pages)
{
//ERROR ********** found**********
title =new char[strlen(theTitle)];
strcpy(title, theTitle);
cout <<endl <<"书名:"<<title
<<"总页数:" <<num_pages;
}
Book{){ delete []title; }
// ERROR ********** found**********
bool isOpen () const { return num_pages!=0;} //书打开时返回true,否则返回false
int numOfPages()const{ return num_pages;} //返回书的页数
int currentPage()const{ return cur_page;} //返回打开页面的页码
void openAtPage(int page no){ //把书翻到指定页
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(! isOpen ())
cout << "书是合上的。";
else{
//ERROR ********** found**********
num_pages =0;
cout <<"已把书合上。";
}
cout << endl;
}
};
int main () {
Book book ("C++语言程序设计", 299);
book.openAtPage (50);
book.openAtNextPage ();
book.close ();
book.close ();
book.openAtNextPage ();
return 0;
}
【参考答案】
(1)title=new char[strlen(theTitle)+1]; (2)bool isOpen()const......
(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)