CS205-2020 Spring - Lecture 1 PDF
CS205-2020 Spring - Lecture 1 PDF
CS205-2020 Spring - Lecture 1 PDF
Language
CS205 Spring
Feng Zheng
2020.02.20
Content
• Brief Biography
http://faculty.sustech.edu.cn/fengzheng/
Services
• Associate Editor:
IET Image Processing
• Journal Reviewer:
IEEE TNNLS, IEEE TCSVT, IEEE TMM, IEEE Access, Neurocomputing,
Information Sciences, IET Computer Vision, IET Image Processing etc..
Office
• Office : Room 602, Building 10, Innovation Park
• Phone: 0755-88015178
• Email : zhengf@sustech.edu.cn
About This Course
Structure
Now
• C related part in C++
pointer (指针)
reference (引用)
C 1-7 Lectures
End of Term
Target Student
• Average ability of programming
Expectations
• Good understanding of C/C++
• Ability to write reasonably complex programs
• Professional attitude and habits
• Programming thinking
Exams test you on
• General knowledge (why) about C/C++
• Being able to tell (read) what a program does
• Ability to write pseudo-code for a moderately complex
algorithm
• Finding errors in a program
Grade Component
Grades
• Assembly language
English-like abbreviations representing elementary computer
operations; Clearer to humans; Incomprehensible to computers
Example: LOAD BASEPAY
High-level Languages
• High-level languages Programmer = translator
Similar to English, use common mathematical notations
Single statements accomplish substantial tasks: Assembly language
requires many instructions to accomplish simple tasks
Translator programs (compilers): Convert to machine language
Interpreter programs: Directly execute it
Example:
grossPay = basePay + overTimePay
https://www.unixmen.com/dennis-m-ritchie-father-c-programming-language/
C Programming Philosophy
• Branching statements
Hard: earlier procedural programming
Easy: structured programming
• Top-down
Divide large tasks into smaller tasks
C Programming Philosophy
• Procedural programming --- Compared to OOP
More algorithms but data
Interaction between procedures
History of C++
• Extension of C
• Early 1980s: Bjarne Stroustrup (Bell Laboratories)
• Provides capabilities for Object-Oriented Programming
Objects: reusable software components: Model items in real world
Object-oriented programs: Easy to understand, correct and modify
• Hybrid language
C-like style
Object-oriented style
C++ Philosophy
• Fit the language to the problem
• A class is a specification describing such a new data form
What data is used to represent an object
The operations that can be performed on that data
• An object is a particular data constructed according to that plan
• Emphasizes the data
• Bottom-up programming
Class definition to program design
Features of C++
• Binding
• Reusable (可重用的)
• Protectability (可保护的)
• Polymorphism (多态性)- multiple definitions for operators and functions
• Inheritance (继承性)
• Portable (可移植性)
Comparison
• Procedural versus Object-oriented (Encapsulated: 封装的)
• Edit
processes the code.
Compiler creates
Compiler Disk object code and stores
it on disk.
• Compile Primary
Memory
file and stores it on disk
• Link
Loader
• Load
..
• Execute
Primary
Memory
CPU
CPU takes each
instruction and
executes it, possibly
storing new data
..
.. values as the program
..
executes.
Creating the Source Code File
• Integrated development environments
Microsoft Visual C++, VSCODE
QT
Apple Xcode
• Any available text editor
Debuggers: GDB: The GNU Project Debugger
Command prompt
Compiler
Proper Extensions
• Suffix
Software Build Process
• Start with C++ source code files (.cpp, .hpp)
• Compile: convert code to object code stored in object file (.o)
• Link: combine contents of one or more object files (and possibly
some libraries) to produce executable program
GNU Compiler Collection (GCC)
C++ Compiler (编译器)
• g++ command provides both compiling and linking functionality
• Command-line usage:
g++ [options] input file . . .
• Compile C++ source file file.cpp to produce object code file file.o:
g++ -c file.cpp
• Link object files file 1.o, file 2.o, . . . to produce executable file
executable_name:
g++ -o executable_name file 1.o file 2.o . . .
• Tools for windows: MinGW, MSYS2, Cygwin, Windows Subsystem
Common g++ Command-Line
Options
• Web site: http://www.gnu.org/software/gcc
• C++ standards support in GCC: https://gcc.gnu.org/projects/cxx-status.html
Windows Compilers
• Choose windows applications: MFC Windows application, dynamic link library, ActiveX
control, DOS or character-mode executable, static library, or console application
• Tools
• Compilers
Setting Out to C++
Content
• Creating a C++ program
• The #include directive
• The main() function
• Placing comments in a C++ program
• Declaring and using variables
• Using the cout object for output
• How and when to use endl
• Using the cin object for input
• Defining and using simple functions
C++ Program Sample
• A program example
Noting: C++ is case sensitive
Preprocessor
Function header
Function body
Return statement
Comments (注释)
• Two styles of comments provided
Comment starts with // and proceeds to end of line
Comment starts with /* and proceeds to first */
• Function header - describe the interface between main() and the operating system
• Otherwise
① A dynamic link library (DLL)
② A controller chip in a robot
C++ Preprocessor (预处理)
• Preprocessor transforms source code, prior to compilation
Preprocessor passes the output to compiler for compilation
Preprocessor behavior can be controlled by directives
Directive occupies single line of code
No semicolon (;\)
• Consists of:
• Example:
Header Filenames
• Reason of using header files
As programs grow larger (and make use of more files), it becomes
increasingly tedious to have to forward declare every function you want
to use that is defined in a different file.
Namespaces
• Reasons of using namesapce
To simplify the writing of large programs
To help organize programs that combine pre-existing code from several
companies
To indicate which product (wanda) you want, using double colon ::
What is the
definition of
type?
Declaration Statements
• Variable: Identify both the storage location and how much
memory space to store an item
Declaration statement: to provide a label for the location and to
indicate the type of storage
Complier: to allocate the memory space
Assignment Statements
• An assignment statement assigns a value to a storage
location
• Assignment operator: =
• Two examples:
Assign serially (from copy)
Arithmetic expression: +-*/ (form CPU)
Assignment: cin
• An object of input istream
>> operator: extract characters from the input stream
The value typed from the keyboard is eventually assigned to the
variable: carrots
Assignment: Called Functions
• The example of called functions
6.25 in parentheses is the input, called an argument or parameter
This example assigns the return value to the variable x
Function prototype of functions
• What a function prototype does for functions is the same to
that a variable declaration does for variables
You can type the function prototype into your source code file yourself
You can include the cmath (math.h on older systems) header file, which
has the prototype in it
• The terminating semicolon in the prototype
Identifies it as a statement
Makes it a prototype instead of a function header
Basic characteristics of functions
• Don’t confuse the function prototype with the function
definition
Function prototype describes the function interface
The definition includes the code for the function’s workings
• It requires a prototype
Review C++ statement types
• After introducing function definition
Declaration statement
Assignment statement
Function call – generally included in expression
Function prototype
Return statement
Summary
• What are the modules of C++ programs called? functions
• What does the preprocessor directive do?
• What does the namespace do?
• What does the header do?
• What is the structure of function?
• Where does the prototype put?
• Where does the program start to run?
……
Summary
• C++ provides two predefined objects: cout, cin
They are objects of the istream and ostream classes, in the iostream file
These classes view input and output as streams of characters
The insertion operator (<<), which is defined for the ostream class, lets you
insert data into the output stream
The extraction operator (>>), which is defined for the istream class, lets you
extract information from the input stream
• C++ can use the extensive set of C library functions
Inclusion statement: #include <cmath>
Power function: pow(double); pow(double, double);
Square root: sqrt(int);
Thanks
zhengf@sustech.edu.cn
http://faculty.sustech.edu.cn/fengzheng/