C++面向对象的程序设计谭浩强第二章课后题

C++⾯向对象的程序设计谭浩强第⼆章课后题
以往章节
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程序,要求:
(1) 将数据成员改为私有的;
(2) 将输⼊和输出的功能改为由成员函数实现;
(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中。
请写出完整的程序,上机调试并运⾏。

本文发布于:2024-09-23 09:29:48,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/166613.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:成员   程序   函数
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议