C++编程题目
1 设计一个日期类,其输出格式是:"月/日/年"或"June 13,1993",再编写一个测试程序,要求测试程序能实现两种输出格式的输出。(提示:利用构造函数重载和输出成员函数重载。)
2 设计一个汽车性能类,其成员包括:颜色品牌车门车灯行驶速度;其方法包括:成员函数包括:打开车门打开车灯加速减速等。最后,编写一个测试程序进行测试。(在成员函数中用输出成员函数名来表示该成员函数被调用。)
参考答案:你题目不清,但是根据提议随便写了点。
//时间问题
#include <iostream.h>
#include <stdio.h>
class c_mytime
{
public:
c_mytime();
void showtime();
protected:
int year;
int month;
int day;
};
c_mytime::c_mytime()
{
year = 0;
month = 0;
day = 0;
}
void c_mytime::showtime()
{
printf("%d月\\%d日\\%d年\n", month, day, year);
}
void main()
{
c_mytime mytime;
mytime.showtime();
}
//车况问题
#include <iostream.h>
#include <stdio.h>
class car
{
public:
car();
bool isdooropen;
bool islightopen;
bool isspeedup;
bool isslowdown;
void status();
};
car::car()
{
isdooropen = false;
islightopen = false;
isspeedup = true;
isslowdown = !isspeedup;
}
void car::status()
{
if(isdooropen == true)
cout<<"车门已关!"<<endl;
else
cout<<"车门开启!"<<endl;
if(islightopen == true)
cout<<"车灯已开!"<<endl;
else
cout<<"车灯关闭!"<<endl;
if(isspeedup == true)
cout<<"车在加速!"<<endl;
else
cout<<"车在减速!"<<endl;
}
void main()
{
car mycar;
mycar.status();
}