从今日开始每日更新两道编程题,同时掺杂基础知识的复习
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{
private :
int hour,minute,sec;
public:
double set_time(){
cin>>hour>>minute>>sec;
}
double show_time(){
cout<<hour<<":"<<minute<<":"<<sec;
}
};
int main(){
Time t1;
t1.set_time();
t1.show_time();
return 0;
}
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{
private :
int length,width,height;
public:
double get_value();
double volume();
};
double Box::get_value(){
cin>>length>>width>>height;
}
double Box::volume(){
double v;
v = length*width*height;
cout<<v;
}
int main(){
Box box1;
box1.get_value();
box1.volume();
return 0;
}
3.编写一个基于对象的程序,求一个有十个数据的整型数组中元素的最大值,要求:
(1)定义一个类Array_max,类内有私有数据成员array[10]、max分别存储十个整数、最大值,公有成员函数set_value()、max_value()。
(2)set_value()函数和max_value()函数在类外定义。set_value()作用是从键盘输入数组十个元素的值,max_value()的作用是求出并显示数组元素的最大值。
(3)在main()函数定义Array_max类的对象arrmax,并调用set_value()函数给数组赋值,调用max_value()函数求出并显示数组元素的最大值。
#include<iostream>
using namespace std;
class Array_max{
private :
int array[10];
int max;
public:
double set_value();
double max_value();
};
double Array_max::set_value(){
for(int i=0;i<10;i++)
cin>>array[i];
}
double Array_max::max_value(){
max = array[0];
for(int i=1;i<10;i++){
if (array[i]>max){
max = array[i];
}
}
cout<<max;
}
int main(){
Array_max arrmax;
arrmax.set_value();
arrmax.max_value();
return 0;
}
明日更新预告:更新改错题和更难一点的题目