include<iostream> #include<string> using namespace s

程序分析题:阅读程序后,填写程序的正确运行结果。


include<iostream>
#include<string>
using namespace std;
class Book {
char*title;
char*author;
int numsold;
public:
Book(){}
Book(const char*strl,const char*str2,const int num) {
int len=strlen(strl);
title=new char[1en+1];
strcpy(title,strl);
len=strlen(str2);
author=new char[1en+1];
strcpy(author,str2);
numsold=num; }
void sethook(const char * strl,const char * str2,const int num) {
int len=strlen(strl);
title=new char[1en+1];
strcpy(title,strl);
len=strlen(str2);
author=new char[1en+1];
strcpy(author,str2);
numsold=num;
}
~Book() {
delete title;
delete author;
}
void print(ostream& output) {
output<<"书名:"<<title<<endl;
output<<"作者:"<<author<<endl;
output<<"月销售量:"<<numsold<<endl;
}
};
void main() {
Book obj1("数据结构","严蔚敏",200),obj2;
obj1.print(cout);
obj2.setbook("C++语言程序设计","李春葆",210);
obj2.print(cout);
}


【正确答案】:

书名:数据结构
作者:严蔚敏
月销售量:200
书名:C++语言程序设计
作者:李春葆
月销售量:210


【题目解析】:

在主函数main中,obj1和obj2都是Book类的实例化。

obj1直接给出参数("数据结构","严蔚敏",200),并调用函数print输出。

obj2是调用 Book类中的setbook函数,并给出参数,再输出。