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

我的Github地址:https://github.com/CNhoward/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。

时间: 2024-11-12 10:39:16

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

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

我的Github地址:https://github.com/CNhoward/Accerlerated-C-plus-plus 10-0. Compile, execute, and test the programs in this chapter. Ans:见Github. 10-1. Rewrite the student-grading program from §9.6/166 to generate letter grades. Ans:见Github. 10-2. Rewrite

[Java 并发] Java并发编程实践 思维导图 - 第一章 简介

阅读<Java并发编程实践>一书后整理的思维导图.

[Java 并发] Java并发编程实践 思维导图 - 第二章 线程安全性

根据<Java并发编程实践>一书整理的思维导图.

[Java 并发] Java并发编程实践 思维导图 - 第一章 简单介绍

阅读<Java并发编程实践>一书后整理的思维导图.

读Java并发编程实践中,向已有线程安全类添加功能--客户端加锁实现示例

在Java并发编程实践中4.4中提到向客户端加锁的方法.此为验证示例,写的不好,但可以看出结果来. package com.blackbread.test; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public

Socket编程实践(10) --select的限制与poll的使用

select的限制 用select实现的并发服务器,能达到的并发数一般受两方面限制: 1)一个进程能打开的最大文件描述符限制.这可以通过调整内核参数.可以通过ulimit -n(number)来调整或者使用setrlimit函数设置,但一个系统所能打开的最大数也是有限的,跟内存大小有关,可以通过cat /proc/sys/fs/file-max 查看 /**示例: getrlimit/setrlimit获取/设置进程打开文件数目**/ int main() { struct rlimit rl;

编程实践积累

1 编程经验实践,积累,总结,分享 2 3 4 博客地址:http://www.cnblogs.com/liaowanzhong/ 5 6 7 8 9 10 11 12 13 14 15 1. 大量群发邮件:购买Edm服务,大的互联网企业是和邮箱服务商签订协议(百度,腾讯,京东,阿里,csdn) 16 站内信 17 内网发短信:短信猫 18 19 2. Servlet Filter 生命周期 20 *Servlet:看配置文件中web.xml配置其启动的优先级别,即当load-on-startu

Socket编程实践(6) --TCP服务端注意事项

僵尸进程处理 1)通过忽略SIGCHLD信号,避免僵尸进程 在server端代码中添加 signal(SIGCHLD, SIG_IGN); 2)通过wait/waitpid方法,解决僵尸进程 signal(SIGCHLD,onSignalCatch); void onSignalCatch(int signalNumber) { wait(NULL); } 3) 如果多个客户端同时关闭, 问题描述如下面两幅图所示: /** client端实现的测试代码**/ int main() { int s

Python编程从入门到实践(第三、四章的列表和元祖) &#142015;

原文: http://blog.gqylpy.com/gqy/414 置顶:来自一名75后老程序员的武林秘籍--必读(博主推荐) 来,先呈上武林秘籍链接:http://blog.gqylpy.com/gqy/401/ 你好,我是一名极客!一个 75 后的老工程师! 我将花两分钟,表述清楚我让你读这段文字的目的! 如果你看过武侠小说,你可以把这个经历理解为,你失足落入一个山洞遇到了一位垂暮的老者!而这位老者打算传你一套武功秘籍! 没错,我就是这个老者! 干研发 20 多年了!我也年轻过,奋斗过!我