以往章节
1. (简答题)
请检查下⾯程序,出其中的错误(先不要上机,在纸⾯上作⼈⼯检查),并改正之。然后上机调试,使之能正常运⾏。运⾏时从键盘输⼊时、分、秒的值,检查输出是否正确。
#include <iostream>
安全门卡
飞轮齿圈using namespace std;
class Time
{ void set_time(void);
void show_time(void);
int hour;
int minute;
int sec;
};
Time t;
int main( )
{
set_time( );
show_time( );
}
int set_time(void)
{
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
}
int show_time(void)
{
cout<<t.hour<<″:″<<t.minute<<″:″<<t.sec<<endl;
}
{
public:
void set_time(void);
void show_time(void);
private:
int hour;
int minute;
int sec;
};
Time t;
int main()
{
t.set_time();
t.show_time();
}
void Time::set_time(void)
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void Time::show_time(void)
{
cout << hour << ":" << minute << ":" << sec << endl; }
2. (简答题)
改写本章例2.1程序,要求:
(3) 在类体内定义成员函数。
void set_time(void)
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void show_time(void)
{
cout << hour << ":" << minute << ":" << sec << endl;
}
};
private:
int hour;
int minute;
int sec;
};
Time t1,t2;
int main()
{
t1.set_time();
t1.show_time();
t2.set_time();
t2.show_time();
return 0;
}
3. (简答题)
在上⼀题的基础上进⾏如下修改: 在类体内声明成员函数,⽽在类外定义成员函数。
void set_time(void);
void show_time(void);
private:
int hour;
int minute;
int sec;
};
int main()
{
Time t1,t2;
t1.set_time();
t1.show_time();
t2.set_time();
t2.show_time();
return 0;
}
void Time::set_time(void)
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void Time::show_time(void)
{
dvd制作cout << hour << ":" << minute << ":" << sec << endl;
}桶盖
4. (简答题)在本章第2.3.3节中分别给出了包含类定义的头⽂件student.h,包含成员函数定义的源⽂件student.cpp 以及包含主函数的源⽂件main.cpp。请完善该程序,在类中增加⼀个对数据成员赋初值的成员函数set_value。上机调试并运⾏。
{
public:
void set_value();
void display();
private:
int num;
string name;
char sex;
};瑞利衰落
//上⾯是我头⽂件底下是我的源⽂件
#include<iostream>
#include"student.h"
void Student::set_value()
{
cin >> num >> name >> sex;
按钮指示灯}
void Student::display()
{
cout << num << endl;
cout << name << endl;
cout << sex << endl;
}
//这是我主函数源⽂件
#include<iostream>
#include"student.h"
using namespace std;
int main()
{
Student s;
s.set_value();
s.display();
return 0;
}
5. (简答题)
将本章的例2.4 改写为⼀个多⽂件的程序:
(1) 将类定义放在头⽂件arraymax.h中;
(2) 将成员函数定义放在源⽂件arraymax.cpp中;
(3) 主函数放在源⽂件file1.cpp中。
请写出完整的程序,上机调试并运⾏。