任务2:
//课后题目:程序找错
#include <iostream>
using namespace std;
class Time
{
public: //在这里缺少公共说明,class默认成员为private,不可访问
void set_time (void);
void show_time (void);
private:
int hour;
int minute;
int sec;
};
Time t;
int main (void)
{
t.set_time (); //缺少调用类成员的说明
t.show_time ();
return 0;
}
void Time::set_time (void) //函数返回型错误,缺少域运算符
{
cin >> t.hour;
cin >> t.minute;
cin >> t.sec;
}
void Time::show_time (void) //与上同
{
cout << hour << ":" << minute << ":" << sec << endl; //删除了t成员的专属调用
}
任务3:
//对类的功能进行增加
#include <iostream>
using namespace std;
class Time
{
public:
void set_time (void);
void show_time (void);
void add_seconds (int);
void add_minutes (int);
void add_hours (int);
private:
void add_a_sec (void);
void add_a_minute (void);
void add_an_hour (void);
bool is_time (int, int, int);
int hour;
int minute;
int sec;
};
int main (void)
{
Time t1;
Time &t2 = t1;
t1.set_time ();
t2.show_time ();
int n, m, t;
cout << "请依次输入增加的秒,分,时:" << endl;
cin >> n >> m >> t;
t1.add_seconds (n);
t1.add_minutes (m);
t1.add_hours (t);
t2.show_time ();
return 0;
}
void Time::set_time (void)
{
char c1, c2;
cout << "请输入时间(格式hh:mm:ss)";
while (1)
{
cin >> hour >> c1 >> minute >> c2 >> sec;
if (c1 != ':' || c2 != ':')
{
cout << "格式不正确,请重新输入。" << endl;
}
else if (!is_time (hour, minute, sec))
{
cout << "时间非法,请重新输入。" << endl;
}
else
{
break;
}
}
}
void Time::show_time (void)
{
cout << hour << ":" << minute << ":" << sec << endl;
}
bool Time::is_time (int h, int m, int s)
{
if (h < 0 || h > 24 || m <= 0 || m >= 60 || s <= 0 || s >= 60)
{
return false;
}
return true;
}
void Time::add_a_sec (void)
{
++sec;
if (sec <= 0 || sec >= 60)
{
sec = 0;
++minute;
}
if (minute <= 0 || minute >= 60)
{
minute = 0;
++hour;
}
}
void Time::add_seconds (int n)
{
while (n)
{
add_a_sec ();
--n;
}
}
void Time::add_a_minute (void)
{
++minute;
if (minute <= 0 || minute >= 60)
{
minute = 0;
++hour;
}
}
void Time::add_minutes (int n)
{
while (n)
{
add_a_minute ();
--n;
}
}
void Time::add_an_hour (void)
{
++hour;
}
void Time::add_hours (int n)
{
while (n)
{
add_an_hour ();
--n;
}
}
任务4:
//对类的功能进行增加
#include <iostream>
using namespace std;
class Changfangzhu
{
public:
void input (void);
void output (void);
private:
int length;
int width;
int heigth;
int volume;
int areas;
int num;
};
int main (void)
{
Changfangzhu chang [3];
for (int i = 0; i < 3; ++i)
{
chang [i].input ();
chang [i].output ();
}
return 0;
}
void Changfangzhu::input (void)
{
int n, a, b, c;
while (1)
{
cout << "请输入长方形的长,宽,高和所要输出的内容:1.体积,2.表面积" << endl;
cin >> a >> b >> c >> n;
length = a;
width = b;
heigth = c;
num = n;
if (num == 1)
{
volume = length * width * heigth;
break;
}
else if (num == 2)
{
areas = 2 * length * width + 2 * width * heigth + 2 * length * heigth;
break;
}
else
{
cout << "选择错误,该次操作无效。" << endl;
}
}
}
void Changfangzhu::output (void)
{
cout << "长:" << length << "宽:" << width << "高:" << heigth << endl;
if (num == 1)
{
cout << "体积:" << volume << endl;
}
else
{
cout << "表面积:" << areas << endl;
}
}
![](http://hi.csdn.net/attachment/201203/5/0_1330948277R06p.gif)