问答题
请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2。此工程中包含一个源程序文件main.cpp,其中有“书”类Book及其派生出的“教材”类TeachingMaterial的定义,还有主函数main的定义。请在横线处填写适当的代码并删除横线,以实现上述类定义和函数定义。该程序的正确输出结果应为:
教材名:C++语言程序设计
页数:299
作者:张三
相关课程:面向对象的程序设计
注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
#include <iostream>
using namespace std;
class Book{ //"书"类
char * title; //书
int num_pages; //页数
char * writer; //作者姓名
public:
//********** found**********
Book(const char * the title, int pages, const char * the writer):
______{
title =new char[ strlen (the_titie) +1];
strcpy(title,the_title);
//********** found**********
______
strcpy(writer,the_writer);
}
~Book(){ delete []title; delete []writer; }
int numOfPages()const{ return numpages;} //返回书的页数
const char * theTitle () const { return title; } //返回书名
const char * theWriter () const{ return writer; } //返回作者名
};
class TeachingMaterial: public Book{ //“教材”类
char * course;
public:
TeachingMaterial (const char * the_title, int pages, const char * the_writer, const char * the_course)
//********** found**********
:______{
course = new char [ strlen (the_course) +i];
strcpy (course, the_course);
}
~TeachingMaterial () { delete [ ]course; }
const char * theCourse () const{ return course; } //返回相关课程的名称
};
int main () {
TeachingMaterial abook ("C++语言程序设计", 299, "张三", "面向对象的程序设计");
cout <<"教材名:" << a book.theTitle() <<endl
<< "页数:" << a_book.numOfPages ()<< endl
<<"作者:" <<a book.theWriter ()<< endl
//********** found**********
<< "相关课程:" <<______;
cout << endl;
return 0;
}
【正确答案】(1)num_pages(pages)
(2)writer=new char[strlen(the_writer)+1];
(3)Book(the_title, pages, the_writer)
(4)a_book.theCourse()
【答案解析】[考点] 本题考查的是Book类及其派生类TeachingMaterial类,其中涉及构造函数、析构函数和const函数。
[解析] (1)主要考查考生对构造函数的掌握情况,使用成员列表进行初始化。
(2)主要考查考生对动态分配的掌握情况,使用new给writer分配空间。
(3)主要考查考生对构造函数的掌握情况,使用成员列表初始化给基类初始化。
(4)主要考查考生对成员函数调用的掌握情况,函数theCourse()返回相关课程。