Sei sulla pagina 1di 26

Computer Programming

Hung-Yu Wei
Department of Electrical Engineering
National Taiwan University

09/19/2006
Course Information
 Time
 Tuesday 15:30~17:20 新物 111
 Wednesday 13:20-14:10 新物 305
 Textbook
 Deitel & Deitel, "C++ How to Program", 5th
Edition
 全華圖書代理
 Grading
 Midterm exam
 Final exam
 Homework (no late homework)
About me
 Hung-Yu Wei ( 魏宏宇 )
 Office: 電機二館 442 室
 Office hour: by e-mail appointment
 Website:
 https://ceiba.ntu.edu.tw/951cpp
 Download course materials
 Ask Questions?
 E-mail: hywei@ntu.edu.tw
 Subject: [C++] ……
What will you learn in this course?
 Learn the basics of computer
programming
 You don’t need to have any experience
on programming
 C++ programming language
 Course format
 2-hour lecture (Tuesday)
 1-hour laboratory (Wednesday)
Why do I need to know computer
programming?
 Computer programs everywhere
 Physicists use programming
languages
 Use programs to control experiment
instruments
 Run computer simulation to understand
and evaluate models
C++ Compiler
 What is a compiler
 Computer program that translates a
high-level language into machine
language
 Computer only understand machine
langue
 The Compiler we will use
 Visual C++ (Visual Studio .Net 2003)
How to get your Visual C++
 NTU Computer Center
 Buy the Visual Studio CD set
 Download and burn CDs
http://ccms.ntu.edu.tw/~jsc/ntucc/software/
 PCs that you can use
 PCs in the computer center
 Department of Physics (305 PC Room)
Dev C++
 Free C++ compiler
 Download
 http://www.bloodshed.net/devcpp.html
Other (free) compilers
 GNU C++ compiler
 g++
 Platform
 Linux
 Cygwin: Linux-link environment in Windows
 Editor
 emac / vi / or any other editors
 Debugging
 gdb
Brief history of C/C++
 C
 Evolve from B
 Dennis Ritchie, Bell Labs, early 1973
 Structured programming
 functions/procedures
 C++
 Bjarne Stroustrup, Bell Labs, 1983
 Object-oriented programming language
 Why C++?
 It is fast
 It is widely used (portable to other computers)
 Object-oriented
Run a C++
Program
 Editor
 Write a program
 Preprocessor
 Check the program
 Compiler
 Create object code
 Linker
 Link object code with
libraries
 Loader
 Load program into
memory
 execution
Syntax and syntax error
 Syntax
 Grammar of a programming language
 Syntax error
 Wrong grammar!
 Computer does not understand it
Your first C++ program

1 // Fig. 2.1: fig02_01.cpp


2 // Text-printing program.
3 #include <iostream> // allows program to output data to the screen
4
5 // function main begins program execution
6 int main()
7 {
8 std::cout << "Welcome to C++!\n"; // display message
9
10 return 0; // indicate that program ended successfully
11

12 } // end function main


Comments (Line 1 & 2)
 At the beginning of a program
 Comments to describe the program
 Author
 Date/time
 Copyright information
 Descriptions of the program
 Purpose
 How to use this program
Add an 1-line comment
 //
 One-line comment
 Syntax
// your comments here
 Example
// Fig. 2.1: fig02_01.cpp
// Text-printing program.
Add multiple lines of comments
 /* */
 Multiple-line comment
 Syntax
/* first line of your comments
second line of your comments
third line of your comments */
 Example
/* Fig. 2.1: fig02_01.cpp
Text-printing program. */
1 // Fig. 2.1: fig02_01.cpp
2 // Text-printing program.
3 #include <iostream> // allows program to output data to the screen
4
5 // function main begins program execution
6 int main()
7 {
8 std::cout << "Welcome to C++!\n"; // display message
9
10 return 0; // indicate that program ended successfully
11

12 } // end function main


Include a header file (Line 3)
 #
 Notify the “preprocessor” before compiling
 Header file
 C++ has some built-in library
 We need to “include” the library before using it
 #include <iostream>
 <iostream> is used to input data or output data
 Always include <iostream> if you want to input
data or output data in a program
Beginning of the main function
(Line 6)
 int main
 Main program in every C++ program
 int
 Integer
 Main program returns an interger
 {
 Begin of a function
Display on the screen (Line 8)
 Std::cout << “Welcome to C++!\n”;
 Display “Welcome to C++!” on screen
 Std:: cout
 Output text on screen
 From <iostream>
 “”
 “ this is a string”
 \n
 Change a line
 ;
 Every statement ends with “;”
Output a special character
std::cout << “Welcome to C++!\n”;

Escape Description
sequence
\n Newline. Position the screen cursor to the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
Carriage return. Position the screen cursor to the beginning of the current
\r
line; do not advance to the next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\' Single quote. Use to print a single quote character.
\" Double quote. Used to print a double quote character.
Return (Line 10)
 Return 0;
 Indicate the main program ended
successfully
 Include this line in every main program
End of the main function (Line 12)
 }
 End of the main function
 Syntax of the main function
int main()
{
this is your program

}
1 // Fig. 2.1: fig02_01.cpp
2 // Text-printing program.
3 #include <iostream> // allows program to output data to the screen
4
5 // function main begins program execution
6 int main()
7 {
8 std::cout << "Welcome to C++!\n"; // display message
9
10 return 0; // indicate that program ended successfully
11

12 } // end function main


Programming with a style
 Use comments
 Other people cannot understand your
codes without comments
 You may forget what you wrote before 
 Use space between lines
 Use tab between sections
What have we learned today?
 Course logistics
 https://ceiba.ntu.edu.tw/951cpp
 Textbook
 Deitel & Deitel, "C++ How to Program"
 Introduction
 Your first C++ program
 Beginning of Chapter 2 (2.1~2.2)
 Things to do in this week
 Get your textbook
 Get your Visual C++ (or Dev C++)
 Start reading Chapter 2
 (optional) read Chapter 1