Chapter 1 Introduction to Computers, Programs, and C++ §1.1 Computer and Program §1.2 Computer Programming §1.3 Introduction to C++ Computer Is Everywhere Computer: -- machine to manipulate data according to instructions. 2 Components of Computers Bus Storage Devices e.g., Disk, CD, and Tape 3 Memory CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer Memory(内存) • Memory is to store data and program instructions for CPU to execute. • A program and its data must be brought to memory before they can be executed. • A memory unit is an ordered sequence of bytes, each holds eight bits. • Memory is volatile • Data is lost when the power is off Bus Storage Devices e.g., Disk, CD, and Tape 4 Memory CPU Communication Devices Input Devices Output Devices e.g., Modem, and NIC e.g., Keyboard, Mouse e.g., Monitor, Printer How Data Stored? • Data: numbers, characters, Memory address Memory content and strings… . . • Computers use “0” and “1” to . . represent all data . . • Encoding and decoding 5 – Data binary code – automatically done by the computer 2000 01001010 Encoding for character ‘J’ 2001 01100001 Encoding for character ‘a’ – The encoding scheme varies. 2002 01110110 Encoding for character ‘v’ • A byte (字节), of 8 bits (位), is the minimum storage unit 2003 01100001 Encoding for character ‘a’ 2004 00000011 Encoding for number 3 Number Systems (数制) binary 0, 1 octal 0, 1, 2, 3, 4, 5, 6, 7 decimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 hexdecimal 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F 6 Computer Program (计算机程序) • Computer programs – known as software, are instructions to the computer. – You tell a computer what to do through programs. – Programs are written using programming languages. Catch the ball! Come back! 7 §1.2 Computer Programming (编程) • The process of writing, testing, debugging, and maintaining the source code of computer programs • Programming Procedure – Problem-solving phase • Problem analysis and specification • General solution (algorithm) • Verification – Implementation phase • Concrete solution (program) • Debugging and test 8 Program vs. Algorithm (算法) • Program: – A sequence of instructions written to perform a specified task with a computer • Algorithm: – an effective method expressed as a finite list of well-defined instructions for calculating a function Step 1: …. Step 2: …. … 9 An Example Algorithm • Problem: – The greatest common divisor/factor of two integers • Euclid’s algorithm: (1599,650) 10 Pseudo-code (伪代码) 1) Input a and b; 2) If b=0 then a is the answer; 1) Input a, b; 3) Repeat: if a > b then let a = a-b, 2) If b=0 then GOTO 8) ; otherwise, let b = b-a; 3) If a > b then GOTO 6); <variable> = <expression> Until b = 0; 4) Let b=b-a; IF <condition> 5) The result is a; 5) GOTO DO 2) action; ; ELSE 6) End. 6) Let a=a-b; DO next action; WHILE <condition> 7) GOTO 2); DO action; 8) Printfora;<variable> from <first value> to <last value> by <step> DO action with variable; 9) ENDfunction <function name>(<arguments>) 11 DO action with arguments; return something; <function name>(<arguments>) // Function call Flowchart (流程图) 12 Commonly Used Symbols • Start and End • Arrow (flow of control) • General process • Input/Output • Conditional or Decision 13 Programming Language • An artificial language to express program • Definition of languages – Syntax: the surface form • Lexical structure: regular expressions • Grammatical structure: Backus–Naur Form 整数 ::= 无符号整数 ::= 正负号 ::= 数字 ::= [正负号] 无符号整数 数字 { 数字 } +|0|1|…|9 expression ::= atom | list atom ::= number | symbol number ::= [+-]?['0'-'9']+ symbol ::= ['A'-'Z''a'-'z'].* list ::= '(' expression* ')' – Semantics: the meaning of statements 14 Language Types/History High-Level Language Assembly Language Machine Language 15 Machine Language (机器语言) • Machine language is a set of primitive instructions built into every computer. • The instructions are in the form of binary code, so you have to enter binary codes for various instructions. • Program with native machine language is a tedious process. • Moreover the programs are highly difficult to read and modify. • For example, to add two numbers, you might write an instruction in binary like this: 1101101010011010 16 Assembly Language (汇编语言) • A symbolic representation of the numeric machine codes • assembly code: instruction • CPU architecture dependent • Assembler, also a program, is used to convert programs into machine code. • For example, to add two numbers: ADDF3 R1, R2, R3 Assembly Source File … ADDF3 R1, R2, R3 … 17 Machine Code File Assembler … 1101101010011010 … High-level Languages(高级语言) • Languages with natural language elements • Be easier to use • Be more portable across platforms. – hide the details of CPU operations such as memory access models • For example: area = 5 * 5 * 3.1415; • Compiled vs. interpreted – BASIC, Perl, … – C, Java, … 18 Programming Paradigm • Imperative programming (命令式编程) Describes computation in terms of statements that change a program state. Describing how to go about accomplishing a task. – Procedural programming – Object-oriented programming • Declarative programming (声明式编程) Describing what the program should accomplish. – Functional Programming – Logic programming –… 19 Compiling vs. Interpreting (编译语言) (解释语言) • To “translate” the whole program AND THEN execute it – Faster and independent execution • To interpret the program and execute the instructions IMMEDIATELY – Easier to programming 20 Popular Languages • COBOL: mainly for in business, finance, and administrative systems for companies and governments • FORTRAN: mainly for scientific computing • BASIC: Beginner All-purpose Symbolic Instructional Code – Visual Basic: Basic-like visual language developed by Microsoft • Pascal: generic language – Delphi: Pascal-like visual language developed by Borland • • • • • 21 Ada: good for embedded and real-time systems C: generic language, strong at system programming C++: an object-oriented language, based on C Java: C++ like language, Cross-platform (multi-platform) Script languages: Perl, Python, etc. §1.3 Introduction to C++ • C, C++, Java, and C# are very similar. – C++ evolved from C. – Java was modeled after C++. – C# is a subset of C++ with some features similar to Java. • C language B language BCPL language – BCPL was developed by Martin Richards in the mid-1960s for writing operating systems and compilers. • C++ C – Developed by Bjarne Stroustrup at Bell Labs during 1983-1985. • C++ added a number of features that improved the C language. – Most importantly, it added the class. • An international standard for C++ was created by American National Standards Institute (ANSI) in 1998. 22 A Simple C++ Program #include <iostream> int main() { // Display Welcome to C++ to the console std::cout << "Welcome to C++!" << std::endl; return 0; } Welcome 23 Run Extending the Simple C++ Program #include <iostream> int main() { std::cout << "Welcome to C++!" << std::endl; std::cout << "Welcome to Visual C++!" << std::endl; std::cout << "Welcome to C++ Compiler!" << std::endl; return 0; } Welcome 24 Run Compiling Source Code • Source code: a program written in a high-level language • Compiler: a program used to translate the source program into an object program, a machine language program. • The object program is often then linked with other supporting library code before execution. Source File 25 Compiler Object File Linker Excutable File Procedure of Developing a C++ Program Create/Modify Source Code Source code (developed by the programm er) #in clu de <i os tre am > int ma in () { / / D is pla y Wel co me to C+ + to th e c on sol e s td: :c out < < " We lco me to C ++! " << st d:: en dl ; r etu rn 0; } Saved on the disk Source Code Compiler If com pilation errors stored on the disk An obj ect file (e.g., Welcom e.obj ) is created. Machine Code program Linker stored on the disk An executable file (e.g., Welcom e.exe) is created. Executable Code Run Executable Code e.g., Welco me Result If runtime errors or incorrect result 26 C++ Programming Tools • Command line tools – Text editor + compiler • Visual tools – C++ builder – Visual C++ – Dev-C++ etc. 27 Summary • Basic concepts related to computer programming – Computer, program, programming • Computer programming techniques – Programming languages – Programming Paradigms • Introduction to C++ 28