Chapters 1 & 2 Programming and Programs: Bjarne Stroustrup
Chapters 1 & 2 Programming and Programs: Bjarne Stroustrup
Bjarne Stroustrup
www.stroustrup.com/Programming
Abstract
Today, well outline the aims for this course and present a rough course plan. Well introduce the basic notion of programming and give examples of areas in which software is critical to our civilization. Finally, well present the simplest possible C++ program and outline how it can be made into running code.
Stroustrup/Programming 2
Overview
Stroustrup/Programming
This is a course
i.e., people who can produce systems that others will use Though not (necessarily) geniuses Though do need sleep occasionally, and take a normal course load
Stroustrup/Programming
Not!
A Washout course
if you can get into the science/engineering parts of a university, you can handle this course
The C++ programming language who want to become language lawyers
A course in
For students
Using
Some untested software development methodologies and a lot of unnecessarily long words
Stroustrup/Programming 5
The Aims
Teach/learn
Fundamental programming concepts Key useful techniques Basic Standard C++ facilities Write small colloquial C++ programs Read much larger programs Learn the basics of many other languages by yourself Proceed with an advanced C++ programming course
The Means
Lectures
Attend every one Read a chapter ahead (about one per lecture) Read the chapter again after each lecture Feedback is welcome (typos, suggestions, etc.)
Notes/Chapters
Stroustrup/Programming
Work
Projects
Thats where the most fun and the best learning takes place
Quizzes Exams
Stroustrup/Programming
Cooperate on Learning
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!
Dont claim to have written code that you copied from others Dont 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
Why C++ ?
You cant learn to program without a programming language 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 most widely used language in engineering areas
http://www.research.att.com/~bs/applications.html
Stroustrup/Programming
10
Why C++ ?
C++ is available on almost all kinds of computers Programming concepts that you learn using C++ can be used fairly directly in other languages
Stroustrup/Programming
11
Types, variables, strings, console I/O, computations, errors, vectors functions, source files, classes File I/O, I/O streams Graphical output Graphical User Interface Free store, pointers, and arrays Lists, maps, sorting and searching, vectors, templates The STL Software ideals and history Text processing, numerics, embedded systems programming, testing, C, etc.
Stroustrup/Programming 12
Throughout
Program design and development techniques C++ language features Background and related fields, topics, and languages
Note: Appendices
C++ language summary C++ standard library summary Index (extensive) Glossary (short)
Stroustrup/Programming
13
Promises
Detail: We will try to explain every construct used in this course in sufficient detail for real understanding
There is no magic
Utility: We will try to explain only useful concepts, constructs, and techniques
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
Stroustrup/Programming
14
More Promises
Realism: the concepts, constructs, and techniques can be used to build industrial strength programs
Simplicity: The examples used are among the simplest realistic ones that illustrate the concepts, constructs, and techniques
Scalability: The concepts, constructs, and techniques can be used to construct large, reliable, and efficient programs
Stroustrup/Programming
15
Feedback request
Please mail questions and constructive comments to your_teacher@your_institution Your feedback will be most appreciated
On style, contents, detail, examples, clarity, conceptual problems, exercises, missing information, depth, etc.
Stroustrup/Programming
16
Why programming?
Stroustrup/Programming
17
Ships
Stroustrup/Programming
Aircraft
Stroustrup/Programming
19
Phones
Stroustrup/Programming
Energy
Stroustrup/Programming
21
PC/workstation
Theres a lot more to computing than games, word processing, browsing, and spreadsheets!
Stroustrup/Programming
22
Mars rovers, animation, graphics, Photoshop, GUI, OS, compilers, slides, chip design, chip manufacturing, semiconductor tools, etc. See www.research.att/~bs/applications.html
Stroustrup/Programming 23
return 0;
}
// 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
24
// main() is where a C++ program starts // output the 13 characters Hello, world! // followed by a new line // return a value indicating success
A second program
// modified for Windows console mode: #include "../../std_lib_facilities.h" // get the facilities for this course int main() { cout << "Hello, world\n"; // main() is where a C++ program starts // output the 13 characters hello, world! // followed by a new line // wait for a keystroke // return a value indicating success
keep_window_open(); return 0;
}
// without keep_window_open() the output window will be closed immediately // before you have a chance to read the output (on Visual C++ 2003)
Stroustrup/Programming
26
Hello, world!
Compiler Program development environment Program execution environment After you get it to work, please make a few mistakes to see how the tools respond; for example
Forget the header Forget to terminate the string Misspell return (e.g. retrun) Forget a semicolon Forget { or }
Stroustrup/Programming 27
Hello world
Only cout << "Hello, world!\n" directly does anything 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
Thats normal
Boiler plate, that is, notation, libraries, and other support is what makes our code simple, comprehensible, trustworthy, and efficient.
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!
Stroustrup/Programming 28
Source code is (in principle) human readable Object code is simple enough for a computer to understand E.g. input/output libraries, operating system code, and windowing code E.g. a .exe file on windows or an a.out file on Unix
Stroustrup/Programming 29
The compiler translates what you wrote into object code (sometimes called machine code)
So what is programming?
Conventional definitions
Telling a very fast moron exactly what to do A plan for solving a problem on a computer Specifying the order of a program execution
But modern programs often involve millions of lines of code And manipulation of data is central
Specifying the structure and behavior of a program, and testing that the program performs its task correctly and with acceptable performance
Programming
Just state what the machine is to do We want the machine to do complex things
Programming is understanding
Will talk about types, values, variables, declarations, simple input and output, very simple computations, and type safety.
Stroustrup/Programming
32