Skip to Content
C++程序设计:原理与实践(基础篇)(原书第2版)
book

C++程序设计:原理与实践(基础篇)(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
9h 36m
Chinese
Pearson
Content preview from C++程序设计:原理与实践(基础篇)(原书第2版)

5.10.1 后置条件

使用前置条件将有助于我们避免设计错误和及早发现使用错误。这种显式说明需求的思想能够被应用在其他方面么?是的,你可以马上联想到:返回值!毕竟,我们一般都需要声明函数的返回值是什么。也就是说,如果一个函数返回一个值的话,我们总是要约定这个返回值是怎样的(否则的话调用者如何知道得到的是什么呢?)。让我们再次看一下面积函数(来自5.6.1节):

这个程序检查了前置条件,但是它没有在注释里面对此进行说明(对于这么一个小函数来说,这还是可以接受的),而且假定计算是正确的(这种简单计算应该也没问题)。但是,我们可以更明确一些:

我们不可能对后置条件进行完全检查,但是我们至少可以检查其中一部分:返回值是否是正数。

试一试

尝试找出一组数据,它们能够满足当前版本面积函数的前置条件,但不满足后置条件。

前置和后置条件提供了基本的程序完整性检查。从这个角度看,它们与不变式(9.4.3节)、正确性(4.2节和5.2节)和测试(第26章)等概念紧密相关。

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252