Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exposure C++ Chapter IV Introduction to C++ Programs.

Similar presentations


Presentation on theme: "Exposure C++ Chapter IV Introduction to C++ Programs."— Presentation transcript:

1

2 Exposure C++ Chapter IV Introduction to C++ Programs

3 Some Small Program Examples Four small program examples follow. Every program is numbered starting with 04 for Chapter 04 followed by 01, 02, 03 and 04 to indicate the program sequence within the chapter. Look for features that are shared by all three examples.

4 //PROG0401.CPP // This demonstrates the absolute minimum program // that does nothing. void main() { } PROG0401.CPP OUTPUT This program has no output

5 // PROG0402.CPP // This program demonstrates program output. // The library is necessary to use the cout // keyword. #include void main() { cout << "Bewilderment + Exposure = Obvious" << endl; } PROG0402.CPP OUTPUT Bewilderment + Exposure = Obvious

6 // PROG0403.CPP // This program adds clrscr to clear the screen // and getch to stop program output. #include // necessary for cout #include // necessary for clrscr // and getch void main() { clrscr(); cout << "Bewilderment + Exposure = Obvious" << endl; getch(); } PROG0403.CPP OUTPUT Bewilderment + Exposure = Obvious is not used by many modern compilers and will not be used in later slide chapters.

7 // PROG0404.CPP /********************************************************************* * This program demonstrates a variety of ways that you can use * * comments in a C++ program. It is possible to make a large * * multi-line comment, like shown in this box. It is also possible * * to make single-line comments with // * *********************************************************************/ #include // contains input/output operations #include // contains console functions like clrscr void main() { clrscr(); // clears the screen cout << "Bewilderment + "; cout << "Exposure = Obvious" << endl; getch(); // stops program execution } PROG0404.CPP OUTPUT Bewilderment + Exposure = Obvious

8 C++ Program Statements C++ program statements are written, in sequence, between the opening and closing braces, like: void main() { program statement 1; program statement 2; program statement 3; } Every program statement ends with a semi-colon ( ; )

9 Using Library Functions The #include preprocessor Libraries of specialized functions are available with the #include preprocesor, like: #include C++ has a large selection of function libraries for many purposes. Each new function library will be explained as it is first introduced.

10 Making Single-Line Comments Syntax for slash-style comment: cout << endl; // starts a new line Syntax for slash-star style comment: cout << endl; /* starts a new line */ /* starts a new line */ cout << endl; cout << /* starts a new line */ endl;

11 Making Multi-Line Comments Syntax for slash-style comment: // INTEREST.CPP // This program will compute an amortization // table based on loan amount, yearly interest, // and monthly payment size. Syntax for slash-star style comment: /* INTEREST.CPP This program will compute an amortization table based on loan amount, yearly interest, and monthly payment size. */

12 PROG0405.CPP OUTPUT Measure today's understanding by yesterday's confusion // PROG0405.CPP // This program uses a single statement to display a single line. #include // contains input/output functions #include // contains console functions like clrscr void main() { clrscr(); cout << "Measure today's understanding by yesterday's confusion"; getch(); } Program Output to the Monitor

13 cout << output cout << "Program Output Information"; cout does not automatically perform carriage return/line feeds. Use double quotes " " at the start, and end of each string of characters that needs to be displayed on the monitor. cout Syntax

14 PROG0406.CPP OUTPUT Measure today's understanding by yesterday's confusion // PROG0406.CPP // This program displays a single output line with three // separate statements. This demonstrates that cout // does not automatically generate a carriage return. #include // contains input/output functions #include // contains console functions like clrscr void main() { clrscr(); cout << "Measure "; cout << "today's understanding "; cout << "by yesterday's confusion"; getch(); }

15 Sending Program Output to a New Line Sending program output to a new line is done with endl. You can think that it means end of line, and anything following will go to the next line. You cannot combine an output string with endl, like: cout << "Thomas Smith" endl; The output string and endl must be separated by the "double chevrons" output-stream-operator << cout << "Thomas Smith" << endl;

16 PROG0407.CPP OUTPUT Measure today's understanding by yesterday's confusion // PROG0407.CPP // This program introduces endl to generate a carriage return. #include // contains input/output functions #include // contains console functions like clrscr void main() { clrscr(); cout << "Measure " << endl; cout << "today's understanding " << endl; cout << "by yesterday's confusion" << endl; getch(); }

17 PROG0408.CPP OUTPUT Measure today's understanding by yesterday's confusion // PROG0408.CPP // This program demonstrates how to insert a blank line between // program output statements. #include // contains input/output functions #include // contains console functions like clrscr void main() { clrscr(); cout << "Measure " << endl << endl; cout << "today's understanding " << endl << endl; cout << "by yesterday's confusion" << endl; getch(); }

18 // PROG0409.CPP // This program is used to explain the difference between the // program listing and the program execution. // Thomas Phillips // Mr. Randy Robertson // Period 3 // September 11, 1997 // Lab Assignment #4 #include // contains input/output functions #include // contains console functions like clrscr void main() { clrscr(); cout << "Thomas Phillips" << endl; cout << "5003 Orleans Court" << endl; cout << "Kensington, Maryland 20795" << endl; getch(); } This is the Program Listing or Program Source Code.

19 This is the Program Execution or Program Output Thomas Phillips 5003 Orleans Court Kensington, Maryland 20795

20 Program Listing The program listing is the source of a program. It contains the program statements and comments that a programmer uses to write a program. The listing is the source code that the compiler checks, and translates into an executable machine code file. The file name that prints the listing normally ends in CPP.

21 Program Execution Output The program execution output is the output generated by the machine code file of a program. The program output will go to the computer monitor, unless specified to go somewhere else. The execution of a program will only generate output if there are statements that produce output like: cout << "This line goes to output";

22 // PROG0410.CPP // This demonstrates that a program does not necessarily // have output. // Nothing is shown on the monitor without a statement // like cout << "HI"; void main() { // This comment is not an output statement }


Download ppt "Exposure C++ Chapter IV Introduction to C++ Programs."

Similar presentations


Ads by Google