Lecture Data structures and other objects using C++ - Chapter 1: Preconditions and postconditions - Trường Đại học Công nghiệp Thực phẩm Tp. Hồ Chí Minh

10 7 0
Lecture Data structures and other objects using C++ - Chapter 1: Preconditions and postconditions - Trường Đại học Công nghiệp Thực phẩm Tp. Hồ Chí Minh

Đang tải... (xem toàn văn)

Thông tin tài liệu

Example You are the head of a  You are the head of a  programming team  programming team  and you want one of  and you want one of  your programmers to  your programmers to  write a func[r]

(1)

An important topic: 

An important topic:  preconditions

preconditions and  and 

postconditions

postconditions

They are a method of 

They are a method of 

specifying what a 

specifying what a 

function accomplishes

function accomplishes

Preconditions and Postconditions

Data Structures

Data Structures

and Other Objects

and Other Objects

Using C++

(2)

Frequently a programmer must communicate  Frequently a programmer must communicate 

precisely 

precisely whatwhat  a function accomplishes, a function accomplishes,  without any indication of 

without any indication of howhow the function  the function  does its work

does its work

Can you think of a situation

Can you think of a situation

where this would occur ?

(3)(4)

One way to specify such requirements is  One way to specify such requirements is 

with a pair of statements about the function with a pair of statements about the function

The 

The preconditionprecondition statement indicates what  statement indicates what  must be true before the function is called must be true before the function is called

The 

The postconditionpostcondition statement indicates what  statement indicates what  will be true when the function finishes its 

will be true when the function finishes its  work

(5)

Example

void write_sqrt(double x) //   Precondition:  x  >=  0.

//   Postcondition:  The square root of x has

//   been written to the standard output.

(6)

void write_sqrt( double x)

//   Precondition:  x  >=  0

//   Postcondition:  The square root of x has //   been written to the standard output

 

}

The precondition and 

The precondition and 

postcondition appear as 

postcondition appear as 

comments in your program

(7)

Example

void write_sqrt( double x)

//   Precondition:  x  >=  0

//   Postcondition:  The square root of x has //   been written to the standard output

 

}

In this example, the precondition 

In this example, the precondition 

requires that

requires that

      

      x >= 0x >= 0

be true whenever the function is be true whenever the function is 

called

(8)

write_sqrt( ­10 ); write_sqrt( 0 ); write_sqrt( 5.6 );

Which of these function calls

Which of these function calls

meet the precondition ?

(9)

Example

Which of these function calls

Which of these function calls

meet the precondition ?

meet the precondition ? write_sqrt( ­10 );

write_sqrt( 0 ); write_sqrt( 5.6 );

(10)

Which of these function calls

Which of these function calls

meet the precondition ?

meet the precondition ?

write_sqrt( ­10 );

write_sqrt( 0 ); write_sqrt( 5.6 );

But the first call violates the 

Ngày đăng: 01/04/2021, 19:26

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan