当前位置:首页 > C语言程序设计(郑莉)课后习题答案
const int MaxCats = 5;
Cat *CatHouse[MaxCats]; int i; for (i = 0; i CatHouse[i] = new Cat(i); TelepathicFunction(); } for ( i = 0; i delete CatHouse[i]; TelepathicFunction(); } return 0; } void TelepathicFunction() { cout << \are \<< Cat::GetHowMany() << \cats alive!\\n\} 程序运行输出: There are 1 cats alive! There are 2 cats alive! There are 3 cats alive! There are 4 cats alive! There are 5 cats alive! There are 4 cats alive! There are 3 cats alive! There are 2 cats alive! There are 1 cats alive! There are 0 cats alive! 5-8 什么叫做友元函数?什么叫做友元类? 解: 友元函数是使用friend关键字声明的函数,它可以访问相应类的保护成员和私有成员。友元类是使用friend关键字声明的类,它的所有成员函数都是相应类的友元函数。 5-9 如果类A是类B的友元,类B是类C的友元,类D是类A的派生类,那么类B是类A的友元吗?类C是类A的友元吗?类D是类B的友元吗? 解: 类B不是类A的友元,友元关系不具有交换性; 类C不是类A的友元,友元关系不具有传递性; 类D不是类B的友元,友元关系不能被继承。 5-10 静态成员变量可以为私有的吗?声明一个私有的静态整型成员变量。 解: 可以,例如: private: static int a; 5-11 在一个文件中定义一个全局变量n,主函数main(),在另一个文件中定义函数fn1(),在main()中对n赋值,再调用fn1(),在fn1()中也对n赋值,显示n最后的值。 解: #include void main() { n = 20; fn1(); cout << \的值为\} // fn1.h文件 extern int n; void fn1() { n=30; } 程序运行输出: n的值为30 21 5-12 在函数fn1()中定义一个静态变量n,fn1()中对n的值加1,在主函数中,调用fn1()十次,显示n的值。 解: #include void fn1() { static int n = 0; n++; cout << \的值为\} void main() { for(int i = 0; i < 10; i++) fn1(); } 程序运行输出: n的值为1 n的值为2 n的值为3 n的值为4 n的值为5 n的值为6 n的值为7 n的值为8 n的值为9 n的值为10 5-13 定义类X、Y、Z,函数h(X*),满足:类X有私有成员i,Y的成员函数g(X*)是X的友元函数,实现对X的成员i加1,类Z是类X的友元类,其成员函数f(X*)实现对X的成员i加5,函数h(X*)是X的友元函数,实现对X的成员i加10。在一个文件中定义和实现类,在另一个文件中实现main()函数。 解: #include \void main() { X x; Z z; z.f(&x); } // my_x_y_z.h文件 #ifndef MY_X_Y_Z_H class X; class Y { void g(X*); }; class X { private: int i; public: X(){i=0;} friend void h(X*); friend void Y::g(X*); friend class Z; }; void h(X* x) { x->i =+10; } void Y::g(X* x) { x->i ++; } class Z { public: void f(X* x) { x->i += 5; } }; #endif // MY_X_Y_Z_H 程序运行输出:无 5-14 定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数totalWeight(),计算二者的重量和。 解: 源程序: #include class Boat; class Car { private: int weight; public: 22 Car(int j){weight = j;} friend int totalWeight(Car &aCar, Boat &aBoat); }; class Boat { private: int weight; public: Boat(int j){weight = j;} friend int totalWeight(Car &aCar, Boat &aBoat); }; int totalWeight(Car &aCar, Boat &aBoat) { return aCar.weight + aBoat.weight; } void main() { Car c1(4); Boat b1(5); cout << totalWeight(c1, b1) << endl; } 程序运行输出: 9 5-15 如果在类模板的定义中有一个静态数据成员,则在程序运行中会产生多少个相应的静态变量? 解: 这个类模板的每一个实例类都会产生一个相应的静态变量。 第 六 章 数组、指针与字符串 6-1 数组A[10][5][15]一共有多少个元素? 解: 10×5×15 = 750 个元素 1-2 在数组A[20]中第一个元素和最后一个元素是哪一个? 解: 第一个元素是A[0],最后一个元素是A[19]。 6-3 用一条语句定义一个有五个元素的整型数组,并依次赋予1~5的初值。 解: 源程序: int IntegerArray[5] = { 1, 2, 3, 4, 5 }; 或:int IntegerArray[] = { 1, 2, 3, 4, 5 }; 6-4 已知有一个数组名叫oneArray,用一条语句求出其元素的个数。 解: 源程序: nArrayLength = sizeof(oneArray) / sizeof(oneArray[0]); 6-5 用一条语句定义一个有5×3个元素的二维整型数组,并依次赋予1~15的初值。 解: 源程序: int theArray[5][3] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }; 或:int theArray[5][3] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12},{13,14,15} }; 6-6 运算符*和&的作用是什么? 解: *称为指针运算符,是一个一元操作符,表示指针所指向的对象的值;&称为取地址运算符,也是一个一元操作符,是用来得到一个对象的地址。 6-7 什么叫做指针?指针中储存的地址和这个地址中的值有何区别? 解: 指针是一种数据类型,具有指针类型的变量称为指针变量。指 23 针变量存放的是另外一个对象的地址,这个地址中的值就是另一个对象的内容。 6-8 定义一个整型指针,用new语句为其分配包含10个整型元素的地址空间。 解: 源程序: int *pInteger = new int[10]; 6-9 在字符串”Hello,world!”中结束符是什么? 解: 是NULL字符。 6-10 定义一个有五个元素的整型数组,在程序中提示用户输入元素值,最后再在屏幕上显示出来。 解: 源程序: #include int main() { int myArray[5]; int i; for ( i=0; i<5; i++) { cout << \cin >> myArray[i]; } for (i = 0; i<5; i++) cout << i << \return 0; } 程序运行输出: Value for myArray[0]: 2 Value for myArray[1]: 5 Value for myArray[2]: 7 Value for myArray[3]: 8 Value for myArray[4]: 3 0: 2 1: 5 2: 7 3: 8 4: 3 6-11 引用和指针有何区别?何时只能使用指针而不能使用引用? 解: 引用是一个别名,不能为NULL值,不能被重新分配;指针是一个存放地址的变量。当需要对变量重新赋以另外的地址或赋值为NULL时只能使用指针。 6-12 声明下列指针:float类型变量的指针pFloat,char类型的指针pString和struct customer型的指针prec。 解: float *pfloat; char *pString; struct customer *prec; 6-13 给定float类型的指针fp,写出显示fp所指向的值的输出流语句。 解: cout << \ 6-14 程序中定义一个double类型变量的指针。分别显示指针占了多少字节和指针所指的变量占了多少字节。 解: double *counter; cout << \cout << '\\nSize of addressed value == \ 6-15 const int * p1 和 int * const p2的区别是什么? 24
共分享92篇相关文档