Chapters 1 & 2 Programming and Programs John Keyser’s Modifications of Slides by Bjarne Stroustrup www.stroustrup.com/Programming.

Slides:



Advertisements
Similar presentations
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Advertisements

Review for Final Exam Chapters 1–20 (Programming and C++ in 75 minutes) Bjarne Stroustrup Ronnie Ward 1Stroustrup/PPP -
Chapters 1 & 2 Programming and Programs Bjarne Stroustrup
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Lecture 1: Overview of Computers & Programming
Introduction to C Programming
Chapters 1 & 2 Programming and Programs
COSC 120 Computer Programming
Chapter 10.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Three types of computer languages
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to a Programming Environment
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages C++ Programming:
C++ fundamentals.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
CHAPTER 1: INTORDUCTION TO C LANGUAGE
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Chapters 1 & 2 Programming and Programs
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Welcome In The World Of ‘C’.  TEXT BOOK: Programming in ANCI ‘C By: E Balagurusamy. TMH  Reference Books: 1) The ‘C Programming Language By: Kernighan.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Lecture 1 Programming and Programs Bjarne Stroustrup modified by Hugh Murrell.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming 1.
Getting Started Lecture 1 Hartmut Kaiser
Instructor: Tina Tian. About me Office: RLC 203A Office Hours: Wednesday 1:30 - 4:30 PM or .
Introduction of C++ language. C++ Predecessors Early high level languages or programming languages were written to address a particular kind of computing.
1 Agenda Administration Background Our first C program Working environment Exercise Memory and Variables.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
Programming With C.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 1 Introduction to Computers and C++ Programming Slides by David B. Teague,
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 1 February 8, 2005.
Guide to Programming with Python Chapter One Getting Started: The Game Over Program.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Beginning C++ Through Game Programming, Second Edition
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Looping and Counting Lecture 3 Hartmut Kaiser
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Chapters 1 & 2 Programming and Programs Bjarne Stroustrup
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Intermediate 2 Computing Unit 2 - Software Development.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
Principles of Programming Chapter 1: Introduction  In this chapter you will learn about:  Overview of Computer Component  Overview of Programming 
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
T01. Outline and concepts Promising future comes from the dedication.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
C++ Programming Chapter 1 Programming & Programs.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapters 1 & 2 Programming and Programs
Lecture 1b- Introduction
Programming what is C++
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Getting Started with C.
Computer Science I CSC 135.
Programming J. Michael Moore CSCE 121.
CSCE , 510, 511, 512 Introduction to Program Design and Concepts Dr. J
Chapters 1 & 2 Programming and Programs
Lecture 1- Introduction
Introduction to Computer Systems
Getting Started Lecture 1 Hartmut Kaiser
Chapter 1 Introduction to Programming
Presentation transcript:

Chapters 1 & 2 Programming and Programs John Keyser’s Modifications of Slides by Bjarne Stroustrup

2 This is a course In Programming In Programming For beginners For beginners who want to become professionals who want to become professionals i.e., people who can produce systems that others will use i.e., people who can produce systems that others will use who are assumed to be bright who are assumed to be bright Though not (necessarily) geniuses Though not (necessarily) geniuses who are willing to work hard who are willing to work hard Though do need sleep occasionally, and take a normal course load Though do need sleep occasionally, and take a normal course load Using the C++ programming language Using the C++ programming language Stroustrup/Programming

3Not! A Washout course A Washout course “If you can get into the science/engineering parts of a university, you can handle this course” “If you can get into the science/engineering parts of a university, you can handle this course” A course in A course in The C++ programming language The C++ programming language For students For students who want to become language lawyers who want to become language lawyers We try not to get bogged down in technical obscurities We try not to get bogged down in technical obscurities who are assumed to be a bit dim and fairly lazy who are assumed to be a bit dim and fairly lazy We try not to spoon feed We try not to spoon feed Using Using Some untested software development methodologies and a lot of unnecessarily long words Some untested software development methodologies and a lot of unnecessarily long words Stroustrup/Programming

4 The Aims Teach/learn Teach/learn Fundamental programming concepts Fundamental programming concepts Key useful techniques Key useful techniques Basic Standard C++ facilities Basic Standard C++ facilities After the course, you’ll be able to After the course, you’ll be able to Write small colloquial C++ programs Write small colloquial C++ programs Read much larger programs Read much larger programs Learn the basics of many other languages by yourself Learn the basics of many other languages by yourself Proceed with an “advanced” C++ programming course Proceed with an “advanced” C++ programming course After the course, you will not (yet) be After the course, you will not (yet) be An expert programmer An expert programmer A C++ language expert A C++ language expert An expert user of advanced libraries An expert user of advanced libraries Stroustrup/Programming

5 The Means Lectures Lectures Attend every one Attend every one Notes/Chapters Notes/Chapters Read a chapter ahead (about one per lecture) Read a chapter ahead (about one per lecture) Read the chapter again after each lecture Read the chapter again after each lecture Feedback is welcome (typos, suggestions, etc.) Feedback is welcome (typos, suggestions, etc.) Stroustrup/Programming

6 The Means (Cont.) Work Work Review questions in chapters Review questions in chapters Review “Terms” in Chapters Review “Terms” in Chapters Drills Drills Always do the drills Always do the drills Always do the drills before the exercises Always do the drills before the exercises Exercises Exercises Course specific Course specific Projects Projects That’s where the most fun and the best learning takes place That’s where the most fun and the best learning takes place Quizzes Quizzes Exams Exams Stroustrup/Programming

7 Cooperate on Learning Except for the work you hand in as individual contributions, we strongly encourage you to collaborate and help each other Except for the work you hand in as individual contributions, we strongly encourage you to collaborate and help each other If in doubt if a collaboration is legitimate: ask! If in doubt if a collaboration is legitimate: ask! Don’t claim to have written code that you copied from others Don’t claim to have written code that you copied from others Don’t give anyone else your code (to hand in for a grade) Don’t give anyone else your code (to hand in for a grade) When you rely on the work of others, explicitly list all of your sources – i.e. give credit to those who did the work When you rely on the work of others, explicitly list all of your sources – i.e. give credit to those who did the work Don’t study alone when you don’t have to Don’t study alone when you don’t have to Form study groups Form study groups Do help each other (without plagiarizing) Do help each other (without plagiarizing) Go to your TA's office hours Go to your TA's office hours Go prepared with questions Go prepared with questions The only stupid questions are the ones you wanted to ask but didn’t The only stupid questions are the ones you wanted to ask but didn’t Stroustrup/Programming

8 Why C++ ? You can’t learn to program without a programming language You can’t learn to program without a programming language The purpose of a programming language is to allow you to express your ideas in code The purpose of a programming language is to allow you to express your ideas in code C++ is the language that most directly allows you to express ideas from the largest number of application areas C++ is the language that most directly allows you to express ideas from the largest number of application areas C++ is the most widely used language in engineering areas C++ is the most widely used language in engineering areas Stroustrup/Programming

9 Why C++ ? C++ is precisely and comprehensively defined by an ISO standard C++ is precisely and comprehensively defined by an ISO standard And that standard is almost universally accepted And that standard is almost universally accepted C++ is available on almost all kinds of computers C++ is available on almost all kinds of computers Programming concepts that you learn using C++ can be used fairly directly in other languages Programming concepts that you learn using C++ can be used fairly directly in other languages Including C, Java, C#, and (less directly) Fortran Including C, Java, C#, and (less directly) Fortran Stroustrup/Programming

10 Rough course outline Part I: The basics Part I: The basics Types, variables, strings, console I/O, computations, errors, vectors functions, source files, classes Types, variables, strings, console I/O, computations, errors, vectors functions, source files, classes Part II: Input and Output Part II: Input and Output File I/O, I/O streams File I/O, I/O streams Graphical output Graphical output Graphical User Interface Graphical User Interface Part III: Data structures and algorithms Part III: Data structures and algorithms Free store, pointers, and arrays Free store, pointers, and arrays Lists, maps, sorting and searching, vectors, templates Lists, maps, sorting and searching, vectors, templates The STL The STL Part IV: Broadening the view Part IV: Broadening the view Software ideals and history Software ideals and history Text processing, numerics, embedded systems programming, testing, C, etc. Text processing, numerics, embedded systems programming, testing, C, etc. Stroustrup/Programming

11 Rough course outline (Cont.) Throughout Throughout Program design and development techniques Program design and development techniques C++ language features C++ language features Background and related fields, topics, and languages Background and related fields, topics, and languages Note: Appendices Note: Appendices C++ language summary C++ language summary C++ standard library summary C++ standard library summary Index (extensive) Index (extensive) Glossary (short) Glossary (short) Class will basically be one chapter per class! Class will basically be one chapter per class! Stroustrup/Programming

12Promises Detail: We will try to explain every construct used in this course in sufficient detail for real understanding Detail: We will try to explain every construct used in this course in sufficient detail for real understanding There is no “magic” There is no “magic” Utility: We will try to explain only useful concepts, constructs, and techniques Utility: We will try to explain only useful concepts, constructs, and techniques We will not try to explain every obscure detail We will not try to explain every obscure detail Completeness: The concepts, constructs, and techniques can be used in combination to construct useful programs Completeness: The concepts, constructs, and techniques can be used in combination to construct useful programs There are, of course, many useful concepts, constructs, and techniques beyond what is taught here There are, of course, many useful concepts, constructs, and techniques beyond what is taught here Stroustrup/Programming

13 More Promises Realism: The concepts, constructs, and techniques can be used to build “industrial strength” programs Realism: The concepts, constructs, and techniques can be used to build “industrial strength” programs i.e., they have been used to … i.e., they have been used to … Simplicity: The examples used are among the simplest realistic ones that illustrate the concepts, constructs, and techniques Simplicity: The examples used are among the simplest realistic ones that illustrate the concepts, constructs, and techniques Your exercises and projects will provide more complex examples Your exercises and projects will provide more complex examples Scalability: The concepts, constructs, and techniques can be used to construct large, reliable, and efficient programs Scalability: The concepts, constructs, and techniques can be used to construct large, reliable, and efficient programs i.e., they have been used to … i.e., they have been used to … Stroustrup/Programming

14 Why programming? Our civilization runs on software Our civilization runs on software Most engineering activities involve software Most engineering activities involve software Note: most programs do not run on things that look like a PC Note: most programs do not run on things that look like a PC a screen, a keyboard, a box under the table a screen, a keyboard, a box under the table Stroustrup/Programming

15 Ships Design Design Construction Construction Management Management Monitoring Monitoring Engine Engine Hull design Hull design Pumps Pumps Stroustrup/Programming

16 Aircraft Communication Communication Control Control Display Display Signal processing Signal processing “Gadget” control “Gadget” control Monitoring Monitoring Stroustrup/Programming

17 Phones Voice quality Voice quality User interfaces User interfaces Billing Billing Mobility Mobility Switching Switching Reliability Reliability Provisioning Provisioning Images Images Stroustrup/Programming

18 Energy Control Control Monitoring Monitoring Analysis Analysis Design Design Communications Communications Visualization Visualization Manufacturing Manufacturing Stroustrup/Programming

19 PC/workstation There’s a lot more to computing than games, word processing, browsing, and spreadsheets! There’s a lot more to computing than games, word processing, browsing, and spreadsheets! Stroustrup/Programming

20 Where is C++ Used? Just about everywhere Just about everywhere Mars rovers, animation, graphics, Photoshop, GUI, OS, compilers, slides, chip design, chip manufacturing, semiconductor tools, etc. See Stroustrup/Programming

21 So what is programming? Conventional definitions Conventional definitions Telling a very fast moron exactly what to do Telling a very fast moron exactly what to do A plan for solving a problem on a computer A plan for solving a problem on a computer Specifying the order of a program execution Specifying the order of a program execution But modern programs often involve millions of lines of code But modern programs often involve millions of lines of code And manipulation of data is central And manipulation of data is central Definition from another domain (academia) Definition from another domain (academia) A … program is an organized and directed accumulation of resources to accomplish specific … objectives … A … program is an organized and directed accumulation of resources to accomplish specific … objectives … Good, but no mention of actually doing anything Good, but no mention of actually doing anything The definition we’ll use The definition we’ll use Specifying the structure and behavior of a program, and testing that the program performs its task correctly and with acceptable performance Specifying the structure and behavior of a program, and testing that the program performs its task correctly and with acceptable performance Never forget to check that “it” works Never forget to check that “it” works Software == one or more programs Software == one or more programs Stroustrup/Programming

22 Programming Programming is fundamentally simple Programming is fundamentally simple Just state what the machine is to do Just state what the machine is to do So why is programming hard? So why is programming hard? We want “the machine” to do complex things We want “the machine” to do complex things And computers are nitpicking, unforgiving, dumb beasts And computers are nitpicking, unforgiving, dumb beasts The world is more complex than we’d like to believe The world is more complex than we’d like to believe So we don’t always know the implications of what we want So we don’t always know the implications of what we want “Programming is understanding” “Programming is understanding” When you can program a task, you understand it When you can program a task, you understand it When you program, you spend significant time trying to understand the task you want to automate When you program, you spend significant time trying to understand the task you want to automate Programming is part practical, part theory Programming is part practical, part theory If you are just practical, you produce non-scalable unmaintainable hacks If you are just practical, you produce non-scalable unmaintainable hacks If you are just theoretical, you produce toys If you are just theoretical, you produce toys Stroustrup/Programming

23 Compilation and linking You write C++ source code You write C++ source code Source code is (in principle) human readable Source code is (in principle) human readable The compiler translates what you wrote into object code (sometimes called machine code) The compiler translates what you wrote into object code (sometimes called machine code) Object code is simple enough for a computer to “understand” Object code is simple enough for a computer to “understand” The linker links your code to system code needed to execute The linker links your code to system code needed to execute E.g. input/output libraries, operating system code, and windowing code E.g. input/output libraries, operating system code, and windowing code The result is an executable program The result is an executable program E.g. a.exe file on windows or an a.out file on Unix E.g. a.exe file on windows or an a.out file on Unix C++ compiler C++ source code Object code linker Executable program Library Object code Stroustrup/Programming

24 Dynamic Linking Sometimes libraries are so large that you don’t want all their code in your executable Sometimes libraries are so large that you don’t want all their code in your executable Sometimes libraries can change over time, and you don’t want to have to create a new executable each time Sometimes libraries can change over time, and you don’t want to have to create a new executable each time Dynamic linking: link only a small amount of object code ahead of time, then at runtime find the library and link more code. Dynamic linking: link only a small amount of object code ahead of time, then at runtime find the library and link more code. C++ compiler C++ source code Object code Compile-time linker Executable program Library Interface Object code Stroustrup/Programming Dynamic Library Object code Run-time linker Running Program

25 A first program – just the guts… // … int main()// main() is where a C++ program starts { cout << "Hello, world!\n";// output the 13 characters Hello, world! // followed by a new line return 0;// return a value indicating success } // quotes delimit a string literal // NOTE: “smart” quotes “ ” will cause compiler problems. // so make sure your quotes are of the style " " // \n is a notation for a new line Stroustrup/Programming

26 A first program – complete // a first program: #include "std_lib_facilities_3.h"// get the library facilities needed for now int main()// main() is where a C++ program starts { cout << "Hello, world!\n";// output the 13 characters Hello, world! // followed by a new line return 0;// return a value indicating success } // note the semicolons; they terminate statements // braces { … } group statements into a block // main( ) is a function that takes no arguments ( ) // and returns an int (integer value) to indicate success or failure Stroustrup/Programming

27 A second program // modified for Windows console mode: #include "std_lib_facilities_3.h"// get the facilities for this course int main()// main() is where a C++ program starts { cout << "Hello, world!\n";// output the 13 characters Hello, world! // followed by a new line keep_window_open();// wait for a keystroke return 0;// return a value indicating success } // without keep_window_open() the output window will be closed immediately // before you have a chance to read the output (on Visual C ) Stroustrup/Programming

28 Hello, world! “Hello world” is a very important program “Hello world” is a very important program Its purpose is to help you get used to your tools Its purpose is to help you get used to your tools Compiler Compiler Program development environment Program development environment Program execution environment Program execution environment Type in the program carefully Type in the program carefully After you get it to work, please make a few mistakes to see how the tools respond; for example After you get it to work, please make a few mistakes to see how the tools respond; for example Forget the header Forget the header Forget to terminate the string Forget to terminate the string Misspell return (e.g. retrun) Misspell return (e.g. retrun) Forget a semicolon Forget a semicolon Forget { or } Forget { or } … Stroustrup/Programming

29 Hello world It’s almost all “boiler plate” It’s almost all “boiler plate” Only cout << "Hello, world!\n" directly does anything Only cout << "Hello, world!\n" directly does anything That’s normal That’s normal Most of our code, and most of the systems we use simply exist to make some other code elegant and/or efficient Most of our code, and most of the systems we use simply exist to make some other code elegant and/or efficient “real world” non-software analogies abound “real world” non-software analogies abound “Boiler plate,” that is, notation, libraries, and other support is what makes our code simple, comprehensible, trustworthy, and efficient. “Boiler plate,” that is, notation, libraries, and other support is what makes our code simple, comprehensible, trustworthy, and efficient. Would you rather write 1,000,000 lines of machine code? Would you rather write 1,000,000 lines of machine code? This implies that we should not just “get things done”; we should take great care that things are done elegantly, correctly, and in ways that ease the creation of more/other software: This implies that we should not just “get things done”; we should take great care that things are done elegantly, correctly, and in ways that ease the creation of more/other software: Style Matters! Style Matters! Stroustrup/Programming

30 The next lecture Will talk about types, values, variables, declarations, simple input and output, very simple computations, and type safety. Will talk about types, values, variables, declarations, simple input and output, very simple computations, and type safety. Stroustrup/Programming