1. Trang chủ
  2. » Giáo án - Bài giảng

Kỹ thuật lập trình hệ cơ điện tử= programming engineering in mechatronics chapter ii modular programming in c++

132 2 0

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI KỸ THUẬT LẬP TRÌNH HỆ CƠ ĐIỆN TỬ Programming Engineering in Mechatronics Giảng viên: TS Nguyễn Thành Hùng Đơn vị: Bộ môn Cơ điện tử, Viện Cơ khí Hà Nội, 2020 Chapter II Modular programming in C++ • Structured programming relies on top-down design • In C and C++ languages, the smallest structural unit having independent functionality is called function • If functions or a group of functions belonging together are put in a separate module (source file), modular programming is realised • Structural programming also contributes to creating new programs from achieved modules (components) by bottomup design • This chapter aims to introduce the modular and procedural programming in C++ Chapter II Modular programming in C++ ❖ The basics of functions ❖ How to use functions on a more professional level? ❖ Namespaces and storage classes ❖ Preprocessor directives of C++ Chapter II Modular programming in C++ ❖ The basics of functions ❖ How to use functions on a more professional level? ❖ Namespaces and storage classes ❖ Preprocessor directives of C++ The basics of functions ❖ Defining, calling and declaring functions ❖ The return value of functions ❖ Parametrizing functions ❖ Programming with functions The basics of functions • In C++, a function is a unit (a subprogram) that has a name and that can be called from the other parts of a program as many times as it is needed • In order to use a function efficiently, some of its inner variables (parameters) are assigned a value when the function is called • When a function is called (activated), the values (arguments) to be assigned to each parameter have to be passed in a similar way • The called function passes control back to the place where it was called by a return statement • The value of the expression in the return statement is the return value returned back by the function, which is the result of the function call expression The basics of functions ❖ Defining, calling and declaring functions • C++ Standard Library provides us many useful predefined functions function header file sqrt() isalpha() atoi() rand() cmath cctype cstdlib cstdlib strlen() wcslen() cstring cwchar The basics of functions ❖ Defining, calling and declaring functions • The general form of a function definition is the following (the signs 〈 〉 indicate optional parts): Function definition The basics of functions ❖ Defining, calling and declaring functions The basics of functions ❖ Defining, calling and declaring functions • The steps of calling a function function_name (〈argument , argument , … argument n 〉) Steps of calling a function 10 Preprocessor directives of C++ ❖ Conditional compilation • For more complex structures, it is recommended to use multiway branches 118 Preprocessor directives of C++ ❖ Conditional compilation • The following example integrates the declaration file on the basis of the manufacturer: 119 Preprocessor directives of C++ ❖ Including files ❖ Conditional compilation ❖ Using macros 120 Preprocessor directives of C++ ❖ Using macros ▪ Symbolic constants • Symbolic constants can be created by using the simple form of the #define directive: 121 Preprocessor directives of C++ ❖ Using macros ▪ Symbolic constants 122 Preprocessor directives of C++ ❖ Using macros ▪ Parameterized macros • The macros can be efficiently used in much more cases if they are parameterized The general form of a function-like parameterized macro: • Using (calling) a macro: 123 Preprocessor directives of C++ ❖ Using macros ▪ Parameterized macros 124 Preprocessor directives of C++ ❖ Using macros ▪ Parameterized macros • All advantages of macros (type-independence, faster code) can be achieved by using inline functions/function templates (instead of macros) while keeping the C++ code legible: 125 Preprocessor directives of C++ ❖ Using macros ▪ Undefining a macro • A macro can be undefined anytime and can be redefined again, even with a different content It can be undefined by using the #undef directive Original source code Substituted code int main() { #define MACRO(x) (x) + int a = MACRO(12); #undef MACRO a = MACRO(12); #define MACRO 123 a = MACRO } int main() { int a = (12) + 7; a = MACRO(12); a = 123 } 126 Preprocessor directives of C++ ❖ Using macros ▪ Macro operators • If the # character is placed before the parameter in a macro, the value of the parameter is replaced as enclosed within quotation marks (i.e as a string) 127 Preprocessor directives of C++ ❖ Using macros ▪ Macro operators • By using the ## operator, two syntactic units (tokens) can be concatenated 128 Preprocessor directives of C++ ❖ Using macros ▪ Predefined macros Macro Description Example DATE String constant containing the date of the compilation "Oct 02 2013" TIME String constant containing the time of the compilation "10:02:04" TIMESTAMP_ The date and time of the last modification of the source file "Mon Jul 29 _ in a string constant" 07:33:29 2013" FILE String constant containing the name of the source file "c:\\preproc.cpp " LINE A numeric constant, containing the number of the actual line of the source file (numbering starts from 1) 1223 STDC cplusplus Its value is if the compiler works as an ANSI C++, otherwise it is not defined Its value is 1, if its value is tested in a C++ source file, otherwise it is not defined 129 Preprocessor directives of C++ ❖ Using macros ▪ #line, #error and #pragma directives • The #line directive forces C++ compilers not to signal the error code in the C++ source text but in the original source file written in another special language 130 Preprocessor directives of C++ ❖ Using macros ▪ #line, #error and #pragma directives • An #error directive can be used to print out a compilation error message which contains the text provided in the statement: #error error_message 131 Preprocessor directives of C++ ❖ Using macros ▪ #line, #error and #pragma directives • #pragma directives are used to control the compilation process in an implementation-dependent way • The empty directive (#) can also be used, but it does not affect preprocessing 132 ... modules (components) by bottomup design • This chapter aims to introduce the modular and procedural programming in C++ Chapter II Modular programming in C++ ❖ The basics of functions ❖ How to use.. .Chapter II Modular programming in C++ • Structured programming relies on top-down design • In C and C++ languages, the smallest structural unit having independent functionality... parts): Function definition The basics of functions ❖ Defining, calling and declaring functions The basics of functions ❖ Defining, calling and declaring functions • The steps of calling a function

Ngày đăng: 15/02/2022, 19:02

Xem thêm: