1. Trang chủ
  2. » Luận Văn - Báo Cáo

Kỹ thuật lập trình hệ cơ điện tử= Programming Engineering in Mechatronics. Chapter II. Modular programming in C++85

132 6 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

Thông tin cơ bản

Định dạng
Số trang 132
Dung lượng 10,85 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

❖ Parametrizing functions▪ Using parameters of different types • Arithmetic type parameters The basics of functions... ❖ Parametrizing functions▪ Using parameters of different types • Pa

Trang 1

KỸ THUẬT LẬP TRÌNH HỆ CƠ ĐIỆN TỬ Programming Engineering in Mechatronic

TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI

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

Trang 2

Chapter II Modular programming in C++

• Structured programming relies on top-down design

• In C and C++ languages, the smallest structural unit havingindependent 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 bottom

up design.

This chapter aims to introduce the modular and procedura

programming in C++.

Trang 3

Chapter II Modular programming in C++

The basics of functions

How to use functions on a more professional level?

Preprocessor directives of C++

Trang 4

Chapter II Modular programming in C++

The basics of functions

How to use functions on a more professional level?

Preprocessor directives of C++

Trang 5

The basics of functions

Defining, calling and declaring functions

The return value of functions

Parametrizing functions

Programming with functions

Trang 6

The basics of functions

• In C++, a function is a unit (a subprogram) that has a name and that c

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 calle

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.

Trang 7

The basics of functions

❖ Defining, calling and declaring functions

• C++ Standard Library provides us many useful predefined functions

Trang 8

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

Trang 9

The basics of functions

❖ Defining, calling and declaring functions

Trang 10

The basics of functions

❖ Defining, calling and declaring functions

• The steps of calling a function

function_name argument(〈 1 ,argument 2, … argument n〉 )

Steps of calling a function

Trang 11

The basics of functions

❖ Defining, calling and declaring functions

• C++ standards require that functions have to be declared before they are called

Function declaration

Function definition

Trang 12

The basics of functions

❖ Defining, calling and declaring functions

• The complete declaration of a function (its prototype ):

return_value function_name( parameter declaration list〈 〉 );

return_value function_name( type_list〈 〉 );

declaration C interpretation C++ interpretation type funct(); type funct( ); type funct(void); type funct( ); type funct( ); type funct( ); type funct(void); type funct(void); type funct(void);

Trang 13

The basics of functions

❖ Defining, calling and declaring functions

• C++ makes it possible that a parameter list containing at least one parameter should end with three dots ( )

• The transferring (throw) of exceptions to the caller function can be enabled o disabled in function header

return_type function_name parameterlist(〈 〉) 〈throw( type_list )〈 〉 〉

{

local definitions and declarations〉

statements

return 〈expression ;〉 }

Trang 14

The basics of functions

❖ The return value of functions

• The return_type figuring in the definition/declaration of a function

determines the return type of the function, which can be of any C++ type wi the exception of arrays and functions.

Trang 15

The basics of functions

❖ The return value of functions

• By using the type void, we can create functions that do not return any value

Trang 16

The basics of functions

❖ The return value of functions

• Functions can return pointers or references

Trang 17

The basics of functions

Trang 18

The basics of functions

❖ Parametrizing functions

▪ Parameter passing methods

• Passing parameters by value

Trang 19

The basics of functions

❖ Parametrizing functions

▪ Parameter passing methods

• Passing parameters by value

Trang 20

The basics of functions

❖ Parametrizing functions

▪ Parameter passing methods

• Passing parameters by reference

Trang 21

The basics of functions

❖ Parametrizing functions

▪ Parameter passing methods

• Passing parameters by reference

Trang 22

The basics of functions

❖ Parametrizing functions

▪ Parameter passing methods

• Passing parameters by reference

Trang 23

❖ Parametrizing functions

▪ Using parameters of different types

• Arithmetic type parameters

The basics of functions

Trang 24

❖ Parametrizing functions

▪ Using parameters of different types

• Arithmetic type parameters

The basics of functions

Trang 25

❖ Parametrizing functions

▪ Using parameters of different types

• User-defined type parameters

The basics of functions

Trang 26

❖ Parametrizing functions

▪ Using parameters of different types

• User-defined type parameters

The basics of functions

Trang 27

❖ Parametrizing functions

▪ Using parameters of different types

• Passing arrays to functions

The basics of functions

Trang 28

❖ Parametrizing functions

▪ Using parameters of different types

• Passing arrays to functions

The basics of functions

Trang 29

❖ Parametrizing functions

▪ Using parameters of different types

• Passing arrays to functions

The basics of functions

Trang 30

❖ Parametrizing functions

▪ Using parameters of different types

• Passing arrays to functions

The basics of functions

Trang 31

❖ Parametrizing functions

▪ Using parameters of different types

• Passing arrays to functions

The basics of functions

Trang 34

❖ Parametrizing functions

▪ Using parameters of different types

• String arguments

Trang 35

The basics of functions

❖ Parametrizing functions

▪ Using parameters of different types

• String arguments

Trang 36

The basics of functions

❖ Parametrizing functions

▪ Using parameters of different types

• Functions as arguments

Trang 37

The basics of functions

❖ Parametrizing functions

▪ Using parameters of different types

• Functions as arguments

Trang 38

The basics of functions

Trang 39

The basics of functions

❖ Parametrizing functions

▪ Using parameters of different types

• Variable length argument list

Trang 40

The basics of functions

❖ Parametrizing functions

▪ Using parameters of different types

• Variable length argument list

Trang 41

The basics of functions

❖ Parametrizing functions

▪ Using parameters of different types

• Parameters and return value of the main() function

Trang 42

❖ Parametrizing functions

▪ Using parameters of different types

• Parameters and return value of the main() function

Providing command line

arguments

The basics of functions

Trang 43

❖ Parametrizing functions

▪ Using parameters of different types

• Parameters and return value of the main() function

In a console window In a development environment

C: \C++Book> command1 first 2nd third

C:\C++Book>

The basics of functions

Trang 44

❖ Parametrizing functions

▪ Using parameters of different types

• Parameters and return value of the main() function

The basics of functions

Trang 45

❖ Parametrizing functions

▪ Using parameters of different types

• Parameters and return value of the main() function

Wrong number of parameters :

command2

Correct number of parameterscommand2 alfa beta

Wrong number of parameters!

Usage: command2 arg1 arg2

Correct number of parameters: argument: alfa 2 argument: bet

The basics of functions

Trang 46

❖ Defining, calling and declaring functions

❖ The return value of functions

❖ Parametrizing functions

❖ Programming with functions

The basics of functions

Trang 47

❖ Programming with functions

▪ Exchanging data between functions using global variables

The basics of functions

Trang 48

❖ Programming with functions

▪ Exchanging data between functions using global variables

• Solution for above example

The basics of functions

Trang 49

❖ Programming with functions

▪ Exchanging data between functions using global variables

• Solution for above example

The basics of functions

Trang 50

❖ Programming with functions

▪ Exchanging data between functions using parameters

The basics of functions

Trang 51

❖ Programming with functions

▪ Exchanging data between functions using parameters

The basics of functions

Trang 52

❖ Programming with functions

▪ Exchanging data between functions using parameters

The basics of functions

Trang 53

❖ Programming with functions

▪ Implementing a simple menu driven program structure

The basics of functions

Trang 54

❖ Programming with functions

Trang 55

❖ Programming with functions

Trang 56

❖ Programming with functions

▪ Recursive functions

• greatest common divisor (gcd):

The basics of functions

Trang 57

❖ Programming with functions

▪ Recursive functions

• binomial numbers:

The basics of functions

Trang 58

❖ Programming with functions

▪ Recursive functions

The basics of functions

Trang 59

❖ Programming with functions

▪ Recursive functions

The basics of functions

Trang 60

Chapter II Modular programming in C++

The basics of functions

How to use functions on a more professional level?

Preprocessor directives of C++

Trang 61

How to use functions on a more professional level?

❖ Inline functions

❖ Overloading (redefining) function names

❖ Function templates

Trang 63

❖ Inline functions

How to use functions on a more professional level?

Trang 65

❖ Overloading (redefining) function names

• Different functions can be defined with the same name and

within the same scope but with a different parameter list

How to use functions on a more professional level?

Trang 66

❖ Overloading (redefining) function names

How to use functions on a more professional level?

Trang 68

❖ Function templates

▪ Creating and using function templates

• A template declaration starts with the keyword template,

followed by the parameters of the template enclosed within the

signs and< >

How to use functions on a more professional level?

Trang 69

❖ Function templates

▪ Creating and using function templates

How to use functions on a more professional level?

Trang 70

❖ Function templates

▪ Creating and using function templates

How to use functions on a more professional level?

Trang 71

❖ Function templates

▪ Function template instantiation

• A function template can be instantiated in an explicit way as we

if concrete types are provided in the template line containing theheader of the function:

How to use functions on a more professional level?

Trang 72

❖ Function templates

▪ Function template specialization

How to use functions on a more professional level?

Trang 73

❖ Function templates

▪ Some further function template examples

How to use functions on a more professional level?

Trang 74

❖ Function templates

▪ Some further function template examples

How to use functions on a more professional level?

Trang 75

❖ Function templates

▪ Some further function template examples

How to use functions on a more professional level?

Trang 76

Chapter II Modular programming in C++

The basics of functions

How to use functions on a more professional level?

Preprocessor directives of C++

Trang 77

Namespaces and storage classes

❖ Storage classes of variables

❖ Storage classes of functions

❖ Modular programs in C++

❖ Namespaces

Trang 78

❖ Storage classes of variables

▪ A storage class:

• defines the lifetime or storage duration of a variable,

• determines the place from where the name of a variable can be accessed directly visibility, scope and also determines which– –name designates which variable linkage.–

▪ A storage class (auto register static extern, , , ) can be assigned to variables when they are defined

Namespaces and storage classes

Trang 79

❖ Storage classes of variables

Namespaces and storage classes

Trang 80

❖ Storage classes of variables

▪ Accessibility (scope) and linkage of variables

• In a C++ source code, variables can have one of the following scopes

block

level

A variable of this type is only visible in the block (function block) where it has b defined so its accessibility is local

If a variable is defined on a block level without the storage classes extern and st

it does not have any linkage.

Namespaces and storage classes

file level

A file level variable is only visible in the module containing its declaration

Identifiers having file level scope are those that are declared outside the function

of the module and that are declared with internal linkage using the static storag class.

Trang 81

Variable scopes

Namespaces and storage classes

Trang 82

❖ Storage classes of variables

▪ Lifetime of variables

• A lifetime is a period of program execution where the given variable exists

static lifetime Identifiers having alifetime static or extern storage class have a static

Namespaces and storage classes

Trang 83

❖ Storage classes of variables

▪ Storage classes of block level variables

• Automatic variables: Automatic variables are created when control is passed to their block and they are deleted when that block is exited

Namespaces and storage classes

Trang 84

❖ Storage classes of variables

▪ Storage classes of block level variables

• The register storage class: The register storage class can only beused for automatic local variables and function parameters

Namespaces and storage classes

Trang 85

❖ Storage classes of variables

▪ Storage classes of block level variables

• Local variables with static lifetime

Namespaces and storage classes

Trang 86

❖ Storage classes of variables

▪ Storage classes of file level variables

Namespaces and storage classes

Trang 87

❖ Storage classes of variables

▪ Storage classes of program level variables

Same definitions (only one of them can

double sum;

double sum = 0;

extern double sum = 0;

extern double sum;

int vector[12];

extern int vector[12] = {0};

extern int vector[];extern int vector[12];extern const int size = 7; extern const int size;

Namespaces and storage classes

Trang 88

❖ Storage classes of variables

❖ Storage classes of functions

❖ Modular programs in C++

❖ Namespaces

Namespaces and storage classes

Trang 89

❖ Storage classes of functions

Definitions (only one of them can be used) Prototypes

double GeomMean( double a, double b) {

return sqrt(a*b); }

extern double GeomMean(double a,

double b) { return sqrt(a*b); }

double GeomMean( double, double )

extern double GeomMean(double, double);

static double GeomMean(double a, double

Trang 90

❖ Storage classes of functions

Namespaces and storage classes

Trang 91

❖ Storage classes of functions

• Accessing the compiled C functions from within C++ source: uthe extern "C" declaration

Namespaces and storage classes

Trang 92

❖ Storage classes of variables

❖ Storage classes of functions

❖ Modular programs in C++

❖ Namespaces

Namespaces and storage classes

Trang 93

❖ Modular programs in C++

Namespaces and storage classes

Trang 94

❖ Modular programs in C++

Namespaces and storage classes

Trang 95

❖ Storage classes of variables

❖ Storage classes of functions

❖ Modular programs in C++

❖ Namespaces

Namespaces and storage classes

Trang 96

❖ Namespaces

▪ The default namespaces of C++ and the scope operator

• C++ enclose Standard library elements in the namespace

Namespaces and storage classes

Trang 97

❖ Namespaces

▪ The default namespaces of C++ and the scope operator

Namespaces and storage classes

Trang 99

❖ Namespaces

▪ Creating and using user-defined namespaces

• Accessing the identifiers of a namespace

Directly by using the scope operator: Or by using the directive using namesp

Namespaces and storage classes

Trang 100

❖ Namespaces

▪ Creating and using user-defined namespaces

• Accessing the identifiers of a namespace

Or by providing using -declarations:

Namespaces and storage classes

Trang 101

❖ Namespaces

▪ Creating and using user-defined namespaces

• Nested namespaces, namespace aliases:

Namespaces and storage classes

Ngày đăng: 11/03/2022, 15:21

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN