文档详情

c 练习题(C exercises).doc

发布:2018-06-01约1.52万字共55页下载文档
文本预览下载声明
c++练习题(C++ exercises) =============================================================================== 6分析以下程序的执行结果 #包括 iostream。” 类样本 { int n; 公共: sample() { } 样本(int) 样本和运算符=(示例); 无效disp() { cout “=” N endl;} }; 样品和样品::操作员=(样本S) { 示例::N =钠; 返回*; } 无效main() { 样本S1(10),S2; S2 = S1; disp() S2; } 7分析以下程序的执行结果 #包括 iostream。” 类样本 { int n; 公共: 样本(int) 运算符+(){ + +;} 运算符+(int)2;};} 无效disp() { cout “=” endl n; } }; 无效main() { 样本A(2),B(2); ++; ++; A. disp(); B. disp(); } 8分析以下程序的执行结果 #包括 iostream。” 类样本 { int [ 10 ] [ 10 ]; 公共: int和operator()(int,int); }; int和样品::operator()(x,y) { 返回一个[ y ]; } 无效main() { 样品; int,j; 对于(i = 0;i 10;i +) 对于(j=0;j<10;j + +) a(i,j)= i + j; 对于(i = 0;i 10;i +) cout 一(I,1)”; cout endl; } 九 设计一个点类点,实现点对象之间的各种运算。 解: 6点类提供了个运算符重载函数(参考声明,写函数体),以实现相应的运算。 10分析以下程序的执行结果 #包括 iostream。” 类样本 { int n; 公共: sample() { } 样本(int) 朋友样本操作员-(样本和样本); 好友样本运算符+(示例和示例); 无效disp() { cout “=” N endl;} }; 样品操作员-(样品和S1,样品和S2) { int m = S1。n-s2。n; 返回样本(m); } 样本运算符+(样本和S1,样本和S2) { 整数m = S1; 返回样本(m); } 无效main() { 样本S1(10),S2(20),S3; S3 = S2-S1; disp() S3; S3 = S2 + S1; disp() S3; } 十一 设计一个三角形类三角形,包含三角形三条边长的私有数据成员,另有一个重载运算符“+”,以实现求两个三角形对象的面积???和。 解: 在三角类中设计一个友元函数操作符+(三角T1,T2,它重载运算符三角形)“+”,返回T1 T2两个三角形的面积之和和。 十二 题11的重载运算符”+“友元函数只能返回两个三角形的面积之和,不能计算三个三角形的面积之和,改进一下,使之能计算任意多个三角形的面积之和。 解: 题11的重载运算符为什么不能计算3个三角形的面积之和呢?对于式子:S = T1、T2和T3,先计算T1、T2、T3返回一个双数然后再进行该双数+的计算,显然没有这样的重载运算符”+“友元函数,只需要再添加一个友元函数朋友双操作符+(双D,三角形T)即可。 十三 设计一个学生类学生,包括姓名和三门课程成绩, The overloaded operator + the sum of scores for all students on an object, then the average of the object for each course. Fourteen Please reload the operator, make the following statement to run: Coutd; //d is the object of the Data class Class Data { Int a, b; Char *c; Public: Data (int x, int y, char z[]); (~Data); }; Fifteen The commonly used String Operator Manual Sixteen Please write a smart pointer of the person class, and override in the smart pointer (show); realize person heap memory release. Class person { Public: Person (char* s); Void
显示全部
相似文档