{ 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 #include 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 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 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 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