Download presentation
Presentation is loading. Please wait.
Published byBriana Hicks Modified over 9 years ago
1
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming
2
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 2 Contents 1.3 Programs and Programming Languages 1.4 What Is a Program Made of? 1.5 Input, Processing, and Output 1.6 The Programming Process 1.7 Procedural and Object-Oriented Programming
3
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 3 Software – Programs That Run on a Computer Categories of software: –Operating system: programs that manage the computer hardware and the programs that run on them. Ex: Windows, UNIX, Linux –Application software: programs that provide services to the user. Ex: word processing, games, programs to solve specific problems
4
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 4 1.3 Programs and Programming Languages Program: a set of instructions to a computer to perform a task Programming Language: a language used to write programs
5
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 5 Programs and Programming Languages Types of languages: –Low-level: used for communication with computer hardware directly. Often written in binary machine code (0’s/1’s) directly. –High-level: closer to human language
6
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 6 Table 1-1
7
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 7 From a High-level Program to an Executable File a)Code the program with a text editor or using an IDE. b)Run preprocessor to convert source file directives to source code program statements. c)Run compiler to convert source program statements into machine language instructions. d)Run linker to connect hardware-specific code to machine instructions, producing an executable file. -Steps b–d are often performed by a single command or button click. -Errors detected at any step will prevent execution of following steps.
8
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 8 From a High-level Program to an Executable File Source Code Preprocessor Modified Source Code Compiler Object Code Linker Executable Code
9
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 9 C and C++ Both languages were developed at Bell Labs C was written by Dennis Ritchie in 1972 C++ was written by Bjarne Stroustrup in the early 1980’s C++ extends the C language to permit object- oriented programming
10
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 10 1.4 What Is a Program Made Of? Common elements in programming languages: –Keywords –Programmer-Defined Symbols –Operators –Punctuation –Syntax
11
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 11 Language Elements, Table 1-2
12
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 12 Example Program #include using namespace std; int main() { string name; cout << "What is your name? "; cin >> name; cout << "Hello there, " << name; return 0; }
13
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 13 Program 1-1 // This program calculates gross pay. #include using namespace std; int main() { double hours, rate, pay; cout << “How many hours did you work? ”; cin >> hours; cout << “How much do you get paid per hour? ”; cin >> rate; pay = hours * rate; cout << “You have earned $” << pay << endl; return 0; }
14
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 14 Program Output How many hours did you work? 10 How much do you get paid per hour? 15 You have earned $150
15
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 15 Key Words Also known as reserved words Have a special meaning in C++ Can not be used for another purpose Examples in program: –using, namespace, int
16
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 16 Programmer-Defined Symbols ( Identifiers ) Names made up by the programmer Not part of the C++ language Used to represent various things: –variables (memory locations), functions, etc. Example in program: –name, hours, rate, calcPay()
17
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 17 Operators Used to perform operations on data Many types of operators: Arithmetic: +, -, *, /, % Assignment: = Examples in program: <<stream insertion operator >> stream extraction operator
18
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 18 Punctuation Characters that mark the end of a statement, or that separate items in a list Examples in program: – ;, ~ () []
19
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 19 Syntax The rules of grammar that must be followed when writing a program Controls the use of: –key words –operators –programmer-defined symbols –punctuation
20
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 20 Lines and Statements cout << "How many hours did you work? "; cout << "How many hours " << "did you work? "; There are 2 statements above. The first statement uses one line the second statements spans two lines the semicolon ( ; ) is the statement terminator
21
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 21 Variable Definitions Two types of information: –numbers and characters Numbers may be integers or floating-point The statement below creates three variables in memory named hours, rate, and pay that each can store a floating- point number double hours, rate, pay;
22
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 22 1.5 Input, Processing, and Output Three steps many programs perform: 1)Gather input data: -from keyboard -from files on disk drives 2)Process the input data 3)Generate output: -send it to the screen -write it to a file
23
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 23 1.5 Input, Processing, and Output Output cout << "Enter your hours and rate " << "separated by a space: "; Input: cin >> hours; cin >> rate; Processing: pay = hours * rate; Output cout << “You have earned $” << pay;
24
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 24 1.6 The Programming Process The programming process consists of several steps, which include: –Design –Code –Test –Debug –Document
25
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 25 1.6 The Programming Process 1.Clearly define what the program is to do. Inputs / Processing / Outputs 2.Develop logic using a flowchart or pseudocode. 3.Desk check for logical errors. 4.Enter/Edit the code and compile it. 5.Correct any errors found during compilation. Repeat steps 4 and 5 as many times as necessary.
26
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 26 The Programming Process 6.Run the program with test data for input. 7.Correct any errors found while running the program. Repeat steps 4 through 7 as many times as necessary. 8.Validate the results of the program.
27
4 th Ed. Home Page4 th Ed. Home Page Appendices AppendicesChapter 1 slide 27 1.7 Procedural and Object-Oriented Programming Procedural programming: focus is on the process. Procedures/functions are written to process data. Object-Oriented programming: focus is on objects, which contain data and the means to manipulate the data. Messages are sent to objects to perform operations.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.