Upload
berniece-vivian-hubbard
View
229
Download
0
Embed Size (px)
Citation preview
1
FP201 Programming Fundamentals
•Introduction To C++ Programming1.0•Basic C++ Program Structure2.0•Program Control3.0•Array And Structures4.0•Function5.0•Pointer6.0•Secure Programming in C++7.0
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
2
2.0 BASIC C++ PROGRAM STRUCTURE
2.1 Understand The C++ Program Basic Structure
FP201 PROGRAMMING FUNDAMENTALS
3
LEARNING OUTCOMES TOPIC 2.1
• Explain the functionality of each item in C++ program structure 1
By the end of this chapter students shall be able to :
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
// my first program in C++ #include <iostream> using namespace std; int main ()
{cout << "Hello World!“<<endl;
return 0; }
comment
PreprocessorDirectives
Main function
braces
output
Return statement
Header file
4
C++ PROGRAM STRUCTURE
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
a) Preprocessor Directives▫such as #define , are typically used to make source
programs easy to change and easy to compile. Directives in the source file tell the preprocessor to perform specific actions.
▫Example:
#define LENGTH 10 var = LENGTH * 20;
FUNCTION OF THE STRUCTURE
5
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
b) Header Files▫.h files or "headers" are libraries of code you may
insert in your program by including them by reference in the top block.
▫Example:#include< stdio.h >#include< iostream.h >
Cont…
6
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
•The point of header files is to create libraries of code that can be used over and over▫#include< stdio.h > is the Standard Input and Output
header file. Expl : printf, scanf.▫#include< iostream.h > is the Input and Output Stream
header file. Expl : cout, cin.
Cont…
7
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
•Types of Header Files (cont’)
1. iostream.h -Input/Output, interaction with the program. Expl : getline.
2. math.h –For using mathematic formula 3. string.h- C++ has no built-in string handling. Use this
library to copy strings, find string length, etc. 4. stdio.h- Similar to stdlib but also has some file
functions. printf, scanf(for C language)
Cont…
8
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
c) Main Function• Is the place where the code execution begins.
int main() { return 0;}
Cont…
9
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
void main() { }
d) Return Statements•Terminates the execution of a function and returns
control to the calling function (or, in the case of the main function, transfers control back to the operating system). Execution resumes in the calling function at the point immediately following the call.
Cont…
10
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
11
2.0 BASIC C++ PROGRAM STRUCTURE
2.2 Classify Identifier and Keyword FP201 PROGRAMMING FUNDAMENTALS
12
LEARNING OUTCOMES TOPIC 2.2
• Explain identifier, variable and constant.
1
• State the naming convention rules for identifier.
2
•Declare variables and constants. 3
•Initializes variables 4
•Determine identifier scope: local, global. 5
•Explain keywords6
By the end of this chapter students shall be able to :
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
• Identifier : A name associated with a variable and constant, usually limited to letters,digits and underscores.
•Variables are identifiers whose value may change during the course of execution of a program.
•Variable represent memory location where you can store value such as characters, numbers and pointers.
•Constants are identifiers whose value is kept permanently for program to use
•Remain unchanged throughout a program
13
IDENTIFIER, VARIABLE AND CONSTANT
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
1. Identifiers must begin with a letter or underscore. ▫Only letters(A-Z,a-z), digits(0-9), or underscore( _ ) may
follow the initial letter. ▫Ex: sum_of_squares, box_22A, GetData
2. The blank space cannot be used. ▫Ex: Get Data cannot be an identifier as blanks are not
allowed in identifiers
14
NAMING CONVENTION RULES FOR IDENTIFIER
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
3. Identifiers cannot be reserved words. Reserved words or keywords are identifiers reserved for system use. ▫Ex: int......cannot be an identifier as it is a reserved
word in C++
4. Identifiers beginning with an underscore have special meaning in some C++ systems, so it is best not to start your identifiers with an underscore.
15
Cont..
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
5. Most C++ compilers will recognize the first 32 characters in an identifier.
Also, C++ is a case sensitive language. C++ will distinguish between upper and lower case letters in identifiers. Therefore:
grade and Grade are different identifier
6. Avoid excessively long identifiers.
16
Cont..
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
7. You can develop your own conventions like below: ▫A single word identifier will be written in lower case
only. Examples: grade, number, sum.
▫If an identifier is made up of several words, the first letter will be lower case. Subsequent words will begin with upper case. Some examples are: stringType, passingScore, largestNum.
▫Identifiers used as constants are often fully capitalized. Examples: PI, MAXSTRLEN.
17
Cont..
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
•A C++ variable is declared by telling the program:▫the data type to be used ▫the name of the variable▫Syntax : dataType variableName;▫Examples:
int Marks;Data Type Variable
DECLARE VARIABLES AND CONSTANT
18
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
•To declare more than one variable of the same data type.
• For example:
int a, b, c;
Cont..
19
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
•Constants are expressions with a fixed value.▫To declare constant (#define) :
#define constName value
For example:
#define PI 3.14159
Cont..
20
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
▫To declare constant (const)
▫Syntax:const dataType constName = value;Example:
const int PATH = 100;
Cont..
21
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
• It is done by appending an equal sign followed by the value to which the variable will be initialized:
type identifier = initial_value ;
For example, :
int a = 0;
INITIALIZE VARIABLES
22
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
•A variable can be either of global or local scope. •A global variable is a variable declared in the main
body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.
LOCAL AND GLOBAL VARIABLES
23
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
Cont..
24
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
#include<iostream.h>
int main(){
int a; // This is a variable
int calArea(int b,int c); //This is a function declaration
cout << "Please enter a value : ";cin >> a;
return 0;}
Global variable
Localvariable
•Keywords are predefined reserved identifiers that have special meanings.
•They cannot be used as identifiers in your program.•Example:
▫void▫main▫float
KEYWORD
25
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
26
2.0 BASIC C++ PROGRAM STRUCTURE
2.3 Classify Data Types FP201 PROGRAMMING FUNDAMENTALS
27
LEARNING OUTCOMES TOPIC 2.3
• Explain the basic data types: char, int, double, float, bool 1
By the end of this chapter students shall be able to :
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
1. int:• is used to declare integers, whole numbers either
positive or negative. The following statement shows how the variables of int type are declared.
int var1; int b = 10;
BASIC DATA TYPES
28
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
2. long:• This long keyword is used for declaring longer
numbers. 3. float:• This keyword float is used to declare floating point
decimal numbers. A sample declaration would be:
float var2; //Sample declaration for floator float var2 = 2.34;
Cont…
29
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
4. char:• This keyword is used to declare characters. The
characters that can be used with this data type are ASCII characters.
char a[9]=“Malaysia”; char a[]=“Malaysia”;
char noPhone[11]=“0134022331”;
Cont…
30
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
5. String:• Must include header file string.h
#include <iostream.h>#include <string.h>
Int main(){
………..string mystring = "This is a string";……return 0;
}
Cont…
31
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
6. Boolean•To declare a boolean variable, we use the keyword bool.
bool bValue;
•When assigning values to boolean variables, we use the keywords true and false.
bool bValue1 = true;
Cont…
32
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
33
2.0 BASIC C++ PROGRAM STRUCTURE
2.4 Use Input Output Statements FP201 PROGRAMMING FUNDAMENTALS
34
LEARNING OUTCOMES TOPIC 2.4
• Identify the syntax use for input and output.1
• Write algorithm and programs that use the input and output statements.2
By the end of this chapter students shall be able to :
F2037 PROGRAMMING FUNDAMENTALS WITH C++
© 2010/2011 | PN NORHASLIZA BT MUHAMAD NOR
35
• Use cin >>• Must have header file iostream.h
#include <iostream.h>void main(){int n1, n2, n3;cout << "Enter 3 numbers : ";cin >> n1 >> n2 >> n3;
}
INPUT STATEMENTS
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
36
#include <iostream.h>void main(){int n1, n2, n3;cout << "Enter 3 numbers : ";cout << “First Number : ";cin >> n1 ;cout << “Second Number : ";cin >> n2 ;cout << “Third Number : ";cin >> n3 ;
}
INPUT STATEMENTS
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
37
• Use cout <<• Must have header file iostream.h
#include <iostream.h>void main(){int n1, n2, n3;cout << "Enter 3 numbers : ";cin >> n1 >> n2 >> n3;
}
OUTPUT STATEMENTS
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
38
#include <iostream.h>void main(){int n1;cout << "Enter 1 numbers : ";cin >> n1 ;cout << “The number you enter : “<< n1;
}
OUTPUT STATEMENTS
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
39
2.0 BASIC C++ PROGRAM STRUCTURE
2.5 Apply Operators & Expression
FP201 PROGRAMMING FUNDAMENTALS
40
LEARNING OUTCOMES TOPIC 2.5
• Explain Arithmetic , Assignment, Increment & Decrement, Relational and logical Operator
1
• Explain operators precedence 2
• Write expression using operator3
• Use expression in program4
By the end of this chapter students shall be able to :
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
41
• The five arithmetical operations supported by the C++ language are:
+ addition - subtraction * multiplication / division % modulus
ARITHMETIC OPERATOR
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
42
a=5value+= increase value = value +
increase; a -= 5 a = a - 5; a /= b a = a / b; price *= units + 1 price = price * (units
+ 1);
• Refer to the symbol (=),equal.• 1)Use to give the value to variable• 2)or to modify the value of a variable by performing an operation
on the value currently stored in that variable :
ASSIGNMENT OPERATOR
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
43
1)Operasi Unari Prefix
2)Operasi Unari Postix
a ++;a --;
++ a; -- a;
Cth :int x = 180, y =200;y= x++;cout<<“x:”<<x<<endl<<“y:”<<y<<endl;
Output: x : 181y : 180
Cth :int x = 180, y =200;y= ++x;cout<<“x:”<<x<<endl<<“y:”<<y<<endl;
Output: x : 181y : 181
INCREMENT & DECREMENT OPERATOR
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
Operator Meaning Example
== Equal to 4 = =4
< Less than 2 < 7
> Greater than 8 > 3
<= Less than or equal to 6 < = 7
>= Greater than or equal to 13 > = 5
!= Not equal to 6 != 2
RELATIONAL OPERATOR
44
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
45
Assignment and Relational operators
= is not the same as the operator
==assigns the value at its right to the variable at its left
the equality operator that compares whether both expressions in the two sides of it are equal to each other
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
X Y x or y||
x And y&&
Not x
True True True True False
True False True False False
False True True False True
False False False False True
LOGICAL OPERATOR
46
FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
47
• The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is:
condition ? result1 : result2
7>5 ? 3 : 2Answer : 3
CONDITIONAL OPERATOR ?:FP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR
48
OPERATORS PRECEDENCEFP201 PROGRAMMING FUNDAMENTALS
© 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR