Giáo trình tin học Chương I

26 940 2
Giáo trình tin học Chương I

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Giáo trình tin học Chương I

Chapter – Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Basics of a Typical C++ Environment Introduction to C++ Programming A Simple Program: Printing a Line of Text Another Simple Program: Adding Two Integers Arithmetic Decision Making: Equality and Relational Operators Introduction to Object Technology  2003 Prentice Hall, Inc All rights reserved History of C and C++ • History of C – Evolved from two other programming languages • BCPL and B – “Typeless” languages – Dennis Ritchie (Bell Laboratories) • Added data typing, other features – Development language of UNIX – Hardware independent • Portable programs – 1989: ANSI standard – 1990: ANSI and ISO standard published • ANSI/ISO 9899: 1990  2003 Prentice Hall, Inc All rights reserved History of C and C++ • History of C++ – – – – Extension of C Early 1980s: Bjarne Stroustrup (Bell Laboratories) “Spruces up” C 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 • Both  2003 Prentice Hall, Inc All rights reserved C++ Standard Library • C++ programs – Built from pieces called classes and functions • C++ standard library – Rich collections of existing classes and functions • “Building block approach” to creating programs – “Software reuse”  2003 Prentice Hall, Inc All rights reserved Basics of a Typical C++ Environment • C++ systems – Program-development environment – Language – C++ Standard Library  2003 Prentice Hall, Inc All rights reserved Basics of a Typical C++ Environment Phases of C++ Programs: Edit Preprocess Compile Editor Preprocessor Compiler Linker Link Loader Load Disk Execute Disk Program is created in the editor and stored on disk Disk Preprocessor program processes the code Disk Compiler creates object code and stores it on disk Disk Primary Memory Loader puts program in memory Primary Memory CPU  2003 Prentice Hall, Inc All rights reserved Linker links the object code with the libraries, creates a.out and stores it on disk CPU takes each instruction and executes it, possibly storing new data values as the program executes Basics of a Typical C++ Environment • Input/output – cin • Standard input stream • Normally keyboard – cout • Standard output stream • Normally computer screen – cerr • Standard error stream • Display error messages  2003 Prentice Hall, Inc All rights reserved Introduction to C++ Programming • C++ language – Facilitates structured and disciplined approach to computer program design • Following several examples – Illustrate many important features of C++ – Each analyzed one statement at a time • Structured programming • Object-oriented programming  2003 Prentice Hall, Inc All rights reserved A Simple Program: Printing a Line of Text • Comments – – – – Document programs Improve program readability Ignored by compiler Single-line comment • Begin with // • Preprocessor directives – Processed by preprocessor before compiling – Begin with #  2003 Prentice Hall, Inc All rights reserved // Fig 1.2: fig01_02.cpp // A first program in C++ #include 10 11 // function main begins program execution int main() { std::cout > (stream extraction operator) • Used with std::cin • Waits for user to input value, then press Enter (Return) key • Stores value in variable to right of operator – Converts value to variable data type • = (assignment operator) – Assigns value to variable – Binary operator (two operands) – Example: sum = variable1 + variable2;  2003 Prentice Hall, Inc All rights reserved 15 10 11 12 13 14 15 16 17 18 // Fig 1.6: fig01_06.cpp // Addition program #include // function main begins program execution int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored std::cout > integer1; // read an integer std::cout > integer2; // read an integer sum = integer1 + integer2; // assign result to sum std::cout y x is greater than y < < x < y x is less than y ≥ >= x >= y x is greater than or equal to y ≤ num2; // read two integers 13 if ( num1 == num2 ) 14 cout

Ngày đăng: 22/08/2012, 10:12

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan