Download presentation
Presentation is loading. Please wait.
Published byGwen Garrison Modified over 9 years ago
1
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014
2
2 Syllabus General Program Layout General Program Layout C++ Preprocessor C++ Preprocessor main() Function main() Function Comments Comments Statements Statements Functions Functions Libraries Libraries C++ Sample Program C++ Sample Program
3
3 General Program Layout Header Files (#include) Global Macro Definitions Global Enumerated Types Global Structure Declarations Global Variable Definitions Global Function Prototypes Function Definitions main (…) { … } fun_2 (…) { … } fun_3 (…) { … } fun_k (…) { … } The compiler scans the source code from the beginning of the file to the end of the file. Rule: If component “A” accesses component “B”, then “B” must be defined (or declared) before “A”. A C++ function is a named block of code that performs a task. It can receive or return data values. A program contains one or more function definitions. Every C++ program must have one, and only one, function named “main”. return_type function_name ( formal_params) { Local Variable Definitions Local Function Prototypes C code statements } // OK to have ending comment here
4
4 C++ Preprocessor Before a C++ program is compiled, the preprocessor scans the source, looking for directives and macros; filters out commentsBefore a C++ program is compiled, the preprocessor scans the source, looking for directives and macros; filters out comments directive → embedded instructions for handling the program before it is compileddirective → embedded instructions for handling the program before it is compiled Example: #include // Loads file stdio.hExample: #include // Loads file stdio.h macro → a named code fragment that is substituted into the program wherever that name is found; may not be proper C, but after inclusion must yield proper C sourcemacro → a named code fragment that is substituted into the program wherever that name is found; may not be proper C, but after inclusion must yield proper C source Example: #define TRUE 1 // Macro TRUE is 1Example: #define TRUE 1 // Macro TRUE is 1
5
5 main() Function When a C++ program is run, the very first function executed is always the one named main(); and main() must be present main() contains the start up code for the program main() contains the start up code for the program Like other functions, main() can receive input values, compute new values, and return an output value Like other functions, main() can receive input values, compute new values, and return an output value Input:Data passed from command line to the program Output:Integer representing the termination status Example: main header with no input arguments needed int main( void )
6
6 Comments Comments help to document a program Block comments are defined by paired /* and */ with all text in between skipped Example: /* Example Program #1 */ int main( void ) { // main /* This program does nothing */ return 0; } //end main After the program is done, it can return a value that indicates the termination status C is free-form in the formatting of the source code. It is good practice to use a consistent style of spacing, indentation, and blank lines to improve readability
7
7 Comments /*…*/ - style comments cannot be “nested” within other /*…*/ comments! Wrong Examples: Area = L * W /* /* Area of rectangle */ */ /* x = r * cos(a); /* x component */ y = r * sin(a); /* y component */ */ A section of code with embedded comments can be commented out using preprocessor directives
8
8 Comments Comments C99 /* */ is still available for block comments C++ allows support for line comments. The characters // mark the start of a comment that extends just to the end of the line. // most C+ versions allow all comments #include int main( void ) { // main float w;/* Comment */ int a;// Comment /* Comment // Comment */ // /* Comment */ // Comment return 0; } //end main
9
9 Statements Strictly speaking, C++ code consists of expressions, yet we’ll treat operations as statements, similar to other programming languages; just politics Strictly speaking, C++ code consists of expressions, yet we’ll treat operations as statements, similar to other programming languages; just politics A statement is a combination of keywords, expressions, and other statements that forms and performs a complete action A statement is a combination of keywords, expressions, and other statements that forms and performs a complete action Individual statements are terminated by the semicolon ; Individual statements are terminated by the semicolon ; A spurious semicolon by itself is a valid C++ statement, known as the "null statement”, or the empty statement; it does nothing A spurious semicolon by itself is a valid C++ statement, known as the "null statement”, or the empty statement; it does nothing
10
10 Functions A function definition consists of the function header, a possibly empty formal parameter list, and the function body enclosed in a pair of { and } braces Header → Describes the function name, parameter list, and return type Body → Contains local declarations and statements Braces mark the extent of the body: »Left brace { marks the beginning »Right brace } marks the end »A semicolon is not needed after the ending brace
11
11 IO Functions in C++ from old C Input: entry from console keyboard scanf( "%c”, &char_variable ); scanf( "%d”, &int_variable ); scanf( "%f”, &float_variable ); scanf( "%lf”,&double_variable ); Output: display to console screen printf( "literal_text” ); printf( "%c", single_character_value ); printf( "%d", integer_value ); printf( "%f", single_precision_floating_point_value ); printf( "%f", double_precision_floating_point_value ); printf( "%s", string_value );
12
12 Libraries The core C language was designed to be concise and minimalistic; e.g. no array assignments; no nested functions; don’t think this is necessarily good. Even C++ has the same limitation The core C language was designed to be concise and minimalistic; e.g. no array assignments; no nested functions; don’t think this is necessarily good. Even C++ has the same limitation I/O functions, math functions, string functions, etc. are provided via external library packages I/O functions, math functions, string functions, etc. are provided via external library packages A common set of libraries evolved to become part of the ISO C standard A common set of libraries evolved to become part of the ISO C standard Each library has an associated header file that is referenced with the " #include " directive Each library has an associated header file that is referenced with the " #include " directive
13
13 C++ Sample Program /* Example Program #3 * #include #include #define PI 3.14159265 int main( void ) { // main int angle_deg; // Angle in degrees double angle_rad; // Angle in radians double calc_value = 0.0; // Prompt user for the input angle printf( "Enter angle in degrees:\n” ); scanf( "%d", &angle_deg ); // Convert input angle to radians angle_rad = angle_deg * PI / 180; calc_value = sin ( angle_rad ) + cos ( angle_rad ); printf( "Result = %f\n", calc_value ); return 0; } //end main To use trig functions, the math library’s header file must be loaded: #include Variables can be initialized as part of their declaration.
14
14 More Complex For Loop in C++ // referencing a “user-defined type” mat_t void init( mat_t m ) { // init int row, col; for ( row = 0; row < N; row++ ) { for ( col = 0; col < N; col++ ) { m[ row ][ col ] = row + col; } // end for } // end init
15
15 Even More Complex For Loop in C++ void print( mat_t m ) { // print int row, col; for ( row = 0; row < 10; row++ ) { for ( col = 0; col < 10; col++ ) { printf( "%7u", m[ row ][ col ] ); // also C++ } // end for printf( "\n" ); } //end for printf( "\n" ); for ( row = N - 10; row < N; row++ ) { for ( col = N - 10; col < N; col++ ) { printf( "%7u", m[ row ][ col ] ); // also C++ } //end for printf( "\n" ); } // end for } //end print
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.