天津理工大学
计算机科学与技术学院
实验报吿
至学年第学期
课程名称
C++程序设计应用
学号
学生姓名
年级
13级
专业
教学班号
实验地点
7-219
实验时间
年月日第节至第节
主讲教师
辅导教师
实验(二)
实验名称复杂形式的对象
软件环境
Visual C卄 6. 0
硬件环境
台式计算机
实验目的
(1)掌握対象数组、対象指针的使用方法;
(2)掌握对象数组、对象指针和对象引用作为函数参数的使用方法;
(3)掌握类对象作为成员的使用方法;
(4)掌握友元的概念和使用方法;
(5)掌握静态数据成员和静态成员函数的使用方法;
(6)进一步了解C++面向对象程序设计的基本思想、基本方法和基本步骤;
实验内容(应包括实验题目、实验要求、实验任务等)
1.代码及注释;
#include
using namespace std;
class Time〃声明 Time 类
{
public:
Time(int a=O,int b=O,int c=0) //带默认参数的构造函数
{
hour=a;
minute=b;
second=c;
}
// set functions
void setTime(int h,int m,int s)// 设置 hour, minute, second
{
hour=h;
minutc=m;
second=s;
)
void setHour(int a)〃设置hour (确保数据在合理范I羽)
{
if(a<24&&a>=0)
hour=a;
实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)
//设置
//设置minute (确保数据在介理范围)
//设置second (确保数据在合理范围)
void sctMinutc(int b) {
if(b<60&&b>=0) minute=b;
}
void setSecond(int c)
{
if(c<60&&c>=0)
second=c;
}
int gctHourO // 返回 hour
{
return hour;
)
int getMinute() // 返冋 minute {
return minute;
}
int getSecond() 〃返回 second {
return second;
void print Un iversal() {
cout?n24
〃按24小时格式输出时间:23:56:12
小 时 格 式 的 时
n?getHour()?H: ,,?getMinute()?n: H?getSecond()?endl;
}
void printStandard() // 按 12 小时格式输出时间:11:56:12 (PM) {
if(hour>12)
cout?H12 小 时 格 式 的 时
n?getHour()-12?":"?getMinute()?',:,,?getSecond()?"(pm)"?endl;
else
cout?n12 小 时 格 式 的 时
"?getHour()?":"?getMinute()?,,:"?getSecond()?,,(am),'?endl;
间为:
-9:23:55(AM)
间为:
间为:
private:
int hour; // 0 - 23 (24 小时格式)
int minute; //0 - 59
int second; // 0 - 59
};//Timel类定义结束
int main()
{
int hl,ml,si;
int h2,m2,s2;
int a;
Time tl,t2;
cout?"请输入一个24小时格式的时间:"vvcndl;
cin?h 1 ?m 1 ?s 1;
t l.setTime(h 1 ,ml ,s 1);
tl.printStandard();
cout?"请输入一个12小时格式的时间:(上午在最示用1表示,下午用2表示)M?endl; cin?h2?m2?s2?a;
t2.sctMinutc(m2);
t2.setSecond(s2);
if(a== 1)
t2.setHour(h2);
else
t2.setHour(h2+12);
t2.pri ntUniversal();
return 0;
结果截图
-创天中文
Time二(All class members]^] ime文件繃5
Time二(All class members]^] ime
文件繃5血入工程那工具窗口龍助
4 邱 1 classes
■: Cl8ssV???]j]FlleView|
1.exe - 0 error(s), 0 warning(s)
2.代码及注释F开始
2.
代码及