行业知识
第十五单元日考技能
2026-11-24 19:46:00
来源:互联网转载
文章目录
- 第十五单元
- 1.编写一个基于对象的程序,求长方体的体积,要求:
- 2.编写strcpy函数(10分)
- 3.建���一个类String,连接两个字符串后。具体要求:
第十五单元
1.编写一个基于对象的程序,求长方体的体积,要求:
① 定义头文件
()② 定义一个长方体类Box,
③ 类内有私有数据成员lengh(长),width(宽),height(高)
()④ 在类内公有成员函数get_value(),volume()。
⑤ get_value()函数在类外定义,get_value()作用是从键盘输入长,宽,高的值。
⑥ volume()函数在类外定义,volume()的作用是计算长方体的体积并在屏幕上显示。
⑦ 在main()函数定义Box类的对象box1
⑧ 调用get_value()函数给长、宽、高赋值
⑨ 调用volume()函数输出长方体体积。
⑩ 按照题目要求写好注释。
class Box{private: int lengh; int width; int height;public: void get_value(); //没有函数体,只有函数定义 void volume();};void Box::get_value(){ cout lengh >> width >> height;}void Box::volume(){ cout Box box; //创建对象,因为没有带参构造所以不用加() box.get_value(); //调用函数给属性赋值 box.volume(); //调用函数计算体积} for(int i = 0;i *arr1 = *arr2; //把arr2的数据给arr1 arr2++; arr1++; }}int main(){ char arr1[2]; //空 char arr2[2] = {'a','b'}; strcpy(arr1,arr2); //字符复制功能 //打印看结果 for(int i= 0;i cout int len; char *s;public: String(int len,char *s){ this-len = len; this-s=s; } ~String(){ cout char *temp= new char[len+a.len]; //定义空数组 strcpy(temp,s); //先把abc放到数组中 strcat(temp,a.s); //再把bcd追加进去 cout cout str1+str2; }};int main(){ String a(3,"abc"); String b(3,"bcd"); a.show(b,a);} //答案是abcbcd