Chapter I. Basics and data management of C++ (Chapter I) - Trương Công Tuấn

UN
Uni24h
0 lượt tải
224
0

Nội dung tài liệu

Trang 1

TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI KỸ THUẬT LẬP TRÌNH HỆ CƠ ĐIỆN TỬ Programming for Mechatronic Systems Giảng viên: TS. Trương Công Tuấn Đơn vị: Bộ môn Cơ điện tử, Viện Cơ khí 1

Trang 2

Chapter I. Basics and data management of C++ Các khái niệm và quản lý dữ liệu trong C++  1. Creation of C++ programs (tạo chương trình C++)  2. Basic data types, variables and constants (Các kiểu dữ liệu cơ bản, biến và hằng)  3. Basic operations and expressions (Các toán tử và phép tính)  4. Control program structures (Cấu trúc chương trình)  5. Exception handling (Xử lý ngoại lệ)  6. Pointers, references and dynamic management (quản lý con trỏ, tham chiếu, bộ nhớ động) memory  7. Arrays and strings (mảng và chuỗi kí tự)  8. User-defined data types (Các loại dữ liệu tự định nghĩa) 2

Trang 3

1. Khởi tạo chương trình C++  Một số quy tắc quan trọng  The basic elements of the program: the characters of the 7 bit ASCII code table  Character and text constants, as well as remarks may contain characters of any coding 3

Trang 4

1. Khởi tạo chương trình C++  Các quy tắc quan trọng  C++ phân biệt chữ hoa và chữ thường  Cần tránh một số từ keywords của chương trình biên dịch.  Tên cho chương trình bắt đầu bằng chữ cái(hoặc dấu gạch dưới), và gồm các chữ cái, chữ số và dấu gạch dưới ở các vị trí tiếp theo. 4

Trang 5

1. Khởi tạo chương trình C++  The first C++ program in two versions // Circle1.cpp C // Circle2.cpp C++ #include "cstdio" #include "iostream" #include "cmath" #include "cmath" using namespace std; using namespace std; int main() int main() { { const double pi = 3.14159265359; const double pi = 3.14159265359; double radius, area, perimeter; // Reading radius // Reading radius double radius; printf("Radius = "); cout << "Radius = "; scanf("%lf", &radius); cin >> radius; // Calculations // Calculations perimeter = 2 * radius*pi; double perimeter = 2 * radius*pi; area = pow(radius, 2)*pi; double area = pow(radius, 2)*pi; printf("Perimeter: %7.3f\n", cout << "Perimeter: " << perimeter perimeter); << endl; printf("Area: %7.3f\n", area); cout << "Area: " << area << endl; // Waiting for pressing Enter // Waiting for pressing Enter getchar(); cin.get(); getchar(); cin.get(); return 0; return 0; } } 5

Trang 6

1. Khởi tạo chương trình C++  Biên dịch và chạy chương trình C++ Steps of C++ program compilation 6

Trang 7

1. Creation of C++ programs  Structure of C++ programs // C++ preprocessor directives #include <iostream> #define MAX 2012 // in order to reach the standard library names using namespace std; // global declarations and definitions double fv1(int, long); // function prototype const double pi = 3.14159265; // definition // the main() function int main() // function definition { double fv1(int a, long b) /* local declarations and definitions { statements *//* local declarations and definitions return 0; // exit the program statements } */ return a + b; // return from the functions } 7

Trang 8

1. Creation of C++ programs  Structure of C++ programs C++ object-oriented (OO) approach /// Circle3.cpp int main() #include "iostream" { #include "cmath" // Reading radius using namespace std; double radius; cout << "Radius = "; // Class definition cin >> radius; class Circle // Creation and usage of object { Circle double radius; Circle circle(radius); static const double pi; cout << "Perimeter: " << public: circle.Perimeter() << endl; Circle(double r) { radius = r; } cout << "Area: " << circle.Area() << double Perimeter() { return 2 * endl; radius*pi; } // Waiting for pressing Enter double Area() { return pow(radius, 2)*pi; cin.get(); } cin.get(); }; return 0; const double Circle::pi = 3.14159265359; } 8

Trang 9

Chapter I. Basics and data management of C++  1. Creation of C++ programs  2. Basic data types, variables and constants  3. Basic operations and expressions  4. Control program structures  5. Exception handling  6. Pointers, references and dynamic memory management  7. Arrays and strings  8. User-defined data types 9

Trang 10

2. Basic data types, variables and constants  Classification of C++ data types  The data type determines the number of bits they occupy in memory and their interpretation (variable). 10

Trang 12

2. Basic data types, variables and constants  Type modifiers  The signed/unsigned modifier pair: negative numbers or not.  The short/long pair: size of the storage can be fixed to 16 or 32 bits.  The long long modifier: 64bits  Type modifiers can also be used as type definitions alone. 12

Trang 13

2. Basic data types, variables and constants  Type modifiers char signed char short int short signed short int signed short int signed signed int long int long signed long int signed long long long int long long signed long long int signed long long unsigned char unsigned short int unsigned short unsigned int unsigned unsigned long int unsigned long unsigned long long unsigned long long int Elements in each row designate the same data type. 13

Trang 14

2. Basic data types, variables and constants  Type modifiers Data type Range of values Size Precision Data type Range of values Size Precision (bytes) (digits) (bytes) (digits) bool false, true 1 long - 4 char -128..127 1 2147483648..21474836 47 signed char -128..127 1 unsigned long 0..4294967295 4 unsigned 0..255 1 char long long - 8 9223372036854775808 wchar_t 0..65535 2 .. int - 4 9223372036854775807 2147483648..214748 unsigned long 0..18446744073709551 8 3647 long 615 unsigned int 0..4294967295 4 float 3.4E-38..3.8E+38 4 6 short -32768..32767 2 double 1.7E-308..1.7E+308 8 15 unsigned 0..65535 2 long double 3.4E-4932..3.4E+4932 10 19 short 14

Trang 15

2. Basic data types, variables and constants  Defining variables  Generalized forms 〈storage class〉 〈type qualifier〉 〈type modifier ... 〉 typevariable name 〈= initial value〉 〈, … ;〉 〈storage class〉 〈type qualifier〉 〈type modifier ... 〉 typevariable name 〈(initial value〉 〈 , … ;〉  the 〈 〉 signs indicate optional elements while the three points show that a definition element can be repeated.  The storage classes – auto, register, staticand extern – of C++ determine the lifetime and visibility of variables.  With type qualifiers further information can be assigned to variables.  Variables with const keyword cannot be modified (they are read-only, i.e. constants). 15

Trang 16

2. Basic data types, variables and constants  Defining variables  With type qualifiers further information can be assigned to variables.  The volatile type qualifier indicates that the value of the variable can be modified by a code independent of our program (e.g. by another running process or thread). The word volatile tells the compiler that it is not known in advance what will happen to that variable. (That is why, compilers get the value of the variable from the memory each time a volatile variable is referenced.) int const const double volatile char float volatile const volatile bool 16

Trang 18

2. Basic data types, variables and constants  Basic data types  Character types  Example character C: ’C’ 67 0103 0x43 18

Trang 20

2. Basic data types, variables and constants  Basic data types  Character types  If we want to work with characters of the 8-bit ANSI code table or with a one- byte integer value, it is recommended to use the unsigned char type.  In order to process a character of the Unicode table, the variable should be the two-byte wchar_t type, and constant character values should be preceded by capital letter L. 20

Hiển thị 20 trang đầu. Tải tài liệu để xem đầy đủ.

Bình luận (0)

Đăng nhập để tham gia bình luận

Chưa có bình luận nào. Hãy là người đầu tiên!