using std::cout; int main () { cout << "Hello world!\n"; return 0; }"> using std::cout; int main () { cout << "Hello world!\n"; return 0; }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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.

2 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; }

3 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.

4 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"

5 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.

6 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.

7 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.

8 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.

9 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)

10 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!

11 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)

12 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.

13 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.

14 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.

15 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)

16 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 where the 5 indicates that the subsequent five values are to be summed. What do we need to know?

17 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 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


Download ppt "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."

Similar presentations


Ads by Google