《c++编程题题库》.doc

  1. 1、本文档共21页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
1.1 编写一个基于对象的程序,要求: (1)定义一个时间类Time,类内有私有数据成员hour(小时)、minute(分钟)、sec(秒),公有成员函数set_time()、show_time()。 (2)set_time()函数和show_time()函数在类内定义。set_time()作用是从键盘输入时间、分钟、秒的值,show_time()的作用是在屏幕上显示时间、分钟、秒的值。 (3)在main()函数定义Time类的对象t1,并调用set_time()函数给时间赋值,调用show_time()函数输出时间的值。 #include <iostream> using namespace std; class Time {public: void set_time() {cin>>hour; cin>>minute; cin>>sec; } void show_time() {cout<<hour<<":"<<minute<<":"<<sec<<endl;} private: int hour; int minute; int sec; }; int main() { Time t1; t1.set_time(); t1.show_time(); return 0; } 1.2 编写一个基于对象的程序,求长方体的体积,要求: (1)定义一个长方体类Box,类内有私有数据成员lengh(长)、width(宽)、height(高),公有成员函数get_value()、volume()。 (2)get_value()函数和volume()函数在类外定义。get_value()作用是从键盘输入长、宽、高的值,volume()的作用是计算长方体的体积并在屏幕上显示。 (3)在main()函数定义Box类的对象box1,并调用get_value()函数给长、宽、高赋值,调用volume()函数输出长方体体积。 #include <iostream> using namespace std; class Box {public: void get_value(); void volume(); private: float lengh; float width; float height; }; void Box::get_value() { cout<<"please input lengh, width,height:"; cin>>lengh; cin>>width; cin>>height; } void Box::volume() { cout<<"volmue of box1 is "<<lengh*width*height<<endl;} int main() {Box box1; box1.get_value(); box1.volume(); return 0; } 1.3. 编写一个基于对象的程序,求一个有十个数据的整型数组中元素的最大值,要求: (1)定义一个类Array_max,类内有私有数据成员array[10]、max分别存储十个整数、最大值,公有成员函数set_value()、max_volume()。 (2)set_value()函数和max_volume()函数在类外定义。get_value()作用是从键盘输入数组十个元素的值,max_volume()的作用是求出并显示数组元素的最大值。 (3)在main()函数定义Array_max类的对象arrmax,并调用set_value()函数给数组赋值,调用max_volume()函数求出并显示数组元素的最大值。 #include <iostream> using namespace std; class Array_max {public: void set_value(); void max_value(); private: int array[10]; int max; }; void Array_max::set_value() { int i; for (i=0;i<10;i++) cin>>array[i]; } void Array_max::max_value() {int i; max=array[0]; for (i=1;i<10;i++) if(array[i]>max) max=array[i]; cout<<"max="<<max; } int main() {Array_

文档评论(0)

OnlyOne + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档