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 12IntroductionThe C++ language will be used as a tool to learn about programmingWe will only cover a small part of C++ programmingYou will learn how to write short, imperative C++ programsObject-oriented aspects of C++ will NOT be covered3C++ Programming ConceptsTo write a C++ program, we need:Text editorC++ compilerBloodshed Dev-C++ contains an editor and a compiler in oneThis is known as an IDE (Integrated Development Environment)4Running C++ ProgramsUse an editor to create/edit C++ code in a text fileC++ filenames ended with extension .cppCompile and link the C++ fileExecute the executable file createdIf 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:14IdentifiersIdentifiers are the words that a programmer uses to name things in a programAn identifier can be made up of letters, digits, and the underscore characterAn identifier cannot begin with a digitC++ is case sensitive, therefore num and Num are different identifiersKeywords 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 while16DataYou can store each piece of program data as:a Constantora VariableYou 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 numberdouble - numbers with fractional parts// Has a decimal pointchar - a single ASCII character// Enclosed in single quotesData Types18VariablesVariables are containers used to hold input dataintermediate dataoutput data in your program (think of them as named chunks of memory)A variable will occupy a number of bytes in Main MemoryThe 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 VariablesDeclaring 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.21ConstantsA constant is similar to a variable, except that its value is set by the programmer and CANNOT changeThe compiler will issue an error if you try to modify a constantWhy use constants?Gives names to otherwise unclear literal valuesFacilitates easier changes to the codePrevents inadvertent errors22Declaring ConstantsDeclaring 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 cellSince
View Full Document