CS208 C++ ProgrammingIntroductionC++ Programming ConceptsRunning C++ ProgramsA Simple C++ program:Slide 6Slide 7Slide 8Slide 9Slide 10A Simple C++ program: Sample RunIdentifiersKeywordsDataData TypesVariablesDeclaring VariablesVariables in MemoryConstantsDeclaring ConstantsConstants in MemoryDeclaring Constants/VariablesConstants/Variables ExampleCommentsFunction StatementsAssignment StatementsSlide 27Arithmetic ExpressionsOperator ResultsDivision and RemainderOperator PrecedenceAssignment RevisitedSlide 33SpacingArithmetic 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 54Running Programs on your C++ CompilerKeeping the DOS screen openExampleExample OutputExerciseTemplate File01/13/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.h>//Displays greeting void main(){ cout << "Hello World!";}(Each line is explained on the following slides)6A Simple C++ program: #include <iostream.h>//Displays greeting void main(){ cout << "Hello World!";}Tells compiler to include code from the iostream library for use of input and output routines.7A Simple C++ program: #include <iostream.h>//Displays greeting void main(){ cout << "Hello World!";}Comment for the programmer – ignored by compiler8A Simple C++ program: #include <iostream.h>//Displays greeting void main(){ cout << "Hello World!";}C++ programs are built using functions. A C++ program must contain at least one function, called main. This is the main function “header”.9A Simple C++ program: #include <iostream.h>//Displays greeting void main(){ cout << "Hello World!";}Left curly brace marks beginning andright curly brace marks end of the main function10A Simple C++ program: #include <iostream.h>//Displays greeting void main(){ cout << "Hello World!";}cout displays output, in this case, Hello World! to the monitor11A Simple C++ program:Sample RunWhen the program is executed, this is the output:12IdentifiersIdentifiers 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)13Keywords 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 while14DataYou can store each piece of program data as:a Constantora VariableYou will assign an identifier (name) to:each constant each variable15Each 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 Types16VariablesVariables 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.)17Declaring 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;18Variables 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.19ConstantsA 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 errors20Declaring 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 the value is set by the programmer, the value must be known when the program is written Format: const type constant-name = value;Examples: const double Pi = 3.14; const int Age = 33; const char Yes = 'Y';21Constants in MemoryThe value stored in a constant CANNOT change as the program runs.const int Dozen = 12; …const double Pi = 3.14; data typeconstant nameconstant value620062006204620462086208621262126216621662206220622462246228622862326232 Dozen:Pi:Memory123.1422Declaring Constants/VariablesDeclare constants and variables at the top of the main functionUse good identifiers to make code "Self-Documenting"Follow the identifier rules:Should begin with a letter, followed by letters, digits and underscoresAre
View Full Document