tailieunhanh - Lecture Introduction to Computer Programming - Lecture 26

In this section, we introduce the features of JML as they apply to the formal specification and verification of an individual function, such as the Factorial function that we specified and verified by hand in the previous section. We also show how JML allows us to specify run-time exceptions, providing a more robust vehicle than the pure Hoare triples in a real computational setting where exceptions actually occur. | CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 26 Thanks for Lecture Slides: 1 Unions : Introduction Memory that contains a variety of objects over timeBut use one data member at a time Members of a union share space and hence conserves storage Members of a union can be of any data type. Number of bytes used to store a union must be at least enough to hold the largest member of the union. Only the last data member defined can be accessed Important to ensure that that the data in a union is referenced with the proper data type 2 Union Declarations Declared with keyword union in the same format as a structure union Number { int x; double y; }; The union definition is normally placed in a header and included in all source files that use the union type. Valid Operations on Unions Assignment to union of same type: = Taking address (&) of a union variable Accessing union members using (.) Accessing members using pointers (->) 4 Invalid Operations on Unions Unions may NOT be compared using (==) and (!=) for the same reasons that structures cannot be compared. Comparing unions is a ‘syntax error’. Union may be initialized with a value of the same type as the first union member. For example, union Number value = { 10 }; Above initialization of union variable is valid because the union is initialized with an int which is the same type as the first union member. But, the following declaration would truncate the floating-point part of the value. union Number value = { }; It will also produce a warning from the compiler. Initializing Unions in Declarations Demonstrating Unions // Fig. : // Displaying the value of a union in both member data types #include // number union definition union number { int x; double y; }; // end union number int main( void ) { union number value; // define union variable = 100; // put an integer into . | CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 26 Thanks for Lecture Slides: 1 Unions : Introduction Memory that contains a variety of objects over timeBut use one data member at a time Members of a union share space and hence conserves storage Members of a union can be of any data type. Number of bytes used to store a union must be at least enough to hold the largest member of the union. Only the last data member defined can be accessed Important to ensure that that the data in a union is referenced with the proper data type 2 Union Declarations Declared with keyword union in the same format as a structure union Number { int x; double y; }; The union definition is normally placed in a header and included in all source files that use the union type. Valid Operations on Unions Assignment to union of same type: = Taking address (&) of a union variable Accessing union members .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.