Accelerated C++:通过示例进行编程实践——练习解答(第9章)

我的Github地址:https://github.com/lanbeilyj/Accerlerated-C-plus-plus

9-0. Compile, execute, and test the programs in this chapter.

Ans:见Github。

9-1. Reimplement the Student_info class so that it calculates the finalgrade when reading the student's record, and stores that grade in the object. Reimplement thegrade function to use this precomputed value.

Ans:见Github。

9-2. If we define the name function as a plain, nonconst member function, what other functions in our system must change and why?

Ans:我们仅仅需要修改函数 compare()的参数类型即可:将原先的bool compare(const Student_info& x,const Student_info& y)改为

bool compare(Student_info x,Student_info y)

Tips:const对象不可以调用const成员函数函数;

为什么又把 Student_info& 改成了Student_info x?

因为sort(students.begin(),students.end(),compare),函数中传递给compare是一个const对象,因为不能用一个const对象来初始化一个非const引用,故需要修改为传值形式。

9-3. Our grade function was written to throw an exception if a user tried to calculate a grade for aStudent_info object whose values had not yet been read. Users who care are expected to catch this exception. Write a program that triggers the exception but does not catch it. Write a program that catches the exception.

Ans:见Github。

9-4. Rewrite your program from the previous exercise to use thevalid function, thereby avoiding the exception altogether.

Ans:见Github。

9-5. Write a class and associated functions to generate grades for students who take the course for pass/fail credit. Assume that only the midterm and final grades matter, and that a student passes with an average exam score greater than 60. The report should list the students in alphabetical order, and indicate P or F as the grade.

Ans:见Github。

9-6. Rewrite the grading program for the pass/fail students so that the report shows all the students who passed, followed by all the students who failed.

Ans:见Github。

9-7. The read_hw function §4.1.3/57 solves a general problem (reading a sequence of values into avector) even though its name suggests that it should be part of the implementation ofStudent_info. Of course, we could change its name—but suppose, instead, that you wanted to integrate it with the rest of theStudent_info code, in order to clarify that it was not intended for public access despite its apparent generality? How would you do so?

Ans:见Github。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值