操作题 1.  请使用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:
    Book(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;
    }
【正确答案】title=new char[strlen(theTitle)+1];
   bool isOpen()const{return cur_page !=0;}
   cur_page=0;
【答案解析】[考点] 本题考查的是Book类,其中涉及构造函数、析构函数、bool函数和const函数。
    (1)主要考查考生对动态分配的掌握情况,如果要复制字符串theTitle,就要分配空间,空间大小应该为theTitle的长度加1。
   (2)主要考查考生对bool函数的掌握情况,根据私有成员定义:int cur_page;//当前打开页面的页码,0表示书未打开,可知应该返回cur_page !=0;。
   (3)主要考查考生对成员函数的掌握情况,根据私有成员定义:int cur_page;//当前打开页面的页码,0表示书未打开,可知应给cur_page赋值为0。