浙大远程教育面向对象程序设计离线作业参考答案

更新时间:2024-01-01 08:40:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

浙江大学远程教育学院 《面向对象程序设计》课程作业

姓名: 年级:

学 号: 学习中心:

—————————————————————————————

第2章

【2.3】 测试下面的注释(它在C++风格的单行注释中套入了类似于C的注释)是

否有效。

//this is a strange /*way to do a comment*/ 答:经过测试,这样的嵌套注释是有效的。

【2.4】 以下这个简短的C++程序不可能编译通过,为什么? #include

using namespace std; int main() {int a,b,c;

cout<<\ cin>>a>>b; c=sum(a,b); cout<<\ return 0;

}

sum(int a,int b) { return a+b; }

答:因为main()函数中要用到sum()函数,所以sum()函数必须要在

main()函数之前定义或申明,否则编译器在编译main()函数时会认为sum()函数未定义。

【2.5】 回答问题。

(1) 以下两个函数原型是否等价:

float fun(int a,float b,char *c); float fun(int,float,char * );

答:等价,因为函数原型可以缺省参数名。 (2) 以下两个函数的第一行是否等价:

float fun(int a,float b,char * c); float fun(int,float,char * );

答:不等价,因为函数的第一行必须包含参数名。

【2.6】 下列语句中错误的是( D )。

A.int *p=new int(10); B.int *p=new int[10]; C.int *p=new int; D.int *p=new int[40](0);

【2.7】 假设已经有定义“const char * const name=\”下面的语句中

正确的是( D )。

A. name[3]='a'; B. name=\C. name=new char[5]; D. cout<

【2.8】 假设已经有定义“char * const name=\”下面的语句中正确的

是( A )。

A. name[3]='q'; B. name=\C. name=new char[5]; D. name=new char('q');

【2.9】 假设已经有定义“const char * name=\”下面的语句中错误的

是( A )。

A. name[3]='q'; B. name=\C. name=new char[5]; D. name=new char('q');

【2.10】重载函数在调用时选择的依据中,( B )是错误的。 A.函数名字 B.函数的返回类型 C.参数个数 D.参数的类型 【2.11】 在( A )情况下适宜采用内联函数。

A.函数代码小,频繁调用 B.函数代码多,频繁调用 C.函数体含有递归语句 D. 函数体含有循环语句

【2.12】 下列描述中,( C )是错误的。 A. 内联函数主要解决程序的运行效率问题

B. 内联函数的定义必须出现在内联函数第一次被调用之前 C. 内联函数中可以包括各种语句 D. 对内联函数不可以进行异常接口声明

【2.13】 在C++中,关于下列设置默认参数值的描述中,( B )是正确的。 A.不允许设置默认参数值

B.在指定了默认值的参数右边,不能出现没有指定默认值的参数 C.只能在函数的定义性声明中指定参数的默认值

D. 设置默认参数值时,必须全部都设置

【2.14】 下面的类型声明中正确是( D )。 A. int &a[4]; B. int &*p; C.int &&q; D. int i,*p=&i; 【2.15】 下面有关重载函数的说法中正确的是( C )。 A.重载函数必须具有不同的返回值类型 B.重载函数形参个数必须不同 C. 重载函数必须有不同的形参列表 D.重载函数名可以不同

【2.16】 关于new运算符的下列描述中,( D )是错误的。 A,它可以用来动态创建对象和对象数组

B. 使用它创建的对象或对象数组可以使用运算符delete删除 C. 使用它创建对象时要调用构造函数 D. 使用它创建对象数组时必须指定初始值

【2.17】 关于delete运算符的下列描述中,( C )是错误的。 A.它必须用于new返回的指针 B. 使用它删除对象时要调用析构函数 C. 对一个指针可以使用多次该运算符

D.指针名前只有一对方括号符号,不管所删除数组的维数

【2.18】 写出下列程序的运行结果。 #include

using namespace std; int i=15; int main() { int i;

i=100; ::i=i+1;

cout<<::i<

return 0;

}

答:运行结果是: 101

【2.19】 写出下列程序的运行结果。

#include using namespace std; void f(int &m,int n) { int temp; temp=m; m=n; n=temp; } int main() { int a=5,b=10; f(a,b);

cout<

答:运行结果是: 10 10

【2.20】 分析下面程序的输出结果。

#include using namespace std; int &f(int &i) { i+=10; return i; } int main() { int k=0; int &m=f(k); cout<

答:输出结果是: 10 20

【2.21】 举例说明可以使用const替代#define以消除#define的不安全性。

答:举例如下:

#include #define x 3+5 #define y 2*x int main() {

cout<

上述程序的运行结果是11而不是16,但很容易被误认为是16。 下面使用const来替代#define,实现这段程序。 #include const x=3+5; const y=2*x; int main() {

cout<

使用const以后,运行结果为16。可见:使用const替代了#define,

就可以消除#define的不安全性。

【2.22】 编写一个C++风格的程序,用动态分配空间的方法计算Fibonacci 数

列的前20项,并存储到动态分配的空间中。 答:程序编写如下: #include using namespace std; int main() {

int * p=new int[20]; *p=1;

*(p+1)=1;

cout<<*p<<\ p=p+2;

for(int i=3;i<=20;i++) {

*p=*(p-1)+*(p-2); cout<<*p<<\

if(i%5==0)cout<

system(\ return 0; }

运行结果是:

【2.23】 编写一个C++风格的程序,建立一个被称为sroot()的函数,返回其参

数的二次方根。重载sroot()3次,让它返回整数、长整数与双精度数的二次方根(计算二次方根时,可以使用标准库函数sqrt())。 答:程序编写如下: #include #include

using namespace std; double sroot(int i)

{

return sqrt(i); }

double sroot(long l) {

return sqrt(l); }

double sroot(double d) {

return sqrt(d); }

int main() {

int i=2; long l=1688; double d=16.88;

cout<

运行结果是:

【2.24】 编写一个C++风格的程序,解决百钱问题:将一元人民币兑换成1、2、

5分的硬币,有多少种换法? 答:程序编写如下: #include using namespace std; int main() {

int i,j,sum=0; for(i=0;i<=20;i++) {

for(j=0;j<=50;j++)

{

if(100-5*i-2*j>=0) {

sum++;

cout<<100-5*i-2*j<<\ } } }

cout<<\ system(\ return 0; }

运行结果是:

将一元人民币兑换成1、2、5分的硬币,有541种换法。

【2.25】 编写一个C++风格的程序,输入两个整数,将它们按由小到大的顺序

输出。要求使用变量的引用。 答:程序编写如下: #include using namespace std; void sort(int &x,int &y) {

if (x>y) {

int temp; temp=x; x=y; y=temp; } }

int main() {

int a,b;

cout<<\请输入两个整数:\ cin>>a>>b; sort(a,b);

cout<<\从小到大的顺序为:\ system(\ return 0; }

运行结果是:

第三章:

【3.7】 在下面有关对构造函数的描述中,正确的是( B )。 A.构造函数可以带有返回值 B.构造函数的名字与类名完全相同 C.构造函数必须带有参数 D.构造函数必须定义,不能默认

【3.8】 在声明类时,下面的说法正确的是( C )。 A.可以在类的声明中给数据成员赋初值 B.数据成员的数据类型可以是register

C.private、public、protected可以按任意顺序出现

D.没有用private、public、protected定义的数据成员是公有成员 【3.9】 在下面有关析构函数特征的描述中,正确的是( C )。 A.一个类中可以定义多个析构函数 B. 析构函数名与类名完全相同 C. 析构函数不能指定返回类型 D. 析构函数可以有一个或多个参数 【3.10】 构造函数是在( B )时被执行的。 A.程序编译 B. 创建对象 C. 创建类 D.程序装人内存 【3.11】 在下面有关静态成员函数的描述中,正确的是( B )。 A.在静态成员函数中可以使用this指针 B. 在建立对象前,就可以为静态数据成员赋值 C. 静态成员函数在类外定义时,要用static前缀 D. 静态成员函数只能在类外定义

【3.12】 在下面有关友元函数的描述中,正确的说法是〔 A )。 A.友元函数是独立于当前类的外部函数

B. 一个友元函数不能同时定义为两个类的友元函数 C. 友元函数必须在类的外部定义

D. 在外部定义友元函数时,必须加关键字friend

【3.13】 友元的作用之一是(A)。

A.提高程序的运行效率 B.加强类的封装性 C. 实现数据的隐藏性 D. 增加成员函数的种类 【3.14】 以下程序运行的结果是( B )。

#include using namespace std; class B{ public: B(){} B(int i,int j) { x=i; y=j; }

void printb()

{cout<

A(int I,int j); void printa(); private: B c; };

A::A(int i,int j):c(i,j) {}

3.15】void A::printa() {c.printb(); } int main() { A a(7,8); a.printa(); return 0; }

A.8,9 B. 7,8 以下程序的运行结果是( A )。#include

using namespace std; class A{ public:

void set(int i,int j) { x=i; y=j; } int get_y() {return y; } private: int x,y; }; class box{ public:

void set(int l,int w,int s,int p) { length=1; width=w; label.set(s,p);

C. 5,6 D. 9,10

【 3.16】 }

int get_area() {return length*width; } private:

int length,width; A label; }; int main() { box b; b.set(4,6,1,20);

cout<

A.24 B. 4 C. 20 D. 6

以下程序的运行结果是( B#include

using namespace std; class Sample{ public:

Sample(int i,int j) { x=i; y=j; } void disp()

{cout<<\ }

void disp() const {cout<<\ }

。 【 ) 3.17】 private: int x,y; }; int main()

{const Sample a(1,2); a.disp(); return 0; }

A. disp1 B.disp2 C. disp1 disp2 D. 以下程序的运行结果是( B )。 #include

using namespace std; class R{ public: R(int r1,int r2) {R1=r1; R2=r2; }

void print(); void print() const; private: int R1,R2; };

void R::print()

{cout<

void R::print() const {cout<

程序编译出错

【 { R a(6,8); const R b(56,88); b.print(); return 0; }

A. 6,8 B. 56,88 C. 0,0 D. 8,6 【3.18】 写出下面程序的运行结果。 #include

using namespace std; class toy { public: toy(int q,int p) {quan=q; price=p; }

int get_quan() {return quan; }

int get_price() { return price; } private: int quan,price; }; int main() { toy op[3][2]={

toy(10,20),toy(30,48), toy(50,68),toy(70,80), toy(90,16),toy(11,120), };

public:

book(int a,int b); void printTotal(); private:

int qu, price; };

book::book(int a, int b) {

qu = a; price = b; }

void book::printTotal() {

cout<

int main() {

book bs[5]={book(1,10), book(2,20), book(3,30), book(4,40),

book(5,50)};

for(int i=0;i<5;i++) bs[i].printTotal(); system(\ return 0; }

运行结果是:

【3.34】 修改习题3.33,通过对象指针访问对象数组,使程序以相反的顺序显

示每个对象数组元素的 qu*price值。 答:

#include using namespace std; class book{ public:

book(int a,int b); void printTotal(); private:

int qu, price; };

book::book(int a, int b) {

qu = a; price = b; }

void book::printTotal() {

cout<

int main() {

book ob[5]={book(1,10), book(2,20), book(3,30), book(4,40),

book(5,50)};

book *p; p=&ob[4];

for(int i=0;i<5;i++) {

p->printTotal(); p--; }

system(\return 0; }

运行结果是:

【3.35】 构建一个类 Stock,含字符数组stockcode[]及整型数据成员quan、

双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、p。当定义Stock的类对象时,将对象的第1个字符串参数赋给数据成员stockcode,第2和第3个参数分别赋给quan、price。未设置第2和第3个参数时,quan的值为1000,price的值为8.98。成员函数print没有形参,需使用this指针,显示对象数据成员的内容。假设类Stock第1个对象的三个参数分别为:“600001”,3000和5.67,第

2个对象的第1个数据成员的值是“600001”,第2和第3个数据成员的值取默认值。要求编写程序分别显示这两个对象数据成员的值。

答:

#include using namespace std; const int SIZE=100; class Stock{ public:

Stock(char na[], int q = 1000, double p = 8.98); void print(); private:

char stockcode[SIZE]; int quan; double price; };

Stock::Stock(char na[], int q, double p) {

strcpy(this->stockcode, na); this->quan = q; this->price = p; }

void Stock::print() {

cout<<\\

\

}

int main() {

Stock s1(\ s1.print(); s2.print();

system(\ return 0; }

运行结果是:

quan:

【3.36】 编写一个程序,已有若干学生的数据,包括学号、姓名、成绩,要求

输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。

答:

#include #include using namespace std; class student{ public:

student(int sno, string sname, double sresult); void print();

static int f_scount;

static double f_stotalresult; private: int f_sno;

string f_sname; double f_sresult; };

student::student(int sno, string sname, double sresult) {

this->f_sno = sno; this->f_sname = sname; this->f_sresult = sresult; student::f_scount++;

student::f_stotalresult += sresult; }

void student::print() {

cout<<\学号: \姓名: \

成绩: \

}

int student::f_scount = 0;

double student::f_stotalresult = 0; int main() {

student s1(201501, \85), s2(201502, \90.5) ,

s3(201502, \

s1.print(); s2.print(); s3.print();

cout<<\学生人数: \

cout<<\平均成绩: \/

student::f_scount<

system(\ return 0; }

运行结果是:

第四章:

【4.8】使用派中类的主要原因是( A )。 A.提高代码的可重用性 B.提高程序的运行效率 C.加强类的封装性 D.实现数据的隐藏

【4.9】 假设已经定义好了一个类student,现在要定义类derived,它是从

student私有派生的,定义类derived的正确写法是( C )。

A.clase derived::student private{?}; B.clase derived::student public{?}; C.clase derived::private student{?}; D.clase derived::public student{?};

【4.10】 在多继承构造函数定义中,几个基类构造函数用( C )分隔。 A. : B. ; C., D. :: 【4.11】 设置虚基类的目的是( B )。

A.简化程序 B. 消除二义性 C. 提高运行效率 D.减少目标代码 【4.12】 写出下面程序的运行结果。

#include using namespace std; class B1{ public: B1(int i) { b1=i;

cout<<\ }

void Print() { cout<

}; class B2{ public: B2(int i) {b2=i;

cout<<\ }

void Print() { cout<

class A:public B2,public B1{ public:

A(int i,int j,int l); void Print(); private: int a; };

A::A(int i,int j,int l):B1(i),B2(j) { a=l;

cout<<\}

void A::Print() { B1::Print();

B2::Print(); cout<

aa.Print(); return 0; }

答:运行结果是: Constructor B2. Constructor B1. Constructor A. 3 2 1

【4.13】 写出下面程序的运行结果。

#include using namespace std; class Main{ protected: char *mainfood; public:

Main(char *name) { mainfood=name; } }; class Sub{ protected: char *subfood; public:

Sub(char *name) { subfood=name; } };

class Menu:public Main,public Sub{ public:

Menu(char *m,char *s):Main(m),Sub(s) {}

void show(); };

void Menu::show()

{ cout<<\主食=\ cout<<\副食=\} int main()

{ Menu m(\ m.show(); return 0; }

答:运行结果是: 主食=bread 副食=steak

【4.14】 写出下面程序的运行结果。

#include using namespace std; class A{ private: int a; public: A() { a=0; } A(int i) { a=i; } void Print() { cout<

};

class B:public A{ private: int b1,b2; public: B()

{ b1=0;b2=0; } B(int i) { b1=i; b2=0; }

B(int i,int j,int k):A(i),b1(j),b2(k) {} void Print() { A::Print();

cout<

{ B ob1,ob2(1),ob3(3,6,9); ob1.Print(); ob2.Print(); ob3.Print(); return 0; }

答:运行结果是: 0,0,0 0,1,0 3,6,9

【4.15】 写出下面程序的运行结果。

#include using namespace std; class B1{ int b1; public: B1(int i) {b1=i;

cout<<\ }

void print() { cout<

cout<<\ }

void print() { cout<

cout<<\

} int getb3() { return b3; } };

class A:public B2,public B1{ int a; B3 bb; public:

A(int i,int j,int k,int l):B1(i),B2(j),bb(k) {a=l;

cout<<\ }

void print() {B1::print(); B2::print();

cout<

答:运行结果是: constructor B2.2 constructor B1.1 constructor B3.3 constructor A.4 1 2 4,3

【4.16】 写出下面程序的运行结果。

#include using namespace std; class A{ public: A(int i,int j) {x=i; y=j; } int sum() { return x+y; } private: int x,y; };

class B:public A{ public:

B(int i,int j,int k,int l); int sum() { return w+h; } private: int w,h; };

B::B(int i,int j,int k,int l):A(i,j) { w=k; h=l; }

void f(A& s)

{ cout<

} int main() { B ob(1,3,5,7); f(ob); return 0; }

答:运行结果是: 4

【4.17】 写出下面程序的运行结果。

#include using namespace std; class A{ int a,b; public: A(int i,int j) {a=i;b=j; }

void Move(int x,int y) { a+=x; b+=y; }

void Show()

{ cout<<\ } };

class B:private A{ int x,y; public:

B(int i,int j,int k,int l):A(i,j) { x=k;y=l; }

void Show()

{ cout<

答:运行结果是: (1,2) 5,6 (6,9)

【4.18】 写出下面程序的运行结果。

#include using namespace std; class base1{ public: base1()

{ cout<<\

} };

class base2{ public: base2()

{ cout<<\ } };

class level1:public base2,virtual public base1{ public: level1()

{cout<<\ } };

class level2:public base2,virtual public base1{ public: level2()

{ cout<<\ } };

class toplevel:public level1,virtual public level2{ public: toplevel()

{ cout<<\ } }; int main() { toplevel obj; return 0; }

答:运行结果是: class base1 class base2 class level2 class base2 class level1 class toplevel

【4.21】 已有类Time和Date,要求设计一个派生类Birthtime,它继承类Time

和Date,并且增加一个数据成员Childname用于表示小孩的名字,同时设计主程序显示一个小孩的出生时间和名字。 class Time{ public:

Time(int h,int m,int s) { hours=h; minutes=m; seconds=s; }

void display()

{ cout<<\出生时间:\时\分\秒\ } protected:

int hours,minutes,seconds; }; class Date{ public:

Date(int m,int d,int y) { month=m; day=d; year=y; }

void display()

{ cout<<\出生年月:\年\月\日\ } protected:

int month,day,year; };

答:

#include #include using namespace std;

class Birthtime:public Date,public Time{ public:

Birthtime(int m,int d,int y, int h,int n,int name):Date(m,d,y),Time(h,n,s)

{

Childname=name; }

void display() {

Date::display(); Time::display();

cout<<\ }

protected:

string Childname; };

int main() {

Birthtime bt(6,22,2008,10,38,56, \ bt.display(); system(\ return 0; }

运行结果是:

s,string

【4.22】 编写一个学生和教师数据输人和显示程序,学生数据有编号、姓名、

班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。

答:

#include #include using namespace std; class person{ public:

void input() {

cout<<\请输入编号:\ cin>>no;

cout<<\请输入姓名:\ cin>>name; }

void display() {

cout<<\编号:\,姓名:\}

protected: int no;

string name; };

class student:public person{ public:

void input() {

person::input();

cout<<\请输入班号:\ cin>>classno;

cout<<\请输入成绩:\ cin>>result; }

void display() {

person::display();

cout<<\,班号:\,成绩:\}

protected: int classno; double result; };

class teacher:public person{ public:

void input() {

person::input();

cout<<\请输入职称:\ cin>>title;

cout<<\请输入部门:\ cin>>department; }

void display() {

person::display();

cout<<\,职称:\,部门:\}

protected: string title;

string department; };

int main() {

student s; teacher t;

cout<<\请输入学生信息!\ s.input();

cout<

cout<

cout<

运行结果是:

本文来源:https://www.bwwdw.com/article/kehx.html

Top