Oop lec 1


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Oop lec 1

  1. 1. Review of Basic Programming Concepts Lecture No 1 COMSATS Institute of Information Technology 1 Object Oriented Programming
  2. 2. Basics of Typical C Program Development Environment A program development environment The language C Standard Library 2 Object Oriented Programming
  3. 3. Six phases to be executed Edit Preprocessor Compile Link Load Execute 3 Object Oriented Programming
  4. 4. Stages of compilation Preprocessing Performed by a program called the preprocessor Modifies the source code (in RAM) according to preprocessor directives (preprocessor commands) embedded in the source code Strips comments and white space from the code 4 Object Oriented Programming
  5. 5. Compilation Performed by a program called the compiler Translates the preprocessor-modified source code into object code (machine code) Checks for syntax errors and warnings o If any compiler errors are received, no object code file will be generated. o An object code file will be generated if only warnings, not errors, are received. 5 Object Oriented Programming
  6. 6. Linking Combines the program object code with other object code to produce the executable file. The other object code can come from the Run-Time Library, other libraries, or object files that you have created. Saves the executable code to a disk file o If any linker errors are received, no executable file will be generated. 6 Object Oriented Programming
  7. 7. Simple c program /* Filename: product.c Description: This program prompts the user for two integer values then displays their product. */ #include <stdio.h> int main( void ) { int value1, value2, product ; printf(“Enter two integer values: “) ; scanf(“%d%d”, &value1, &value2) ; product = value1 * value2 ; printf(“Product = %dn”, product) ; return 0 ; } 7 Object Oriented Programming
  8. 8. Anatomy of c program program header comment preprocessor directives (if any) int main ( void ) { statement(s) return 0 ; } 8 Object Oriented Programming
  9. 9. A comment is descriptive text used to help a reader of the program understand its content. Lines that begin with a # are called preprocessor directives (commands). Every program must have a function called main. This is where program execution begins. A left brace (curly bracket) -- { -- begins the body of every function. A corresponding right brace -- } -- ends the function body. 9 Object Oriented Programming
  10. 10. int value1, value2, product; Declaration of variables  Variables: locations in memory where a value can be stored int means the variables can hold integers (-1, 3, 0, 47) Variable names (identifiers)  value1, value2, product  Identifiers: consist of letters, digits (cannot begin with a digit) and underscores( _ )  Case sensitive Declarations appear before executable statements  If an executable statement references and undeclared variable it will produce a syntax (compiler) error 10 Object Oriented Programming
  11. 11. scanf("%d%d", &value1,&value2); Obtains values from the user  scanf uses standard input (usually keyboard) This scanf statement has two arguments  %d - indicates data should be a decimal integer  &value1 - location in memory to store variable When executing the program the user responds to the scanf statement by typing in a number, then pressing the enter (return) key 11 Object Oriented Programming
  12. 12. = (assignment operator) Assigns a value to a variable Is a binary operator (has two operands) product = value1 * value2; Variable receiving value on left printf( “Product is %dn", product); Similar to scanf  %d means decimal integer will be printed  product specifies what integer will be printed 12 Object Oriented Programming
  13. 13. Arithmetic Arithmetic operators: C operation Addition Subtraction Multiplication Division Modulus Arithmetic operator + * / % Algebraic expression f+7 p–c bm x/y r mod s Rules of operator precedence: Operator(s) () f p b x r + * / % 7 c m y s Order of evaluation (precedence) Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated left to right. Multiplication,Divi Evaluated second. If there are several, they are *, /, or % sion, Modulus evaluated left to right. Addition Evaluated last. If there are several, they are + or Subtraction evaluated left to right. Object Oriented Programming 13 Operation(s) Parentheses C expression
  14. 14. Decision Making: Equality and Relational Operators  Executable statements  Perform actions (calculations, input/output of data)  Perform decisions  May want to print "pass" or "fail" given the value of a test grade  if control structure  Simple version in this section, more detail later  If a condition is true, then the body of the if statement executed  0 is false, non-zero is true  Control always resumes after the if structure  Keywords  Special words reserved for C  Cannot be used as identifiers or variable names 14 Object Oriented Programming
  15. 15. Decision Making: Equality and Relational Operators Standard algebraic equality operator or relational operator C equality or relational operator Example of C Meaning of C condition condition == != x == y x is equal to y x != y x is not equal to y x > y x is greater than y < > < x < y x is less than y >= >= x >= y x is greater than or equal to y <= <= x <= y x is less than or equal to y Equality Operators = not = Relational Operators > 15 Object Oriented Programming
  16. 16. Control Structures Control Flow In C, three ways to specify sequence of statement execution Sequential Execution Conditional Execution Iterative Execution 16 Object Oriented Programming
  17. 17. Conditional Execution One-way selection or if Two-way selection or if-else Multiple Selection or nested if 17 Object Oriented Programming
  18. 18. One-way selection or if If(expression) statement1; If(x>10) y=z/x; y=z*x; If(x>y) printf(“the largest is %d”,x); 18 Object Oriented Programming
  19. 19. Two-way selection or if-else If(expression) statement1; else statement2; If(x>y) printf(“the largest is %d”,x); else printf(“the largest is %d”,y); 19 Object Oriented Programming
  20. 20. Multiple Selection or nested if If(condition1) { if(condition2) statement1; } else statement2; 20 Object Oriented Programming
  21. 21. Iterative Execution The while statement The for statement The do-while statement 21 Object Oriented Programming
  22. 22. The while statement  While(expression) statement1; #include<stdio.h> int main() { Int k, num, maximum=10; k=1; while(k<=maximum) { scanf(“%d”, &num); printf(“%4d”, num); k++; } return 0; } 22 Object Oriented Programming
  23. 23. The for statement for(expression1;expression2;expression3) statement1; expression1: initial value expression2: boolean expression expression3: update of expression 23 Object Oriented Programming
  24. 24. Example #include<stdio.h> int main() { int sum, n, i; sum = 0; scanf(“%d”, &n); for(i=1; i<=n; i++) sum = sum + i; printf(“Sum=%dn”,sum); return 0; } 24 Object Oriented Programming
  25. 25. The do-while statement do statement1 while(expression); 25 Object Oriented Programming
  26. 26. Example #include<stdio.h> int main() { int sum, num; sum = 0; num=1; do { sum = sum + num; num++; } while(num <= 10); printf(“Sum=%dn”,sum); return 0; } 26 Object Oriented Programming
  27. 27. Other Control Structures switch break continue 27 Object Oriented Programming
  28. 28. Example #include<stdio.h> int main() { int n; printf(“Enter an integer valuen” ); scanf(“%d”, &n); switch(n) { case 1: printf(“The value is 1n”); break; case 2: printf(“The value is 2n”); break; default: printf(“The value is neither 1 nor 2n”); } return 0; } 28 Object Oriented Programming
  29. 29. Functions An independent set of statements for performing some special tasks Attributes Name of function (user-defined) Parameters Variable declaration (if any) The body of function 29 Object Oriented Programming
  30. 30. Divisions of Function Prototype int largest(int x, int y); Definition Head function itself Invocation large = largest (x, y); 30 Object Oriented Programming
  31. 31. Types of Function  System-supplied functions To instruct system to accomplish specific operations Saves programmers time to writing own functions Completely debugged, efficient and always produce precise output Reduce source code printf(“%d”, x);  User-defined functions Defined by programmer as part of source code Result-type user-defined-name(parameters) 31 Object Oriented Programming
  32. 32. User-defined function #include<stdio.h> int retlarge(int num1, int num2); int main() { int num1, num2, large; scanf(“%d%d”, &num1, &num2) ; printf(“%d %dn”, num1, num2) ; large = retlarge(num1, num2); printf(“large = %dn”, large); return 0; } int retlarge(int x, int y) { if (x>y) return(x); else return(y); } 32 Object Oriented Programming
  33. 33. Methods for transmitting parameters Passing by value treated as local variables the initial value is obtained by copying the values of associated actual parameters Passing by location passing by address or reference linking actual parameters to formal parameters by passing address of actual parameters from the calling program to called program 33 Object Oriented Programming
  34. 34. Arrays  Group of elements of same type.  Collection of similar data type stored in contiguous memory location.  To refer to an element, specify Name of array (Note that all elements of this array have the same name, c) c[0] -45 c[1] 6 c[2] 0 c[3] 72 c[4] 1543 c[5] -89 arrayname[ position number ] c[6] 0  First element at position 0  n element array named c:  c[ 0 ], c[ 1 ]...c[ n – 1 ] c[7] 62 c[8] -3 c[9] 1  Array name  Position number  Format: c[10] c[11] 34 Object Oriented Programming 6453 78 Position number of the element within array c
  35. 35. Declaring Arrays  When declaring arrays, specify Name Type of array Number of elements arrayType arrayName[ numberOfElements ]; Examples: int c[ 10 ]; float myArray[ 3284 ];  Declaring multiple arrays of same type Example: int b[ 100 ], x[ 27 ]; 35 Object Oriented Programming
  36. 36.  Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 }  All elements 0 If too many a syntax error is produced syntax error C arrays have no bounds checking  If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array 36 Object Oriented Programming
  37. 37.  Character arrays  String “first” is really a static array of characters  Character arrays can be initialized using string literals char string1[] = "first";  Null character '0' terminates strings  string1 actually has 6 elements  It is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '0' };  Can access individual characters string1[ 3 ] is character ‘s’  Array name is address of array, so & not needed for scanf scanf( "%s", string2 );  Reads characters until whitespace encountered  Can write beyond end of array, be careful 37 Object Oriented Programming
  38. 38. Array Subscripting Formula Address of i-th element = address of 0-th element + i * (size of type) Assuming declaration int intarr[10]; Assuming address of array intarr is located at address 10 in memory Accessing element intarr[5] can be calculated by formula: Address of i-th element = 10 + 5 * 2 = 20 38 Object Oriented Programming
  39. 39. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer indirectly references a value. Referencing a value through a pointer is called indirection. A pointer variable must be declared before it can be used. 39 Object Oriented Programming
  40. 40.  Memory can be conceptualized as a linear set of data locations.  Variables reference the contents of a locations  Pointers have a value of the address of a given location  A declaration int intval=10; Then int ptr can be set as int *ptr; Pointing to intval by ptr = &intval; Address of intval assigned to ptr, not contents! 40 Object Oriented Programming
  41. 41. & and *  When is & used?  When is * used?  & -- "address operator" which gives or produces the memory address of a data variable  * -- "dereferencing operator" which provides the contents in the memory location specified by a pointer 41 Object Oriented Programming
  42. 42. Pointers and Function Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there. If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function can change the values stored in the calling routine. This is known as "call by reference" since we are referencing the variables. 42 Object Oriented Programming
  43. 43. Passing pointers to function #include <stdio.h> void exchange ( int *a, int *b ) ; int main ( ) { int a = 5, b = 6; printf("a=%d b=%dn",a,b) ; exchange (&a, &b) ; printf("a=%d b=%dn",a,b) ; return 0 ; } 43 Object Oriented Programming
  44. 44. void exchange( int *a, int *b ) { int temp; temp= *a; *a= *b; *b = temp ; printf ("a=%d b=%dn", *a, *b); } Results: a=5 b=6 a=6 b=5 a=6 b=5 44 Object Oriented Programming