cps120: introduction to computer science introduction to c++
TRANSCRIPT
![Page 1: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/1.jpg)
CPS120: Introduction to Computer Science
Introduction to C++
![Page 2: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/2.jpg)
A Simple C++ Program
Comments // Simple C++ Program // // Purpose: To demonstrate the // parts of a simple C++ program
Compiler Directive #include <iostream.h>Main Function main ( )Braces {Statements cout << "This is a simple program ";
return 0;}
![Page 3: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/3.jpg)
C++ and Blank Space
• C++ allows for great flexibility in the spacing and layout of code
• Use this feature to make it easier for you as a human being to read the code
![Page 4: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/4.jpg)
Formatting in C++• Modern programming languages are free form with
delimiters instead of columns to determine the end of instructions
–The ; (semi-colon) is the delimiter used in C++
• Use tabs, indents, and blank lines in any manner that makes code easier to understand
• Many programming instructions become subordinate to other instructions due to scope and other restrictions. Formatting code to reflect this makes it easier to read
![Page 5: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/5.jpg)
Uppercase or Lowercase
• Be careful to use the same combination of uppercase or lowercase lettering when you enter source code
• Commands and other reserved words are all lower case
![Page 6: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/6.jpg)
Characteristics of a C++ Program
• Comments
• Compiler Directives
• Functions
• Braces
• Statements
![Page 7: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/7.jpg)
Comments
• Explain the purpose of a program
• Keep notes regarding changes to source code
• Store details about the programmers for future reference
• Explain parts of the program
![Page 8: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/8.jpg)
Comments
• Document what is happening, why it is happening and other issues
• Commentary is ignored by the compiler• C++ has inline, block and documentary comments
–Inline comments are within line of code• Use the // symbols
–Block comments are long comments delimited with /* and */
![Page 9: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/9.jpg)
Sample CommentsAt the start of the program
/**************************************************
** Miles Per Gallon **
** Programmer: Paul J. Millis **
** Purpose: Calculate mile per gallon and price per mile **
** given miles, gallons and gas price **
*************************************************/Within specific lines of code
float PricePerMile = 0.00; //store the price per mile
float MilesPerGallon = 0.0; //stores the miler per gallon achieved
![Page 10: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/10.jpg)
Compiler Directives
• Instructions to the compiler rather than part of the C++ language– Most common directive is #include
• For Example: #include <iostream.h>– A .h file is a header file. It serves as a link between
program code and standard C++ code needed to make programs run
![Page 11: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/11.jpg)
Functions
• A function is a block of code that carries out a specific task
• Every C++ program has a main function that executes when a program initiates– Includes open parenthesis to designate a
function– Ends with a return 0; statement
![Page 12: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/12.jpg)
Braces
• Mark the beginning and ending of blocks of related code– Every opening brace must have a closing brace
![Page 13: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/13.jpg)
Statements
• Functions contain statements that consist of instructions or commands that make the program work
![Page 14: CPS120: Introduction to Computer Science Introduction to C++](https://reader036.vdocument.in/reader036/viewer/2022082821/5697bfe81a28abf838cb663d/html5/thumbnails/14.jpg)
Semicolons
• There must be a semicolon after every statement– To tell the compiler that the statement is
complete– Function definitions and compiler directives are
exempt