COMP 102 Programming Fundamentals I Presented by : Timture Choi COMP102 Lab 011
Introduction to C++ C++ Extension of C Supports OO programming Free-format language General form Comment Include necessary header files “main()” function COMP102 Lab 012
Comments Provides explanatory notes Purpose of the program/each function Usage of each variable Appear in green in VC++ 1. Single line comments // 2. Multiple line comments /* … */ COMP102 Lab 013
Compiler Directives Appear in blue in VC++ Import the required library in program Pre-defined #include E.g., User-defined #include “xxx.h” COMP102 Lab 014
“main()” Function Start of the program Variables/constant declaration Must be declared before used Identifier Name for variables/constant/function Should not be a keyword Case sensitive Start with letter Statements Deal with user input/output Specific calculation COMP102 Lab 015
Programming Style Free-format language Increase writability How easily a language can be used to write programs Decrease readability Measures how easy it can be understood Note: Write comments for both variables and functions Use meaningful variable names Only one statement for each line COMP102 Lab 016
Declarations Allocation Reserves memory to store constants and variables Deallocation Releases storage of constants and variables from memory Binding Associates the constants/variables with memory location COMP102 Lab 017
Declarations Constant Cannot be changed throughout the program Syntax: const = ; const: keyword E.g. const double pi=3.14, gravity=9.8; COMP102 Lab 018
Declarations Variable Can be changed during program execution Note: will not be initialized automatically Better to initialize when declared Syntax: ; , ; = ; COMP102 Lab 019
Data Type void no value and operation int Integer 32 bits (4 bytes) Float Floating point 32 bits (4 bytes) Double 64 bits (8 bytes) char Character bool Either true/false COMP102 Lab 0110
Data Type Note: Precision Data size Depend on design/compiler of the language Bytes/Bits Overflow/Underflow Affect program size Data range Signed/Unsigned Signed bit COMP102 Lab 0111
SUMMARY By the end of this lab, you should be able to: Understand the general form of a program Comments Compiler directives “main()” function Keyword and identifier Defined and Initialized Constants Variables With appropriate data types COMP102 Lab 0112