220 likes | 325 Views
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.
E N D
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. Look for features that are shared by all three examples.
//PROG0401.CPP// This demonstrates the absolute minimum program // that does nothing.void main(){} PROG0401.CPP OUTPUT This program has no output
// 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
// 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
// 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
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 ( ; )
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.
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;
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. */
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
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.
// 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
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;
// 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
// 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
// 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.
This is the Program Executionor Program Output Thomas Phillips 5003 Orleans Court Kensington, Maryland 20795
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.
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";
// 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}