blob: 21f9345c51094891d854b50cb63d23196425ff8f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
// Copyright (C) 2008-2018 Lorenzo Caminiti
// Distributed under the Boost Software License, Version 1.0 (see accompanying
// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
//[n1962_circle
#include <boost/contract.hpp>
#include <cassert>
class shape {
public:
virtual ~shape() {}
virtual unsigned compute_area(boost::contract::virtual_* v = 0) const = 0;
};
unsigned shape::compute_area(boost::contract::virtual_* v) const {
unsigned result;
boost::contract::check c = boost::contract::public_function(v, result, this)
.postcondition([&] (int const& result) {
BOOST_CONTRACT_ASSERT(result > 0);
})
;
assert(false);
return result;
}
class circle
#define BASES public shape
: BASES
{
friend class boost::contract::access;
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
BOOST_CONTRACT_OVERRIDE(compute_area);
public:
static int const pi = 3; // Truncated to int from 3.14...
explicit circle(unsigned a_radius) : radius_(a_radius) {
boost::contract::check c = boost::contract::constructor(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT(radius() == a_radius);
})
;
}
virtual unsigned compute_area(boost::contract::virtual_* v = 0) const
/* override */ {
unsigned result;
boost::contract::check c = boost::contract::public_function<
override_compute_area>(v, result, &circle::compute_area, this)
.postcondition([&] (unsigned const& result) {
BOOST_CONTRACT_ASSERT(result == pi * radius() * radius());
})
;
return result = pi * radius() * radius();
}
unsigned radius() const {
boost::contract::check c = boost::contract::public_function(this);
return radius_;
}
private:
unsigned radius_;
};
int main() {
circle c(2);
assert(c.radius() == 2);
assert(c.compute_area() == 12);
return 0;
}
//]
|