CPS120: Introduction to Computer Science What We Know About C++ Lecture 8 - A
Characteristics of a C++ Program Comments Compiler Directives Functions Braces Statements Show MileFunc.cpp
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; }
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
Sample Comments At 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
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
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
Braces Mark the beginning and ending of blocks of related code Every opening brace must have a closing brace
Statements Functions contain statements that consist of instructions or commands that make the program work
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
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
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