5.8 面向对象程序设计C++课后习题
2021/5/8 12:25:10
本文主要是介绍5.8 面向对象程序设计C++课后习题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.请检查下面的程序,找出其中的错误(先不要上机,在纸面上作人工的检查),并改正。然后上机调试,使之能正常运行。运行时从键盘输入时、分、秒的值,检查是否正确。
# include <iostream>
using namespace std;
class Time
{ void set_time();
void show_time();
int hour;
int minute;
int sec;};
// hour, minute, sec是私有变量,而函数是公有函数应该是public
Time t;
int main()
{
set_time();
show_time();
//应该添加类的对象
return 0;
}
int set_time() //函数的类型与声明时的类型不同,且未限定作用域
{
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
}
int show_time() //函数的类型与声明时的类型不同,且未限定作用域
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
修正后:
# include <iostream>
using namespace std;
class Time
{ int hour;
int minute;
int sec;
public:
void set_time();
void show_time();
};
Time t;
void Time::set_time()
{
cin>>t.hour;
cin>>t.minute;
cin>>t.sec;
}
void Time::show_time()
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
int main()
{
t.set_time();
t.show_time();
return 0;
}
2.改写例8.1程序,要求:
(1)将数据成员改为私有的;
(2)将输入和输出的功能改为由成员函数实现;
(3)在类体内定义成员函数。
8.1代码:
# include <iostream>
using namespace std;
class Time
{public:
int hour;
int minute;
int sec;
};
int main()
{Time t1;
cin>>t1.hour;
cin>>t1.minute;
cin>>t1.sec;
cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;
return 0;
}
# include <iostream>
using namespace std;
class Time
{int hour;
int minute;
int sec;
public:
void set_hour()
{cin>>hour;}
void set_minute()
{cin>>minute;}
void set_sec()
{cin>>sec;}
void show_time()
{cout<<hour<<":"<<minute<<":"<<sec<<endl;}
};
int main()
{Time t1;
t1.set_hour();
t1.set_minute();
t1.set_sec();
t1.show_time();
return 0;
}
3.在第2题的基础上进行如下修改:在类体内声明成员函数,而在类外定义成员函数。
# include <iostream>
using namespace std;
class Time
{int hour;
int minute;
int sec;
public:
void set_hour();
void set_minute();
void set_sec();
void show_time();
};
int main()
{Time t1;
t1.set_hour();
t1.set_minute();
t1.set_sec();
t1.show_time();
return 0;
}
void Time::set_hour()
{cin>>hour;}
void Time::set_minute()
{cin>>minute;}
void Time::set_sec()
{cin>>sec;}
void Time::show_time()
{cout<<hour<<":"<<minute<<":"<<sec<<endl;}
6.需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、height(高)。要求用成员函数实现以下功能:
(1)由键盘分别输入3个长方柱的体积;
(2)计算长方柱的体积;
(3)输出3个长方柱的体积。
请编写程序,上机调试并运行。
代码:
# include <iostream>
using namespace std;
class Rectangular
{public:
float length,width,height;
void volume()
{cout<<length*width*height<<endl;}
};
Rectangular R1,R2,R3;
main()
{cin>>R1.height>>R1.length>>R1.width;
cin>>R2.height>>R2.length>>R2.width;
cin>>R3.height>>R3.length>>R3.width;
R1.volume();
R2.volume();
R1.volume();
return 0;
}
这篇关于5.8 面向对象程序设计C++课后习题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-04el-table 开启定时器下,表格的选中状态会消失是什么原因-icode9专业技术文章分享
- 2024-10-03如何安装和初始化飞牛私有云 fnOS?-icode9专业技术文章分享
- 2024-10-03如何安装 App 并连接到飞牛 NAS?-icode9专业技术文章分享
- 2024-10-03如何安装飞牛 TV 并连接到影视服务器?-icode9专业技术文章分享
- 2024-10-03如何在PVE和ESXI上安装飞牛私有云 fnOS?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS安装系统异常情况处理-icode9专业技术文章分享
- 2024-10-03飞牛NAS如何创建存储空间?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS硬盘会自动休眠吗?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS如何安装飞牛影视和创建媒体库?-icode9专业技术文章分享
- 2024-10-03fnOS国产最强NAS如何为家人朋友开通影视账号?-icode9专业技术文章分享