1 / 21

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

Exposure C++ Chapter IV Introduction to C++ Programs. 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.

Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

  6. // 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 <iostream.h> // contains input/output operations#include <conio.h> // contains console functions like clrscrvoid main(){ clrscr(); // clears the screen cout << "Bewilderment + "; cout << "Exposure = Obvious" << endl; getch(); // stops program execution} PROG0404.CPP OUTPUT Bewilderment + Exposure = Obvious

  7. 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 ( ; )

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

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

  10. 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. */

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

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

  13. // 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 <iostream.h> // contains input/output functions #include <conio.h> // contains console functions like clrscr void main() { clrscr(); cout << "Measure "; cout << "today's understanding "; cout << "by yesterday's confusion"; getch(); } PROG0406.CPP OUTPUT Measure today's understanding by yesterday's confusion

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

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

  16. // PROG0408.CPP // This program demonstrates how to insert a blank line between // program output statements. #include <iostream.h> // contains input/output functions #include <conio.h> // 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(); } PROG0408.CPP OUTPUT Measure today's understanding by yesterday's confusion

  17. // 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 <iostream.h> // contains input/output functions#include <conio.h> // contains console functions like clrscrvoid 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.

  18. This is the Program Executionor Program Output Thomas Phillips 5003 Orleans Court Kensington, Maryland 20795

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

  20. 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";

  21. // 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}

More Related