Download presentation
Presentation is loading. Please wait.
Published byKarin Sanders Modified over 9 years ago
1
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include using std::cout; int main () { cout << "Hello world!\n"; return 0; }
2
2 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include 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.
3
3 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"
4
4 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include using std::cout; int main () { cout << "Hello world!\n"; return 0; } #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 The angle brackets surrounding iostream tell the preprocessor that the requested file is part of the C++ Standard Library.
5
5 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.
6
6 Namespaces using namespace std; 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.
7
7 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include 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.
8
8 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include 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)
9
9 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!
10
10 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include 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)
11
11 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include 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.
12
12 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.
13
13 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.
14
14 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays the phrase "Hello world!" on the screen. // Revisions: // ======================================================= #include 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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.