Accelerated C++:通过示例进行编程实践——练习解答(第9章)
2014-10-25 12:12
447 查看
[b]我的Github地址:https://github.com/lanbeilyj/Accerlerated-C-plus-plus
[/b]
9-0. Compile, execute, and test the programs in this chapter.
Ans:见Github。
9-1. Reimplement the
Ans:见Github。
9-2. If we define the
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
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 the
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
we could change its name—but suppose, instead, that you wanted to integrate it with the rest of the
Ans:见Github。
[/b]
9-0. Compile, execute, and test the programs in this chapter.
Ans:见Github。
9-1. Reimplement the
Student_infoclass so that it calculates the final
gradewhen reading the student's record, and stores that grade in the object. Reimplement the
gradefunction to use this precomputed value.
Ans:见Github。
9-2. If we define the
namefunction as a plain, non
constmember 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
gradefunction was written to throw an exception if a user tried to calculate a grade for a
Student_infoobject 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 the
validfunction, 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_hwfunction §4.1.3/57 solves a general problem (reading a sequence of values into a
vector) even though its name suggests that it should be part of the implementation of
Student_info. Of course,
we could change its name—but suppose, instead, that you wanted to integrate it with the rest of the
Student_infocode, in order to clarify that it was not intended for public access despite its apparent generality? How would you do so?
Ans:见Github。
相关文章推荐
- Accelerated C++:通过示例进行编程实践——练习解答(第3章)
- Accelerated C++:通过示例进行编程实践——练习解答(第8章)
- Accelerated C++:通过示例进行编程实践——练习解答(第4章)
- Accelerated C++:通过示例进行编程实践——练习解答(第6章)
- Accelerated C++:通过示例进行编程实践——练习解答(第15章)
- Accelerated C++:通过示例进行编程实践——练习解答(第11章)
- Accelerated C++:通过示例进行编程实践——练习解答(第1章)
- Accelerated C++:通过示例进行编程实践——练习解答(第7章)
- Accelerated C++:通过示例进行编程实践——练习解答(第13章)
- Accelerated C++:通过示例进行编程实践——练习解答(第14章)
- Accelerated C++:通过演示样例进行编程实践——练习解答(第9章)
- Accelerated C++:通过示例进行编程实践——练习解答(第12章)
- Accelerated C++:通过示例进行编程实践——练习解答(第10章)
- Accelerated C++:通过示例进行编程实践——练习解答(第5章)
- Accelerated C++:通过示例进行编程实践——练习解答(第2章)
- Accelerated C++:通过示例进行编程实践——练习解答(第0章)
- Accelerated C++:通过示例进行编程实践——练习解答(第16章)
- Accelerated C++:通过示例进行编程实践——习题解答(第1章)
- Accelerated C++:通过示例进行编程实践——习题解答(第0章 入门)
- 《Accelerated C++中文版 通过示例进行编程实践》课后习题解答,第0章