全国计算机二级C++考试题库及答案.doc
第一题
#includeiostream
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)//Book(const char *theTitle, int pages) num_pages(pages)
{
title=new char[strlen(theTitle)+1];
strcpy(title,theTitle);
coutendl书名:title
总页数:num_pages;
}
~Book(){ delete []title; }
bool isClosed()const{ return cur_page==0;} //书合上时返回true,否则返回false
bool isOpen()const{ return !isClosed();} //书打开时返回true,否则返回false
int numOfPages()const{ return num_pages;} //返回书的页数
int currentPage()const{ return cur_page;} //返回打开页面的页码
// ERROR **********found**********
void openAtPage(int page_no){ //把书翻到指定页//void openAtPage(int page_no)const{
coutendl;
if(page_no1 || page_nonum_pages) {
cout无法翻到第 cur_page 页。;
close();
}
else {
cur_page=page_no;
cout已把“title”翻到第 cur_page 页;
}
}
void openAtPrevPage(){ openAtPage(cur_1); } //把书翻到上一页
void openAtNextPage(){ openAtPage(cur_page+1); } //把书翻到下一页
void close(){ //把书合上
coutendl;
if(isClosed())
cout书是合上的。;
else{
// ERROR **********found**********
cur_page=0;//num_page=0;
cout已把书合上。;
}
coutendl;
}
};
int main(){
Book book(C++语言程序设计, 299);
book.openAtPage(50);
book.openAtNextPage();
book.openAtNextPage();
book.openAtPrevPage();
book.close();
cout当前页: book.currentPage()endl;
return 0;
}
第二题
#includeiostream
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()const{ return room_no; } //返回房间号
double theLength()const{ return length; } //返回房间长度
double theWidth()const{ return width; } //返回房间宽度
//
原创力文档

文档评论(0)