《C++语言与面向对象的设计》习题及参考解答(8)

2020-06-05 09:12

{ Building::print(); cout<<\ cout<<\ } };

void main() {

Building build1(18,360,8000); build1.print();

House house(6,24,2880,4,2); house.print();

Office office(18,360,8000,4,4); office.print(); }

程序执行输出结果如下:base constructor called. this is a 18-story building 习题9 参考答案

there are 360 houses And total area is 8000 base constructor called. House constructer called. this is a 6-story building there are 24 houses And total area is 2880 bedroomnum=4 bathroomnum=2

base constructor called. Office Constructor called. this is a 18-story building there are 360 houses And total area is 8000 stuffnum=4 telnum=4

9.1 简要回答下列问题:(1)什么是多态性?什么是重载?编译系统是如何实现重载的? (2)什么是函数重载?编译系统如何区分同名函数的不同版本?

(3)若基类中定义了成员函数f1(),其公有派生类中是否一定要重定义f1()?派生类重定义的基类成员函数是否由参数区分?

(4)为什么友元运算符重载不能使用缺省参数? 答:(1) 多态性指程序对同一名字或符号在不同情况下做出不同解释的能力。比如两次调用一个同名函数时却执行的是不同的代码。将程序名与其对应的代码块联系起来的行为称为绑定。编译时完成的绑定称为静态绑定,程序运行期间完成的绑定称为动态绑定。编译系统通过静态绑定实现多态性。

(2)程序编译时实现的多态性属于静态的多态性,静态的多态性又叫重载(overload),编译时如果遇到名称相同但参数不同的函数,系统把它们看做不同的函数分别联系到不同的执行代码块的首地址。程序运行时根据同名函数调用时的实参不同而调用不同的代码。

(3)派生类中可出出重定义(override)也可出不重定义基类中已有定义的成员函数,派生类重定义的基类成员函数时,应具有和基类中重定义函数相同的函数原型,而不是由参数区分。

(4) 因为友元运算符(函数)不是类的成员,所以,函数的参数表中没有隐含的this指针,所以,友元运算符重载没有缺省参数可以使用。

9.2 指出下面一组函数原型中,哪些是同一函数的重载:

(1) int function(int double); (2)double function(int a, int b=0); (3)void function( int x, int y); (4)int function( double, int); (5)float function( ); (6)float func(float, float); 答:出下4个函数都是函数function的重载:(1)int function(int double);(3)void function( int x, int y);(4)int function( double, int);(5)float function( );

31

9.3 下面定义的sub( )函数是不是重载函数?为什么? int sub (int x, int y) { return (x*x –y*y ); }

int sub (int p, int q) { return (p+q)*(p-q); }

答:形参名不同不能区分重载函数, 两个sub ( ) 函数不是正确的函数重载。 9. 4 分析下列程序的运行结果:(1) #include class character { char i; public: character (char a=0){i =a; } character operator ++(); character operator ++(int); void print(){cout<

#include class Matrix { double *ptr; int row,col; public: Matrix(int r,int c) { row=r; col=c; ptr=new double[row*col]; } double& operator()(int i, int j) {

j.i=i++; return j; } void main() { character x(65), y(98), z; z = ++x; x.print(); z.print(); z = y++; y.print(); z.print(); } 程序运行结果:B B c b return ptr[i*col+j]; } }; void main() { Matrix m(3,5); double t=1024; for(int i=0;i<3;i++) for(int j=0;j<5;j++) { m(i,j)=t; t=t/2; } for( i=0;i<3;i++)

32

{ cout<

1 0.5 0.25 0.125 0.0625 (3)

#include ~Test() class Test { { cout<<\ static int count; } int id; Test& operator= (const Test& t) public: { Test() cout<<\\assigned from { \ id=++count; return *this; cout<<\\default } constructor\}; } int Test::count=0; Test(const Test& t) void main( ) { { id=++count; Test s1,s2; cout<<\\copied from Test t(s1); \ s1=s2; } }

程序输出为:Test 1 default constructor Test 2 default constructor Test 3 copied from 1 Test 1 assigned from 2 Test 3 destroyed Test 2 destroyed Test 1 destroyed

9.5 上机实验题: 根据下列Time类的声明,给出三个运算符重载函数的实现定义,并编写测试程序验证其正确性。

#include Time operator++(); class Time Time operator++(int); { Time operator+(int second); int hh, mm, ss; void public: print(){cout<

Time Time::operator++() return *this; { } ss++; Time Time::operator++(int) if (ss>59){ mm++; ss=0; } { if (mm>59){ hh++; mm=0; } Time temp=*this; if (hh>23) hh=0; ++*this;

33

return temp; } } if (hh >23) hh = hh$; Time Time::operator+(int second) } { return *this; if (second<0) } { void main() cout<<\{ return *this; Time t1,t2; } t1+100; else ++t1; { t2=t1++; ss=ss+second; cout<<\ if (ss>59){ cout<<\ mm = mm + ss/60; t1+3610; ss = ss`; cout<<\ } ++t1; if (mm >59){ cout<<\ hh = hh + mm /60; } mm = mm`;

9.6 上机实验题:按已给出的函数原型为Matrix类重载 +、-、* 运算符,以完成正确的矩阵加法、减法、乘法计算,并编写main()函数,用给定的数据进行测试。 #include { class Matrix for(int i=0;i

34

参考程序:

Matrix Matrix::operator+(Matrix m) {

{

Matrix a(2,3),b(2,3),c(3,2), d(2,3), e(2,3),f(2,2); Matrix temp(row,col); a.set(0,0,3.0); for(int i=0;i

a.set(1,2,4.0); Matrix Matrix::operator-(Matrix m) b.set(0,0,11.0); {

b.set(0,1,-2.0); Matrix temp(row,col); b.set(0,2,-3.0); for(int i=0;i

c.set(0,1,2.0); Matrix Matrix::operator*(Matrix m) c.set(1,0,3.0); {

c.set(1,1,4.0); Matrix temp(row,m.col); c.set(2,0,5.0); for(int i=0;i

c.print(); for(int k=0;k

d.print(); temp.set(i,j,temp(i,j)+(*this)(i,k)*m(k,j)); e=a-b; } e.print(); return temp; f=a*c; }

f.print(); void main() }

习题10参考答案

10. 1 单项选择题:(1) 以下保留字( )不能出现在说明虚函数原型的语句中。A.static B. operator C. void D. const .

(2) 以下关于虚函数和纯虚函数的说法中,( )是不正确的 A.在派生类中虚函数可以不用关键字virtual说明。 B. 虚函数在派生类中可以不重定义。 C. 不能为虚函数定义缺省操作。

D. 纯虚函数在派生类中可以不重定义。

35


《C++语言与面向对象的设计》习题及参考解答(8).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:回复买家好评模板

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: