资源描述
浙江大学远程教育学院
《面向对象程序设计》课程作业
姓名:
学 号:
年级:
2013(春)计算机科学与技术(专本(业余))
学习中心:
南京学习中心
—————————————————————————————
第2章
【2.3】 测试下面的注释(它在C++风格的单行注释中套入了类似于C的注释)是否有效。
//this is a strange /*way to do a comment*/
答:此注释有效,单行注释中可以嵌套 / * „„ * /方式的注释。
【2.4】 以下这个简短的C++程序不可能编译通过,为什么?
#include<iostream>
using namespace std;
int main()
{int a,b,c;
cout<<"Enter two numbers:";
cin>>a>>b;
c=sum(a,b);
cout<<"sum is:"<<c;
return 0;
}
sum(int a,int b)
{ return a+b;
}
答:不可能通过编译.在using namespace std;后面加上sum(int a,int b)
【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 * );
答:(1)这两个函数原型是等价的,函数原型中的参数名可以缺省。 (2)这两个函数的第1行是不等价的,因为这个函数的第1行中必须包含参数名。
【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="chen";”下面的语句中正确的是( D )。
A. name[3]='a'; B. name="lin";
C. name=new char[5]; D. cout<<name[3];
【2.8】 假设已经有定义“char * const name="chen";”下面的语句中正确的是( A )。
A. name[3]='q'; B. name="lin";
C. name=new char[5]; D. name=new char('q');
【2.9】 假设已经有定义“const char * name="chen";”下面的语句中错误的是( A )。
A. name[3]='q'; B. name="lin";
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<iostream>
using namespace std;
int i=15;
int main()
{ int i;
i=100;
::i=i+1;
cout<<::i<<endl;
return 0;
}
答:101
【2.19】 写出下列程序的运行结果。
#include<iostream>
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<<a<<" "<<b<<endl;
return 0;
}
答:10,10
【2.20】 分析下面程序的输出结果。
#include<iostream>
using namespace std;
int &f(int &i)
{ i+=10;
return i;
}
int main()
{ int k=0;
int &m=f(k);
cout<<k<<endl;
m=20;
cout<<k<<endl;
return 0;
}
答:10
20
【2.21】 举例说明可以使用const替代#define以消除#define的不安全性。
答:例如:#include <iostream.h>
#define A 2+4
#define B A*3
void main()
{ cout<<B<<endl; }
上面程序的运行结果是14而不是18,但很容易被认为是18。用const替代#define就能得到正确结果,从而消除了#define的不安全性。
#include <iostream.h>
const A=2+4;
const B=A*3;
void main()
{ cout<<B<<endl; }
运行结果为18。
【2.22】 编写一个C++风格的程序,用动态分配空间的方法计算Fibonacci 数列的前20项,并存储到动态分配的空间中。
答:#include<iostream>
using namespace std;
int main()
{
int * p=new int[20];
*p=1;
*(p+1)=1;
cout<<*p<<”\t<<*(p+1)<<”\t”;
p=p+2;
for(int i=3;i<=20;i++)
{
*p=*(p-1)+*p(p-2);
Cout<<*p<<”\t”;
If(i%5==0)cout<<endl;
P++;
【2.23】 编写一个C++风格的程序,建立一个被称为sroot()的函数,返回其参数的二次方根。重载sroot()3次,让它返回整数、长整数与双精度数的二次方根(计算二次方根时,可以使用标准库函数sqrt())。
答:。#include<iostream>
#include<cmath>
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=12;
Long l=1234;
Double d=12.34;
Cout<<”i的二次方根是:”<<sroot(i)<<endl;
Cout<<”l的二次方根是:”<<sroot(l)<<endl;
Cout<<”d的二次方根是:”<<sroot(d)<<endl;
Return 0;
}
【2.24】 编写一个C++风格的程序,解决百钱问题:将一元人民币兑换成1、2、5分的硬币,有多少种换法?
答:#include<iostream>
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<<”\t”<<j<<”\t”<<i<<endl;
}
Cout<<”sum is “<<sum<<endl;
Return 0;
}
【2.25】 编写一个C++风格的程序,输入两个整数,将它们按由小到大的顺序输出。要求使用变量的引用。
答:#include<iostream>
Using namespace std;
Int main()
{ void change(int &,int &);
Int a,b;
Cin>>a>>b;
If(a>b)change(a,b);
Cout<<a<<” “<<b<<endl;
Return 0;
}
Void change(int &a1,int &b1)
{int temp;
Temp=a1;
A1=b1;
B1=temp;}
第三章:
【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<iostream>
using namespace std;
class B{
public:
B(){}
B(int i,int j)
{ x=i;
y=j;
}
void printb()
{cout<<x<<","<<y<<endl;
}
private:
int x,y;
};
class A{
public:
A()
{}
A(int I,int j);
void printa();
private:
B c;
};
A::A(int i,int j):c(i,j)
{}
void A::printa()
{c.printb();
}
int main()
{ A a(7,8);
a.printa();
return 0;
}
A.8,9 B. 7,8 C. 5,6 D. 9,10
【3.15】 以下程序的运行结果是( A)
#include<iostream>
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);
}
int get_area()
{return length*width;
}
private:
int length,width;
A label;
};
int main()
{ box b;
b.set(4,6,1,20);
cout<<b.get_area()<<endl;
return 0;
}
A.24 B. 4 C. 20 D. 6
【3.16】 以下程序的运行结果是( B )。
#include<iostream>
using namespace std;
class Sample{
public:
Sample(int i,int j)
{ x=i;
y=j;
}
void disp()
{cout<<"disp1"<<endl;
}
void disp() const
{cout<<"disp2"<<endl;
}
private:
int x,y;
};
int main()
{const Sample a(1,2);
a.disp();
return 0;
}
A. disp1 B.disp2 C. disp1 disp2 D. 程序编译出错
【3.17】 以下程序的运行结果是( B )。
#include<iostream>
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<<R1<<","<<R2<<endl;
}
void R::print() const
{cout<<R1<<","<<R2<<endl;
}
int main()
{ 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<iostream>
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),
};
for(int i=0;i<3;i++)
{cout<<op[i][0].get_quan()<<",";
cout<<op[i][0].get_price()<<"\n";
cout<<op[i][1].get_quan()<<",";
cout<<op[i][1].get_price()<<"\n";
}
cout<<endl;
return 0;
}
答:10,20
30,48
50,68
70,80
90,16
11,120
【3.19】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class example
{ public:
example(int n)
{ i=n;
cout<<"Constructing\n";
}
~example()
{ cout<<"Destructing\n";
}
int get_i()
{ return i;
}
private:
int i;
};
int sqr_it(example o)
{return o.get_i()*o.get_i();
}
int main()
{ example x(10);
cout<<x.get_i()<<endl;
cout<<sqr_it(x)<<endl;
return 0;
}
答:Constructing
10
Destructing
100
Destructing
【3.20】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class aClass
{ public:
aClass()
{total++;
}
~aClass()
{total--;
}
int gettotal()
{ return total;
}
private:
static int total;
};
int aClass::total=0;
int main()
{aClass o1,o2,o3;
cout<<o1.gettotal()<<"objects in existence\n";
aClass *p;
p=new aClass;
if(!p)
{ cout<<"Allocation error\n";
return 1;
}
cout<<o1.gettotal();
cout<<"objects in existence after allocation\n";
delete p;
cout<<o1.gettotal();
cout<<"objects in existence after deletion\n";
return 0;
}
答:
3 objects in existence
4 objects in existence after allocation
3 objects in existence after deletion。
【3.21】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class test
{ public:
test();
~test(){};
private:
int i;
};
test::test()
{ i=25;
for(int ctr=0;ctr<10;ctr++)
{ cout<<"Counting at"<<ctr<<"\n";
}
}
test anObject;
int main()
{ return 0;
}
答:
Counting at 0
Counting at 1
Counting at 2
Counting at 3
Counting at 4
Counting at 5
Counting at 6
Counting at 7
Counting at 8
Counting at 9
【3.22】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class A{
int a,b;
public:
A()
{a=0;
b=0;
cout<<"Default constructor called.\n";
}
A(int i,int j)
{a=i;
b=j;
cout<<"Constructor: a="<<a<<",b="<<b<<endl;
}
};
int main()
{A a[3];
A b[3]={A(1,2),A(3,4),A(5,6)};
return 0;
}
答:
Default constructor called.
Default constructor called.
Default constructor called.
Constructor:a=1,b=2
Constructor:a=3,b=4
Constructor:a=5,b=6
【3.23】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class Test{
private:
int val;
public:
Test()
{ cout<<"default."<<endl;
}
Test(int n)
{ val=n;
cout<<"Con."<<endl;
}
Test(const Test& t)
{val=t.val;
cout<<"Copy con."<<endl;
}
};
int main()
{Test t1(6);
Test t2=t1;
Test t3;
t3=t1;
return 0;
}
答: Con.
Copy con.
Default.
【3.24】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class N{
private:
int A;
static int B;
public:
N(int a)
{A=a;
B+=a;
}
static void f1(N m);
};
void N::f1(N m)
{cout<<"A="<<m.A<<endl;
cout<<"B="<<B<<endl;
}
int N::B=0;
int main()
{ N P(5),Q(9);
N::f1(P);
N::f1(Q);
return 0;
}
答:
A=5
B=14
A=9
B=14
【3.25】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class M{
int x,y;
public:
M()
{ x=y=0;
}
M(int i,int j)
{x=i;
y=j;
}
void copy(M*m);
void setxy(int i,int j)
{x=i;
y=j;
}
void print()
{cout<<x<<","<<y<<endl;
}
};
void M::copy(M*m)
{x=m->x;
y=m->y;
}
void fun(M m1,M*m2)
{m1.setxy(12,15);
m2->setxy(22,25);
}
int main()
{M p(5,7),q;
q.copy(&p);
fun(p,&q);
p.print();
q.print();
return 0;
}
答:5,7
22,25
【3.26】 写出下面程序的运行结果。
#include<iostream>
using namespace std;
class M{
int A;
static int B;
public:
M(int a)
{A=a;
B+=a;
cout<<"Constructing"<<endl;
}
static void f1(M m);
~M()
{cout<<"Destructing \n";
}
};
void M::f1(M m)
{cout<<"A="<<m.A<<endl;
cout<<"B="<<B<<endl;
}
int M::B=0;
int main()
{M p(5),Q(10);
M::f1(p);
M::f1(Q);
return 0;
}
答:Constructing
Constructing
A=5
B=15
Destructing
A=10
B=15
Destructing
Destructing
Destructing。
【3.27】 指出下列程序中的错误,并说明为什么。
#include<iostream>
using namespace std;
class Student{
public:
void printStu();
private:
char name[10];
int age;
float aver;
};
int main()
{ Student p1,p2,p3;
p1.age=30;
.
.
.
return 0;
}
答:语句“pl.age=30;”编译时出现错误,因为age是私有数据成员,不能直接访问。
【3.28】 指出下列程序中的错误,并说明为什么。
#include<iostream>
using namespace std;
class Student{
int sno;
int age;
void printStu();
void setSno(int d);
};
void printStu();
{ cout<<"\nSno is"<<sno<<",";
cout<<"age is"<<age<<"."<<endl;
}
void setSno(int s)
{ sno=s;
}
void setAge(int a)
{ age=a;
}
int main()
{ Student lin;
lin.setSno(20021);
lin.setAge(20);
lin.printStu();
}
答:这个函数是不能访问到类student中的age属性的 age不是类的成员 也不能通过lin.setage() 调用
【3.29】 指出下列程序中的错误,并说明为什么。
#include<iostream>
using namespace std;
class Point{
public:
int x,y;
private:
Piont()
{x=1;y=2;
}
};
int main()
{Point cpoint;
cpoint.x=2;
return 0;
}
答:构造函数不能是private属性的 必须是public的 要不然构造函数 访问不到 就不能进行申明这个实例
【3.30】 下面是一个计算器类的定义,请完成该类成员函数的实现。
class counter{
public:
counter(int number);
void increment(); //给原值加1
void decrement(); //给原值减1
int getvalue(); //取得计数器值
int print(); //显示计数
private:
int value;
};
答:
Class counter{
Public:
Counter(int number);
Void increment();
Void decrement();
Int getvalue();
Int print();
Private:
Int value;
};
Counter::counter(int number){value=number;}
Void counter::increment(){value++;}
Void counter::decrement(){value--;}
Int counter::getvalue(){return value;}
Int counter::print()
{
Cout<<”value is “<<value<<endl;{
Return 0;
}
【3.31】 根据注释语句的提示,实现类Date的成员函数。
#include<iostream>
using namespace std;
class Date{
public:
void printDate(); //显示日期
void setDay(int d); //设置日的值
void setMonth(int m); //设置月的值
void setYear(int y); //设置年的值
private:
int day,month,year;
};
int main()
{ Date testDay;
testDay.setDay(5);
testDay.setMonth(10);
testDay.setYear(2003);
testDay.printDate();
return 0;
}
答:Void Date::printDate()
{
Cout<<”\nDate is”<<day<<”.”;
Cout<<month<<”.”<<year<<endl;
}
Void Date::setDay(int d){day=
展开阅读全文