Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.

Similar presentations


Presentation on theme: "Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming."— Presentation transcript:

1 Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming in C++ 7.0 1 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

2 2

3 Explain the functionality of each item in C++ program structure 1 3 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

4 // my first program in C++ #include using namespace std; int main () { cout << "Hello World!“<<endl; return 0; } comment Preprocessor Directives Main function braces output Return statement Header file 4 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

5 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; 5 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

6 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 #include 6 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

7 The point of header files is to create libraries of code that can be used over and over ▫ #include is the Standard Input and Output header file. Expl : printf, scanf. ▫ #include is the Input and Output Stream header file. Expl : cout, cin. 7 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

8 Types of Header Files (cont’) 1.iostream.h -Input/Output, interaction with the program. Expl : getline.iostream.h 2.math.h –For using mathematic formulamath.h 3.string.h- C++ has no built-in string handling. Use this library to copy strings, find string length, etc.string.h 4.stdio.h- Similar to stdlib but also has some file functions. printf, scanf(for C language)stdio.h 8 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

9 c) Main Function Is the place where the code execution begins. int main() { return 0; } 9 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR void main() { }

10 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. 10 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

11 11

12 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 keywords 6 12 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

13 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 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

14 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 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

15 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 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

16 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 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

17 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 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

18 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 TypeVariable 18 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

19 To declare more than one variable of the same data type. For example: int a, b, c; 19 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

20 Constants are expressions with a fixed value. ▫ To declare constant (#define) : #define constName value For example: #define PI 3.14159 20 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

21 ▫ To declare constant (const) ▫ Syntax: const dataType constName = value; Example: const int PATH = 100; 21 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

22 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; 22 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

23 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. 23 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

24 24 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR #include 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; } #include 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 Local variable

25 Keywords are predefined reserved identifiers that have special meanings. They cannot be used as identifiers in your program. Example: ▫ void ▫ main ▫ float 25 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

26 26

27 Explain the basic data types: char, int, double, float, bool 1 27 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

28 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; 28 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

29 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 float or float var2 = 2.34; 29 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

30 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”; 30 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

31 5. String: Must include header file string.h #include Int main() { …… ….. string mystring = "This is a string"; …… return 0; } 31 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

32 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; 32 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

33 33

34 Identify the syntax use for input and output. 1 Write algorithm and programs that use the input and output statements. 2 34 © 2010/2011 | PN NORHASLIZA BT MUHAMAD NOR

35 Use cin >> Must have header file iostream.h #include void main() { int n1, n2, n3; cout << "Enter 3 numbers : "; cin >> n1 >> n2 >> n3; } 35 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

36 #include 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 ; } 36 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

37 Use cout << Must have header file iostream.h #include void main() { int n1, n2, n3; cout << "Enter 3 numbers : "; cin >> n1 >> n2 >> n3; } 37 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

38 #include void main() { int n1; cout << "Enter 1 numbers : "; cin >> n1 ; cout << “The number you enter : “<< n1; } 38 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

39 39

40 Explain Arithmetic, Assignment, Increment & Decrement, Relational and logical Operator 1 Explain operators precedence 2 Write expression using operator 3 Use expression in program 4 40 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

41 The five arithmetical operations supported by the C++ language are: +addition - subtraction *multiplication / division % modulus 41 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

42 a=5 value+= increase value = value + increase; a -= 5a = a - 5; a /= ba = a / b; price *= units + 1price = 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 : 42 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

43 1)Operasi Unari Prefix 2)Operasi Unari Postix 43 a ++; a --; ++ a; -- a; Cth : int x = 180, y =200; y= x++; cout<<“x:”<<x<<endl<<“y:”<<y<<endl; Output: x : 181 y : 180 Cth : int x = 180, y =200; y= ++x; cout<<“x:”<<x<<endl<<“y:”<<y<<endl; Output: x : 181 y : 181 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

44 OperatorMeaning 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 44 © 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 45 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

46 XYx or y || x And y && Not x True False TrueFalseTrueFalse True FalseTrue False True 46 © 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 : 2 Answer : 3 47 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

48 48 © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR


Download ppt "Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming."

Similar presentations


Ads by Google