Post on 08-Jan-2018
description
COMP 102
Programming Fundamentals I
Presented by : Timture Choi
COMP102 Lab 01 1
Introduction to C++ C++ Extension of C Supports OO programming Free-format languageGeneral form Comment Include necessary header files “main()” function
COMP102 Lab 01 2
CommentsProvides explanatory notes
Purpose of the program/each function Usage of each variableAppear in green in VC++
1. Single line comments //
2. Multiple line comments /* … */
COMP102 Lab 01 3
Compiler DirectivesAppear in blue in VC++Import the required library in programPre-defined #include <xxx> E.g. <iostream>, <math>User-defined #include “xxx.h”
COMP102 Lab 01 4
“main()” FunctionStart of the programVariables/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 01 5
Programming StyleFree-format language Increase writability
How easily a language can be used to write programs Decrease readability
Measures how easy it can be understoodNote: Write comments for both variables and
functions Use meaningful variable names Only one statement for each line
COMP102 Lab 01 6
DeclarationsAllocation Reserves memory to store constants and
variablesDeallocation Releases storage of constants and variables
from memoryBinding Associates the constants/variables with
memory location
COMP102 Lab 01 7
DeclarationsConstant Cannot be changed throughout the
program Syntax:
const <type> <identifier> = <expression>;
const: keyword E.g.
const double pi=3.14, gravity=9.8;
COMP102 Lab 01 8
DeclarationsVariable Can be changed during program
execution Note: will not be initialized automatically
Better to initialize when declared Syntax:
<type> <identifier>; <type> <identifier>, <identifier>; <type> <identifier> = <expression>;
COMP102 Lab 01 9
Data Typevoid
no value and operationint
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 01 10
Data TypeNote: Precision Data size
Depend on design/compiler of the languageBytes/BitsOverflow/UnderflowAffect program size
Data range Signed/Unsigned
Signed bit
COMP102 Lab 01 11
SUMMARYBy 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 01 12