Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 1 Introduction to Computers and C++ Programming.

Similar presentations


Presentation on theme: "Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 1 Introduction to Computers and C++ Programming."— Presentation transcript:

1 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 1 Introduction to Computers and C++ Programming

2 Our textbook

3 The Slides These will be posted and left on the web for review purposes. These will be posted and left on the web for review purposes. The slides are NOT a substitute for class attendance and participation. The slides are NOT a substitute for class attendance and participation. Many of these slides are provided by the author of the text. I have frequently revised or added information to existing slides as well as created new slides. I have frequently revised or added information to existing slides as well as created new slides.

4 Slide 1- 4 Overview - We'll cover this rather quickly 1.1 Computer Systems -review this in the text on your own as it was covered in 10051 and is not essential for this course. 1.2 Programming and Problem Solving 1.3 Introduction to C++ 1.4 Testing and Debugging

5 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.2 Programming and Problem- Solving

6 Slide 1- 6 Algorithms Algorithm A sequence of precise instructions that leads to a solution A sequence of precise instructions that leads to a solutionProgram An algorithm expressed in a language the computer can understand An algorithm expressed in a language the computer can understand

7 Slide 1- 7 Program Design Programming is a creative process No complete set of rules for creating a program exist. No complete set of rules for creating a program exist. Program Design Process Problem Solving Phase Problem Solving Phase Result is an algorithm that solves the problem Implementation Phase Implementation Phase Result is the algorithm is translated into a programming language

8 Slide 1- 8 Problem Solving Phase Be certain the task is completely specified What is the input? What is the input? What information is in the output? What information is in the output? How is the output organized? How is the output organized? Develop the algorithm before implementation Experience shows this saves time in getting your program to run. Experience shows this saves time in getting your program to run. Test the algorithm for correctness Test the algorithm for correctness

9 Translate the algorithm into a programming language Easier as you gain experience with the language Easier as you gain experience with the language Compile the source code Locates errors in using the programming language Locates errors in using the programming language Run the program on sample data Verify correctness of results Verify correctness of results Results may require modification of the algorithm and program Slide 1- 9 Implementation Phase

10

11 Slide 1- 11 Object Oriented Programming Abbreviated OOP Used for many modern programs Program is viewed as interacting objects Each object contains algorithms to describe its behavior Each object contains algorithms to describe its behavior Program design phase involves designing objects and their algorithms Program design phase involves designing objects and their algorithms

12 Slide 1- 12 OOP Characteristics Encapsulation Information hiding Information hiding Objects contain their own data and algorithms Objects contain their own data and algorithmsInheritance Writing reusable code Writing reusable code Objects can inherit characteristics from other objects Objects can inherit characteristics from other objectsPolymorphism A single name can have multiple meanings depending on its context A single name can have multiple meanings depending on its context

13 Slide 1- 13 Software Life Cycle Analysis and specification of the task (problem definition) Design of the software (object and algorithm design) Implementation (coding) Maintenance and evolution of the system Obsolescence

14 Slide 1- 14 Section 1.2 Conclusion Can you… Describe the first step to take when creating a program? Describe the first step to take when creating a program? List the two main phases of the program design process? List the two main phases of the program design process? Explain the importance of the problem-solving phase? Explain the importance of the problem-solving phase? List the steps in the software life cycle? List the steps in the software life cycle?

15 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.3 Introduction to C++

16 Slide 1- 16 Introduction to C++ Where did C++ come from? Derived from the C language Derived from the C language C was derived from the B language C was derived from the B language B was derived from the BCPL language B was derived from the BCPL language Why the ‘++’? ++ is an operator in C++ and results in a cute pun ++ is an operator in C++ and results in a cute pun

17 Slide 1- 17 C++ History C developed by Dennis Ritchie at AT&T Bell Labs in the 1970s. Used to maintain UNIX systems Used to maintain UNIX systems Many commercial applications written in C Many commercial applications written in C C++ developed by Bjarne Stroustrup at AT&T Bell Labs in the 1980s. Overcame several shortcomings of C Overcame several shortcomings of C Incorporated object oriented programming Incorporated object oriented programming C remains a subset of C++ C remains a subset of C++

18 A simple C++ program begins this way #include using namespace std; int main() { And ends this way return 0; } Slide 1- 18 A Sample C++ Program

19 Note: In order to fit the code comfortably on a few number of slides, I won't always add comments. This does NOT mean that you should do that also!!

20 <==== Optional

21

22 Slide 1- 22 Explanation of code (1/5) Variable declaration line int number_of_pods, peas_per_pod, total_peas; Identifies names of three variables to name numbers Identifies names of three variables to name numbers int means that the variables represent integers int means that the variables represent integers

23 Slide 1- 23 Explanation of code (2/5) Program statement cout << “Press return after entering a number.\n”; cout (see-out) used for output to the monitor cout (see-out) used for output to the monitor “<<“ inserts “Press…a number.\n” in the data bound for the monitor “<<“ inserts “Press…a number.\n” in the data bound for the monitor Think of cout as a name for the monitor Think of cout as a name for the monitor “<<“ points to where the data is to end up ‘\n’ causes a new line to be started on the monitor ‘\n’ causes a new line to be started on the monitor

24 Slide 1- 24 Explanation of code (3/5) Program statement cin >> number_of_pods; cin (see-in) used for input from the keyboard cin (see-in) used for input from the keyboard “>>” extracts data from the keyboard “>>” extracts data from the keyboard Think of cin as a name for the keyboard Think of cin as a name for the keyboard “>>” points from the keyboard to a variable where the data is stored

25 Slide 1- 25 Explanation of code (4/5) Program statement total_peas = number_of_pods * peas_per_pod; Performs a computation Performs a computation ‘*’ is used for multiplication ‘*’ is used for multiplication ‘=‘ causes total_peas to get a new value based on the calculation shown on the right of the equal sign ‘=‘ causes total_peas to get a new value based on the calculation shown on the right of the equal sign

26 Slide 1- 26 Explanation of code (5/5) Program statement cout << number_of_pods; Sends the value of variable number_of_pods to the monitor Sends the value of variable number_of_pods to the monitor

27 Slide 1- 27

28 Slide 1- 28 Program Layout (1/3) Compiler accepts almost any pattern of line breaks and indentation Programmers format programs so they are easy to read Place opening brace ‘{‘ and closing brace ‘}’ on a line by themselves Place opening brace ‘{‘ and closing brace ‘}’ on a line by themselves Indent statements Indent statements Use only one statement per line Use only one statement per line

29 Slide 1- 29 Program Layout (2/3) Variables are declared before they are used Typically variables are declared at the beginning of the program Typically variables are declared at the beginning of the program Statements (not always lines) end with a semi-colon Statements (not always lines) end with a semi-colon Include Directives #include Include Directives #include Tells compiler where to find information about items used in the program Tells compiler where to find information about items used in the program iostream is a library containing definitions of cin and cout iostream is a library containing definitions of cin and cout

30 Slide 1- 30 Program Layout (3/3) using namespace std; Tells the compiler to use names in iostream in a “standard” way Tells the compiler to use names in iostream in a “standard” way To begin the main function of the program To begin the main function of the program int main() { To end the main function return 0; } Main function ends with a return statement Main function ends with a return statement

31 Slide 1- 31 Running a C++ Program C++ source code is written with a text editor The compiler on your system converts source code to object code. The linker combines all the object code into an executable program.

32 Use ssh to connect to the computer on which you have an account. I will use putty for ssh and loki.cs.kent.edu. In the lab you will use a similar version of ssh. However, I want to have be able to set the font larger than that one allows. If you want putty for your machine, go to http://www.chiark.greenend.org.uk/~sgtatham/putty/ Choose downloads and download PuTTY (the Telnet and SSH client itself) Slide 1- 32 Running a C++ Program

33 Make ssh Selections

34 Optional Selection to Increase Font Size

35 Press OK and then Open to obtain a window in loki.cs.kent.edu Login with your departmental username and password.

36 Create a Subdirectory and Move There create cs1 move to cs1 check path to cs1 list contents of cs1 currently empty

37 Code We Will Run Use vim as the Editor

38 Invoke vim

39 Type i to start the insertion of code

40 To Save and Exit push ESC Then shift : Then wq for write and quit

41 testing.cpp is written and is listed in the current directory

42 Compile and Run compile executable file for testing run testing.cpp Output is correct list contents of diretory

43 To Use a Name Other Than a.out for the Executable

44 Slide 1- 44 Section 1.3 Conclusion Can you… Describe the output of this line? cout << “C++ is easy to understand.”; Describe the output of this line? cout << “C++ is easy to understand.”; Explain what this line does? cin >> peas_per_pod; Explain what this line does? cin >> peas_per_pod; Explain this? #include Explain this? #include

45 Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.4 Testing and Debugging

46 Slide 1- 46 Testing and Debugging Bug A mistake in a program A mistake in a programDebugging Eliminating mistakes in programs Eliminating mistakes in programs Term used when a moth caused a failed relay on the Harvard Mark 1 computer. Grace Hopper and other programmers taped the moth in logbook stating: “First actual case of a bug being found.” Term used when a moth caused a failed relay on the Harvard Mark 1 computer. Grace Hopper and other programmers taped the moth in logbook stating: “First actual case of a bug being found.”

47 Slide 1- 47 Program Errors Syntax errors Violation of the grammar rules of the language Violation of the grammar rules of the language Discovered by the compiler Discovered by the compiler Error messages may not always show correct location of errors Run-time errors Error conditions detected by the computer at run-time Error conditions detected by the computer at run-time Logic errors Errors in the program’s algorithm Errors in the program’s algorithm Most difficult to diagnose Most difficult to diagnose Computer does not recognize an error Computer does not recognize an error

48 Slide 1- 48 Section 1-4 Conclusion Can you… Describe the three kinds of program errors? Describe the three kinds of program errors? Tell what kind of errors the compiler catches? Tell what kind of errors the compiler catches? What kind of error is produced if you forget a punctuation symbol such as a semi-colon? What kind of error is produced if you forget a punctuation symbol such as a semi-colon? Tell what type of error is produced when a program runs but produces incorrect results? Tell what type of error is produced when a program runs but produces incorrect results?

49 In the Lab You Will Learn About the Following ssh ssh The file structure for UNIX/Linux Simple UNIX/Linux commands - Simple UNIX/Linux commands - lscatmkdirdelrmdirpwd cdcpmvmanmoreg++ gdbexitapropos Using the editor vim Using g++ and the debugger gdb How to submit programs


Download ppt "Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 1 Introduction to Computers and C++ Programming."

Similar presentations


Ads by Google