1、请检查下面程序,找出其中的错误(先不要上机,自己先检查),并改正之。然后上机调试,使之能正常运行,运行时从键盘输入时、分、秒的值,检查输出是否正确。
#include <iostream>
using namespace std;
class Time
{
int hour;
int minute;
int sec;
void set_time();
void show_time();
};
Time t;
int main( )
{
set_time();
show_time();
return 0;
}
void set_time()
{
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
}
void show_time()
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
2、分别给出如下的3个文件:
(1)、含类定义的头文件student.h,
class Student //类声明
{ public:
void display( ); //公用成员函数原型声明
private:
int num;
char name[20];
char sex;
};
(2)、包含成员函数定义的源文件student.cpp,
#include <iostream>
using namespace std;
#include "student.h" //不要漏写此行,否则编译通不过
void Student::display( ) //在类外定义display类函数
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
(3)、包含主函数的源文件main.cpp,
为了组成一个完整的源程序,应当有包含主函数的源文件:
#include "student.h" //将类声明头文件包含进来
int main( )
{Student stud; //定义对象
stud.display( ); //执行stud对象的display函数
return 0;
}
请完善该程序,在类中增加一个对数据成员赋予初值的成员函数void input_data()。上机调试并运行。
3、需要求三个长方柱的体积,请编写一个基于对象的程序,数据成员包括length(长)、width(宽)、height(高)。
要求用成员函数实现以下功能:
(1)、由键盘输入三个长方柱的长、宽、高;
(2)、计算三个长方柱的体积;
(3)、输出三个长方柱的体积;