Mechatronic Systems Programming in C++

Péter Tamás

Antal Huba

József Gräff

A tananyag a TÁMOP-4.1.2.A/1-11/1-2011-0042 azonosító számú „ Mechatronikai mérnök MSc tananyagfejlesztés ” projekt keretében készült. A tananyagfejlesztés az Európai Unió támogatásával és az Európai Szociális Alap társfinanszírozásával valósult meg.

Manuscript completed: February 2014

Language reviewed by: Ágoston Nagy

Published by: BME MOGI

Editor by: BME MOGI

2014


Table of Contents
I. Basics and data management of C++
I.1. Creation of C++ programs
I.1.1. Some important rules
I.1.2. The first C++ program in two versions
I.1.3. Compilation and running of C++ programs
I.1.4. Structure of C++ programs
I.2. Basic data types, variables and constants
I.2.1. Classification of C++ data types
I.2.1.1. Type modifiers
I.2.2. Defining variables
I.2.2.1. Initial values of variables
I.2.3. Basic data types
I.2.3.1. Character types
I.2.3.2. Logical Boolean type
I.2.3.3. Integer types
I.2.3.4. Floating point types
I.2.3.5. enum type
I.2.3.6. sizeof operation
I.2.4. Creation of alias type names
I.2.5. Constants in language C++
I.3. Basic operations and expressions
I.3.1. Classification of operators based on the number of operands
I.3.2. Precedence and grouping rules
I.3.2.1. Rule of precedence
I.3.2.2. Rule of associativity
I.3.3. Mathematical expressions
I.3.3.1. Arithmetical operators
I.3.3.2. Mathematical functions
I.3.4. Assignment
I.3.4.1. Left value and right value
I.3.4.2. Side effects in evaluation
I.3.4.3. I.3.4.3 Assignment operators
I.3.5. Increment and decrement operations
I.3.6. Phrasing of conditions
I.3.6.1. Relational and equalityoperations
I.3.6.2. Logical operations
I.3.6.3. Short circuit evaluation
I.3.6.4. Conditional operator
I.3.7. Bit operations
I.3.7.1. Bitwise logical operations
I.3.7.2. Bit shift operations
I.3.7.3. Bit operations in compound assigment
I.3.8. Comma operator
I.3.9. Type conversions
I.3.9.1. Implicit type conversions
I.3.9.2. Explicit type conversions
I.4. Control program structures
I.4.1. Empty statements and statement blocks
I.4.2. Selective structures
I.4.2.1. if statements
I.4.2.2. switch statements
I.4.3. Iteration structures (loops)
I.4.3.1. while loops
I.4.3.2. for loops
I.4.3.3. do-while loops
I.4.3.4. break statements in loops
I.4.3.5. continue statements
I.4.4. goto statements
I.5. Exception handling
I.5.1. The try – catch program structure
I.5.2. Provoking exceptions - the throw statement
I.5.3. Filtering exceptions
I.5.4. Nested exceptions
I.6. Pointers, references and dynamic memory management
I.6.1. Pointers
I.6.1.1. Single indirection pointers
I.6.1.2. Pointer arithmetic
I.6.1.3. void * type general pointers
I.6.1.4. Multiple indirection pointers
I.6.1.5. Constant pointers
I.6.2. References
I.6.3. Dynamic memory management
I.6.3.1. Allocating and accessing heap memory
I.6.3.2. Deallocating allocated memory
I.7. Arrays and strings
I.7.1. C++ array types
I.7.1.1. One-dimensional arrays
I.7.1.1.1. Initializing and assigning values to one-dimensional arrays
I.7.1.1.2. One-dimensional arrays and the  typedef
I.7.1.2. Two-dimensional arrays
I.7.1.3. Variable-length arrays
I.7.1.4. The relationship between pointers and arrays
I.7.2. Dynamically allocated arrays
I.7.2.1. One-dimensional dynamic arrays
I.7.2.2. Two-dimensional dynamic arrays
I.7.3. The usage of the vector type
I.7.3.1. One-dimensional arrays in vectors
I.7.3.2. Two-dimensional arrays in vectors
I.7.4. Handling C-style strings
I.7.4.1. Strings in one-dimensional arrays
I.7.4.2. Strings and pointers
I.7.4.3. Using string arrays
I.7.5. The string type
I.8. User-defined data types
I.8.1. The structure type
I.8.1.1. Structure type and structure variables
I.8.1.2. Accessing the data members of structures
I.8.1.3. Nested structures
I.8.1.4. Structures and arrays
I.8.1.4.1. Arrays as data members of a structure
I.8.1.4.2. Structures as array elements
I.8.1.5. Creating singly linked lists
I.8.2. The class type
I.8.3. The union type
I.8.3.1. Anonymous unions
I.8.4. Bit fields
II. Modular programming in C++
II.1. The basics of functions
II.1.1. Defining, calling and declaring functions
II.1.2. The return value of functions
II.1.3. Parametrizing functions
II.1.3.1. Parameter passing methods
II.1.3.1.1. Passing parameters by value
II.1.3.1.2. Passing parameters by reference
II.1.3.2. Using parameters of different types
II.1.3.2.1. Arithmetic type parameters
II.1.3.2.2. User-defined type parameters
II.1.3.2.3. Passing arrays to functions
II.1.3.2.4. String arguments
II.1.3.2.5. Functions as arguments
II.1.3.2.5.1. Function types and typedef
II.1.3.2.5.2. Pointers to functions
II.1.3.2.5.3. Examples for pointers to functions
II.1.3.2.6. Default arguments
II.1.3.2.7. Variable length argument list
II.1.3.2.8. Parameters and return value of the main() function
II.1.4. Programming with functions
II.1.4.1. Exchanging data between functions using global variables
II.1.4.2. Exchanging data between functions using parameters
II.1.4.3. Implementing a simple menu driven program structure
II.1.4.4. Recursive functions
II.2. How to use functions on a more professional level?
II.2.1. Inline functions
II.2.2. Overloading (redefining) function names
II.2.3. Function templates
II.2.3.1. Creating and using function templates
II.2.3.2. Function template instantiation
II.2.3.3. Function template specialization
II.2.3.4. Some further function template examples
II.3. Namespaces and storage classes
II.3.1. Storage classes of variables
II.3.1.1. Accessibility (scope) and linkage of variables
II.3.1.2. Lifetime of variables
II.3.1.3. Storage classes of block level variables
II.3.1.3.1. Automatic variables
II.3.1.3.2. The register storage class
II.3.1.3.3. Local variables with static lifetime
II.3.1.4. Storage classes of file level variables
II.3.1.5. Storage classes of program level variables
II.3.2. Storage classes of functions
II.3.2.1. Accessing the compiled C functions from within C++ source
II.3.3. Modular programs in C++
II.3.4. Namespaces
II.3.4.1. The default namespaces of C++ and the scope operator
II.3.4.2. Creating and using user-defined namespaces
II.3.4.2.1. Creating namespaces
II.3.4.2.2. Accessing the identifiers of a namespace
II.3.4.2.3. Nested namespaces, namespace aliases
II.3.4.2.4. Anonymous namespaces
II.4. Preprocessor directives of C++
II.4.1. Including files
II.4.2. Conditional compilation
II.4.3. Using macros
II.4.3.1. Symbolic constants
II.4.3.2. Parameterized macros
II.4.3.3. Undefining a macro
II.4.3.4. Macro operators
II.4.3.5. Predefined macros
II.4.3.6. #line, #error and #pragma directives
III. Object-oriented programming in C++
III.1. Introduction to the object-oriented world
III.1.1. Basics
III.1.2. Basic principles
III.1.2.1. Encapsulation, data hiding
III.1.2.2. Inheritance
III.1.2.3. Abstraction
III.1.2.4. Polymorphism
III.1.3. An object-oriented example code
III.2. Classes and objects
III.2.1. From structures to classes
III.2.1.1. A little revision
III.2.1.2. Grouping together data and operations
III.2.1.3. Data hiding
III.2.1.4. Constructors
III.2.1.4.1. Using member initialisation lists
III.2.1.4.2. Explicit initialisation of objects
III.2.1.5. Destructor
III.2.1.6. Objects of a class, the pointer this
III.2.2. More about classes
III.2.2.1. Static class members
III.2.2.2. How to structure classes
III.2.2.2.1. Implicit inline member functions
III.2.2.2.2. Class structures in C++/CLI applications
III.2.2.2.3. Storing member functions in separate modules
III.2.2.3. Friend functions and classes
III.2.2.4. What can we also add to classes?
III.2.2.4.1. Constant data members of objects
III.2.2.4.2. Reference type data members
III.2.2.4.3. Data members as objects
III.2.2.5. Pointers to class members
III.2.3. Operator overloading
III.2.3.1. Creating operator functions
III.2.3.2. Using type conversion operator functions
III.2.3.3. Extending classes with input/output operations
III.3. Inheritance (derivation)
III.3.1. Derivation of classes
III.3.2. Initialising base class(es)
III.3.3. Accessing class members in case of inheritance
III.3.3.1. Accessing inherited members
III.3.3.2. The friend relationship in inheritance
III.3.4. Virtual base classes in case of multiple inheritance
III.3.5. Inheritance and/or composition?
III.3.5.1. Reuse with composition
III.3.5.2. Reuse by public inheritance
III.4. Polymorphism
III.4.1. Virtual member functions
III.4.2. Redefining virtual functions
III.4.3. Early and late binding
III.4.3.1. Static early binding
III.4.3.2. Dynamic late binding
III.4.3.3. Virtual method table
III.4.4. Virtual destructors
III.4.5. Abstract classes and interfaces
III.4.6. Run-time type informations in case of classes
III.5. Class templates
III.5.1. A step-be-step tutorial for creating and using class templates
III.5.2. Defining a generic class
III.5.3. Instantiation and specialisation
III.5.4. Value parameters and default template parameters
III.5.5. The "friends" and static data members of a class template
III.5.6. The Standard Template Library (STL) of C++
III.5.6.1. The structure of STL
III.5.6.2. STL and C++ arrays
III.5.6.3. Using STL containers
III.5.6.4. Using STL container adaptors
IV. Programming Microsoft Windows in C++
IV.1. Specialties of CLI, standard C++ and C++/CLI
IV.1.1. Compiling and running native code under Windows
IV.1.2. Problems during developing and using programs in native code
IV.1.3. Platform independence
IV.1.4. Running MSIL code
IV.1.5. Integrated development environment
IV.1.6. Controllers, visual programming
IV.1.7. The .NET framework
IV.1.8. C#
IV.1.9. Extension of C++ to CLI
IV.1.10. Extended data types of C++/CLI
IV.1.11. The predefined reference class: String
IV.1.12. The System::Convert static class
IV.1.13. The reference class of the array implemented with the CLI array template
IV.1.14. C++/CLI: Practical realization in e.g. in the Visual Studio 2008
IV.1.15. The Intellisense embedded help
IV.1.16. Setting the type of a CLR program.
IV.2. The window model and the basic controls
IV.2.1. The Form basic controller
IV.2.2. Often used properties of the Form control
IV.2.3. Events of the Form control
IV.2.4. Updating the status of controls
IV.2.5. Basic controls: Label control
IV.2.6. Basic controls: TextBox control
IV.2.7. Basic controls: Button control
IV.2.8. Controls used for logical values: CheckBox
IV.2.9. Controls used for logical values: RadioButton
IV.2.10. Container object control: GroupBox
IV.2.11. Controls inputting discrete values: HscrollBar and VscrollBar
IV.2.12. Control inputting integer numbers: NumericUpDown
IV.2.13. Controls with the ability to choose from several objects: ListBox and ComboBox
IV.2.14. Control showing the status of progressing: ProgressBar
IV.2.15. Control with the ability to visualize PixelGrapic images: PictureBox
IV.2.16. Menu bar at the top of our window: MenuStrip control
IV.2.17. The ContextMenuStrip control which is invisible in basic mode
IV.2.18. The menu bar of the toolkit: the control ToolStrip
IV.2.19. The status bar appearing at the bottom of the window, the StatusStrip control
IV.2.20. Dialog windows helping file usage: OpenFileDialog, SaveFileDialog and FolderBrowserDialog
IV.2.21. The predefined message window: MessageBox
IV.2.22. Control used for timing: Timer
IV.2.23. SerialPort
IV.3. Text and binary files, data streams
IV.3.1. Preparing to handling files
IV.3.2. Methods of the File static class
IV.3.3. The FileStream reference class
IV.3.4. The BinaryReader reference class
IV.3.5. The BinaryWriter reference class
IV.3.6. Processing text files: the StreamReader and StreamWriter reference classes
IV.3.7. The MemoryStream reference class
IV.4. The GDI+
IV.4.1. The usage of GDI+
IV.4.2. Drawing features of GDI
IV.4.3. The Graphics class
IV.4.4. Coordinate systems
IV.4.5. Coordinate transformation
IV.4.6. Color handling of GDI+ (Color)
IV.4.7. Geometric data (Point, Size, Rectangle, GraphicsPath)
IV.4.7.1. Storing dimensions
IV.4.7.2. Storing planar points
IV.4.7.3. Storing planar rectangles
IV.4.7.4. Geometric shapes
IV.4.8. Regions
IV.4.9. Image handling (Image, Bitmap, MetaFile, Icon)
IV.4.10. Brushes
IV.4.11. Pens
IV.4.12. Font, FontFamily
IV.4.13. Drawing routines
IV.4.14. Printing
References:
V. Developing open-source systems
V.1. The structure of Linux systems
V.1.1. History of Unix
V.1.2. The Open Source software development model
V.1.3. The Linux operating system
V.1.4. Linux distributions
V.1.5. X Window System
V.1.6. Embedded Linux
V.2. The GCC compiler
V.2.1. The origins of GCC
V.2.2. Steps of compilation with GCC
V.2.3. Host and Target
V.2.4. The frequently used options of GCC
V.2.5. The make utility
V.2.6. The gdb debugger
V.3. Posix C, C++ system libraries
V.3.1. stdio.h
V.3.2. math.h
V.3.3. stdlib.h
V.3.4. time.h
V.3.5. stdarg.h
V.3.6. string.h
V.3.7. dirent.h
V.3.8. sys/stat.h
V.3.9. unistd.h
VI. Aim-specific applications
VI.1. SOC (System On a Chip)
VI.1.1. What is SOC?
VI.1.2. Parts of a SOC
VI.2. Embedded devices, development environments for PCs
VI.2.1. Atmel: WinAVR and the AVR Studio
VI.2.2. Microchip: MPLAB IDE and MPLAB-X
VI.3. Programming of distributed systems
VI.3.1. CORBA
VI.3.2. Open-source implementations of CORBA
VI.3.3. ICE – internet communication engine
A. Appendix – Standard C++ summary tables
A.1. ASCII code table
A.2. Reserved keywords in C++
A.3. Escape characters
A.4. C++ data types and their range of values
A.5. Statements in C++
A.6. C++ preprocessor directives
A.7. Precedence and associativity of C++ operations
A.8. Some frequently used mathematical functions
A.9. C++ storage classes
A.10. Input/Output (I/O) manipulators
A.11. The standard C++ library header files
B. Appendix – C++/CLI summary tables
B.1. C++/CLI reserved words
B.2. The types of classes and structures in C++/CLI
B.3. Operators for pointers and references in C++/CLI
B.4. The .NET (CTS) and the C++/CLI primitive types
List of Figures
I.1. Project selection
I.2. Project settings
I.3. Possible source files
I.4. Window of the running program
I.5. Steps of C++ program compilation
I.6. Classification of C++ data types
I.7. Functioning of a simple if statement
I.8. Logical representation of if-else structures
I.9. Logical representation of multi-way branches
I.10. Logical representation of while loops
I.11. Logical representation of for loops
I.12. Logical representation of do-while loops
I.13. C++ program memory usage
I.14. Dynamic memory allocation
I.15. Graphical representation of an one-dimensional array
I.16. Graphical representation of a two-dimensional array
I.17. The relationship between pointers and arrays
I.18. Two-dimensional arrays in memory
I.19. Dynamically allocated row vectors
I.20. Dynamically allocated pointer vector and row vectors
I.21. String constant in memory
I.22. String array stored in a two-dimensional array
I.23. Optimally stored string array
I.24. Structure in memory
I.25. Processing data in the program CDCatalogue
I.26. A singly linked list
I.27. Union in memory
I.28. The layout of the structure date in memory
II.1. Function definition
II.2. Steps of calling a function
II.3. Graph of the third degree polynomial
II.4. The interpretation of the parameter argv
II.5. Providing command line arguments
II.6. Calculating the area of a triangle
II.7. Variable scopes
II.8. The compilation process in C++
III.1. The object myCar (an instance of the class Truck)
III.2. Inheritance
III.3. Multiple inheritance
III.4. The class Employee and its objects
III.5. The multiple inheritance of I/O classes in C++
III.6. Hierarchy of geometrical classes
III.7. Using virtual base classes
III.8. Early binding example
III.9. Late binding example
III.10. Virtual method tables of the example code
IV.1. The memory before cleaning
IV.2. The memory after cleaning
IV.3. The window in the View/Designer
IV.4. The program in the View/Code window
IV.5. The Toolbox
IV.6. The Control menu
IV.7. The Properties Window
IV.8. The Event handlers
IV.9. A defined Event Handler
IV.10. The Intellisense window
IV.11. Solution Explorer menu
IV.12. Project properties
IV.13. Part of the program’s window
IV.14. Normal size picturebox on the form
IV.15. Stretched size picturebox on the form
IV.16. Automatic sized picturebox on the form
IV.17. Centered image in the picturebox on the form
IV.18. Zoomed bitmap in the picturebox on the form
IV.19. Menustrip
IV.20. Menuitem on the menustrip
IV.21. The Help menu
IV.22. The submenu
IV.23. The contextmenu
IV.24. Toolkit on toolstrip
IV.25. The MessageBox
IV.26. Binary file processing
IV.27. The classes of GDI+
IV.28. The drawn line automatically appears after every resizing activity.
IV.29. If we do not draw in Paint then the blue line disappears when maximizing after minimizing.
IV.30. General axonometry
IV.31. Isometric axonometry
IV.32. Military axonometry
IV.33. The default coordinate-system on form
IV.34. Cube in axonometry
IV.35. Central projection
IV.36. The perspective views of the cube
IV.37. Setting the distortion
IV.38. The distortion
IV.39. Trasnlating and scaling with a matrix
IV.40. Translation and rotation
IV.41. Trasnlating and shearing
IV.42. The mm scale and the PageScale property
IV.43. Color mixer
IV.44. Alternate and Winding curve chains
IV.45. Elliptical arc
IV.46. Cubic Bezier curve
IV.47. Cubic Bezier curve joined continously
IV.48. The cardinal spline
IV.49. Catmull-Rom spline
IV.50. Text in the figure
IV.51. Two concatenated figures connected and disconnected
IV.52. Widened figure
IV.53. Distorted shape
IV.54. Clipped figure
IV.55. Vectorial A
IV.56. Rasterized A
IV.57. Image on the form
IV.58. Halftone representation
IV.59. Rotated image
IV.60. Coloring bitmap
IV.61. Non-managed bitmap manipulating
IV.62. Brushes
IV.63. Pens
IV.64. Character features
IV.65. Traditional character widths
IV.66. ABC character widths
IV.67. Font families
IV.68. Font distortions
IV.69. Zoomed image and distorted zoomed image
IV.70. The OneNote program is the default printer
VI.1. The block diagram of PIC32MX
VI.2. Possibilities of connecting a RealTek SOC
VI.3. The example program
VI.4. The MPLAB IDE
VI.5. The structure of CORBA
VI.6. The structure of ICE programs
VI.7. The ICE libraries