class book
{
char* title;
int num_pages;
int cur_page;
public:
book(const char* theTitle, int pages) :num_pages(pages)
{
title = new char[strlen(theTitle) + 1];
strcpy(title, theTitle);
int x = strlen(title);
cout << endl << "书名:" << title << endl << "总页数 :" << num_pages;
}
~book()
{
delete[] title;
}
bool isClased()const{ return cur_page == 0; }
bool idOpen()const{ return !isClased(); }
int numOfpage()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 << "没有" << page_no << "页";
}
else
{
cur_page = page_no;
cout << title << "打开到" << cur_page << "页";
}
}
void openAtPrevPage(){ openAtPage(cur_page - 1); }
void openAtNextPage(){ openAtPage(cur_page + 1); }
void close()
{
if (isClased())
{
cout << "书是合着呢" << endl;
}
else{ cur_page = 0; cout << "书已经合上了" << endl; }
}
};
int _tmain()
{
book iBook("C++语言程序设计 ", 299);
iBook.openAtPage(50);
iBook.openAtNextPage();
iBook.openAtNextPage();
iBook.openAtPrevPage();
iBook.close();
cout << "当前页:" << iBook.currentPage() << endl;
return 0;
}