Unformatted text preview:

CS208 C++ ProgrammingIntroductionC++ Programming ConceptsRunning C++ ProgramsA Simple C++ program:Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12A Simple C++ program: Sample RunIdentifiersKeywordsDataData TypesVariablesDeclaring VariablesVariables in MemoryConstantsDeclaring ConstantsConstants in MemoryDeclaring Constants/VariablesConstants/Variables ExampleCommentsFunction StatementsAssignment StatementsSlide 29Arithmetic ExpressionsOperator ResultsDivision and RemainderOperator PrecedenceAssignment RevisitedSlide 35SpacingArithmetic EquivalenciesInput/Output (I/O)Output StatementOutput ExampleInput StatementInput ExampleVariables during Program Execution (1/8)Variables during Program Execution (2/8)Variables during Program Execution (3/8)Variables during Program Execution (4/8)Variables during Program Execution (5/8)Variables during Program Execution (6/8)Variables during Program Execution (7/8)Variables during Program Execution (8/8)Age Program DescriptionAge Program DesignAlgorithmAge Program CodeAge Program ExplanationSlide 56Running Programs on your C++ CompilerKeeping the DOS screen openExampleExample OutputExerciseTemplate File01/14/19 1CS208 C++ Programming Part 12IntroductionThe C++ language will be used as a tool to learn about programmingWe will only cover a small part of C++ programmingYou will learn how to write short, imperative C++ programsObject-oriented aspects of C++ will NOT be covered3C++ Programming ConceptsTo write a C++ program, we need:Text editorC++ compilerBloodshed Dev-C++ contains an editor and a compiler in oneThis is known as an IDE (Integrated Development Environment)4Running C++ ProgramsUse an editor to create/edit C++ code in a text fileC++ filenames ended with extension .cppCompile and link the C++ fileExecute the executable file createdIf find bugs or errors, go back to the first step and correct them and try again.5A Simple C++ program: #include <iostream>using namespace std;//Displays greeting int main(){ cout << "Hello World!"; return 0;}(Each line is explained on the following slides)6A Simple C++ program: Tells compiler to include code from the iostream library for use of input and output routines.#include <iostream>using namespace std;//Displays greeting int main(){ cout << "Hello World!"; return 0;}7A Simple C++ program: Tells compiler to use a standard environment.(The first 2 lines must appear in ALL your programs)#include <iostream>using namespace std;//Displays greeting int main(){ cout << "Hello World!"; return 0;}8A Simple C++ program: Comment for the programmer – lines beginning with "//" are ignored by the compiler#include <iostream>using namespace std;//Displays greeting int main(){ cout << "Hello World!"; return 0;}9A Simple C++ program: C++ programs are built using functions. A C++ program must contain at least one function, called main. This is the main function “header”.NoteYour text uses:void main()But our compiler requires:int main()#include <iostream>using namespace std;//Displays greeting int main(){ cout << "Hello World!"; return 0;}10A Simple C++ program: Left curly brace marks beginning andright curly brace marks end of the main function#include <iostream>using namespace std;//Displays greeting int main(){ cout << "Hello World!"; return 0;}11A Simple C++ program: cout displays output, in this case, Hello World! to the monitor#include <iostream>using namespace std;//Displays greeting int main(){ cout << "Hello World!"; return 0;}12A Simple C++ program: return tells the program to exit from the main function. By default, returning 0 implies success. #include <iostream>using namespace std;//Displays greeting int main(){ cout << "Hello World!"; return 0;}13A Simple C++ program:Sample RunWhen the program is executed, this is the output:14IdentifiersIdentifiers are the words that a programmer uses to name things in a programAn identifier can be made up of letters, digits, and the underscore characterAn identifier cannot begin with a digitC++ is case sensitive, therefore num and Num are different identifiersKeywords CANNOT be identifiers (see next slide)15Keywords C++ keywords (do NOT use as Identifiers) :asm auto bool break casecatch char class const const_castcontinue default delete do doubledynamic-cast else enum explicit externFALSE float for friend gotoif int long mutable namespacenew operator private register returnShort signed sizeof static static-caststruct switch template this throwtry TRUE typedef typeid typenameunion unsigned using virtual voidvolatile wchar_t while16DataYou can store each piece of program data as:a Constantora VariableYou will assign an identifier (name) to:each constant each variable17Each piece of data stored in a program must also have a type. Three basic C++ data types are:int - whole numbers// No commas or leading zeros in numberdouble - numbers with fractional parts// Has a decimal pointchar - a single ASCII character// Enclosed in single quotesData Types18VariablesVariables are containers used to hold input dataintermediate dataoutput data in your program (think of them as named chunks of memory)A variable will occupy a number of bytes in Main MemoryThe number of bytes allocated to a variable depends on the type of data that will be stored in it (e.g. numbers, characters, etc.)19Declaring VariablesDeclaring a variable will: - define its type - reserve a memory cell for it - give the memory cell a name (an identifier)Format: type variable-list;Examples: char initial; int num, count; double gpa;20Variables in Memoryint numStudents; …int average, max; data typevariable name(s) double total; …920092009204920492089208921292129216921692209220922492249228922892329232 numStudents:average: max:total:MemoryThe value stored in a variable is initially garbage, and changes as the program runs.21ConstantsA constant is similar to a variable, except that its value is set by the programmer and CANNOT changeThe compiler will issue an error if you try to modify a constantWhy use constants?Gives names to otherwise unclear literal valuesFacilitates easier changes to the codePrevents inadvertent errors22Declaring ConstantsDeclaring a constant will: - define its type and reserve a memory cell for it - give the memory cell a name (an identifier) - store a value in the memory cellSince


View Full Document

RU CS 208 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?