ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact time will be determined in today's class.
A simple C++ program // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> using std::cout; int main () { cout << "Hello world!\n"; return 0; }
A simple C++ program C++ comments. Everything between the // // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> using std::cout; using std::endl; int main () { cout << "Hello world!\n"; return 0; } C++ comments. Everything between the // and the end of line is ignored by the compiler.
Commenting guidelines I Every file should have a comment section at the top, containing: The name of the file A brief description of the file's contents The name of the author The date the file was created If the file has been revised, a revision log describing the date of the revision, who did it and what was revised. File header comments are usually "framed"
A simple C++ program #include is a preprocessor directive. Preprocessing occurs before compilation. This directive instructs the preprocessor to include the file iostream at that point. iostream is a library containing utilities that perform I/O // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> using std::cout; int main () { cout << "Hello world!\n"; return 0; } The angle brackets surrounding iostream tell the preprocessor that the requested file is part of the C++ Standard Library.
Namespaces A namespace is a collection of name definitions (e.g. class, variable, function names). Grouping names in namespaces and specifying what namespace we're currently using, reduces the probability of errors due to conflicting names. The std namespace contains the names defined in several standard C++ libraries, including iostream. A using directive tells the compiler that we will be using names defined in a particular namespace.
Namespaces using namespace std; using std::cout What's the difference? This tells the compiler that we will be using names defined in the standard namespace. However, std is a very large collection of definitions. It is a better idea to specify exactly which names we will be using: using std::cout This tells the compiler that we will be using the cout name (more on what this is later) which is defined in the std namespace. What's the difference? Think of a namespace as a collection of available tools. using namespace std; is the equivalent to emptying the whole toolbox on the worktable when you only need a hammer. using std::cout; is the equivalent to taking only the hammer out of the toolbox.
A simple C++ program a using directive instructs // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> using std::cout; int main () { cout << "Hello world!\n"; return 0; } a using directive instructs the compiler that we'll be using cout, which is defined in the standard namespace.
A simple C++ program Program execution always begins and ends in the // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> using std::cout; int main () { cout << "Hello world!\n"; return 0; } Program execution always begins and ends in the main function. There should be exactly one main function in a C++ program. You may invoke other functions from within main The body of main(). It consists of a sequence of statements (two of them)
Indentation It helps make the code readable. Keep in mind: the compiler ignores white space. int main() { int i; for(i=0; i<10; i++) { cout << "1TBS" << endl; } return 0; int main() { int i; for(i=0; i<10; i++) { cout << "Allman" << endl; } return 0; Choose ONE style and STICK with it!
A simple C++ program The type of the return value of main (an integer) // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> using std::cout; int main ( ) { cout << "Hello world!\n"; return 0; } The type of the return value of main (an integer) A list of input arguments for main (currently empty)
A simple C++ program // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> using std::cout; int main () { cout << "Hello world!\n"; return 0; } This line prints the message Hello world! on the screen. 'cout <<' is an instruction that says "write what follows to the standard output" (typically the screen). We will talk more about cout itself later.
Statement A statement is a unit of executable code. All statements in C++ are terminated by semicolons Note that a preprocessor directive is NOT a statement The body of a function consists of a sequence of statements.
Strings "Hello world!\n" is a string (a sequence of characters) Strings are enclosed in double quotes. \n is a special character. It means "new line". cout << "Hello world!\n"; will print out the message Hello world! and then move the cursor to the next line.
A simple C++ program A return statement is used to specify // ======================================================= // File: helloworld.cpp // Author: Vana Doufexi // Date: 9/21/2006 // Description: Displays the phrase "Hello world!" on the screen. // Revisions: #include<iostream> using std::cout; int main () { cout << "Hello world!\n"; return 0; } A return statement is used to specify the value that should be returned by a function (recall that main is supposed to return an integer). A function terminates immediately after a return statement is encountered. 0 means that at this point the program terminates successfully (with no errors)
A more complex example What do we need to know? Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number of values remaining to be entered. A typical input sequence might be 5 100 6 189 3 2 where the 5 indicates that the subsequent five values are to be summed. What do we need to know?
A more complex example What do we need to know? Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number of values remaining to be entered. A typical input sequence might be 5 100 6 189 3 2 where the 5 indicates that the subsequent five values are to be summed. What do we need to know? How to read numbers from the keyboard How to store their values An algorithm to compute their sum How to display a message on the screen