Docsity

Object Oriented programing, Slides of Object Oriented Programming

Partial preview of the text

Download Object Oriented programing and more Slides Object Oriented Programming in PDF only on Docsity!

CCC- 302

OBJECT ORIENTED PROGRAMMING

LECTURE # 1

  • A pointer variable must be declared before it can be used.
  • Pointer declarations: int *a; float *b; char *c;
  • Asterisk * tells compiler that variable is a pointer.

POI NT E R S

Variables contain a value, while pointers contain a memory address.

  • Memory address:
    • We know each memory address contain some value.
    • Pointers contain memory addresses, and we can get its value (indirectly).
  • Unary operator * is dereferencing operator applied on pointers
  • Access value of object referred by the pointer
  • The statement *P = 5 puts the value 5 in a variable pointed by P

DE R E FER ENCING

cout << *P; *P = 177; P = 177; // This is unadvisable C … 7 3 4 … 172 173 174 175 176 177 178 179 180 181 … 174 3 4 … P 832 833 834 835 836 837 838 839 840 841 177 177

  • Memory is divided into bytes which are further divided into bits
    • Each bit can have a value of 0 or 1
    • A byte is eight bits
      • Can hold any value from 0 to 255
    • Memory can be thought of as a sequence of bytes starting at 0

BI T S AND BY T E S

  • Pointing to something that already exists: int *ptr, var1, var2; var1 = 5; ptr = &var1; var2 = *ptr;
  • var1 and var2 have room implicitly allocated for them.

ptr var

?

var

? 5? 5

POI NT E R S AND AL L OC AT I ON

  • Arrays and pointers are closely related
    • Array name is like a constant pointer
    • Array elements are placed in consecutive locations. (Only valid in static memory allocation)
      • Example: int List [10];
    • Pointers can do array subscripting operations (Access array elements using pointers)
      • Example: int *ptr = List;

RE LATIONSHIP BE TWE EN POI NTE RS AND ARRAYS

  • Array name denotes memory address of its first slot.
    • int List [ 50 ];
    • int *Pointer;
    • Pointer = List;
  • Other slots of array List [50] can be accessed by performing arithmetic operations on Pointer.
  • Address of ( th element) can be accessed:
    • int *Value = Pointer + 3;
  • Value of ( th element) can be accessed:
    • int Value = *(Pointer + 3); Address Data 980 Element 0 982 Element 1 984 Element 2 986 Element 3 988 Element 4 990 Element 5 992 Element 6 994 Element 7 996 Element 8 998 Element 50 … …

ACCE SSI NG 1 - DE MENSIONAL AR R AY USI NG POI NT E R S

const int MAX = 4; int main () { char *names[MAX] = { “Amir", "Ali", “Akbar", “Abid", }; for (int i = 0; i < MAX; i++) { cout << “Names[" << i << "] = "; cout << names[i] << endl; } } Output Names[0] = Amir Names[1] = Ali Names[2] = Akbar Names[3] = Abid

AR R AY OF POI NT E R S

  • When an integer (n) is added/subtracted from a pointer (ptr)
    • New pointer value (ptr) is changed by ptr address plus n multiple (*)
    • ptr + n * (bytes of ptr data type)
  • Example
    • int *ptr;
    • ptr = 1000;
    • ptr = ptr + 2; // ptr address is now changed to 1000 + 2 * 4 (int takes 4 bytes) and new address is now 1008

ARITHME TIC ON POI NTE RS

  • Example (1)
    • long *ptr;
    • ptr = 1000;
    • ptr++; 1000 + 1*4 = 1004
  • Example (2)
    • float *ptr;
    • ptr = 1000;
    • ptr+=3; 1000 + 3*4 = 1012
  • Example (3)
    • int *ptr;
    • int num1 = 0;
    • ptr = &num1;
    • ptr++; 1002 + 4 = 1006 Memory width is 2 bytes Address data 1000 1002 Num1= 1004 1006 1008

AR I T H ME T IC ON POI NT E R S

  • We can apply logical operators (<, >, <=, >=, ==, != ) on pointers.
    • Pointers can be compared to pointers or NULL
  • Example (5)
    • int *pointer1, *pointer2; // both pointer2 contains NULL addresses
    • int num1 = 93;
    • If ( pointer1 == NULL ) // pointer compared to NULL pointer1 = &num1; pointer2 = &num1;
    • If ( pointer1 == pointer2 ) // pointer compared to pointer printf(“Both pointers are equal”);

L OG I C AL OPE R ATOR S ON POI NT E R S

E XAMPL E

  • int *Pointer;
  • Pointer = &List [3];
    • Represents we are accessing address of 4th slot.
    • In a 2-Demensional array the statements
  • int List [ 5 ] [ 6 ];
  • int *Pointer;
  • Pointer = &List [3];
  • Represents we are accessing address of 4 th row and 1 st column equivalent to List [ 3 ][ 1 ] Address Data 980 Element 0 982 Element 1 984 Element 2 986 Element 3 988 Element 4 990 Element 5 992 Element 6 994 Element 7 996 Element 8 998 Element 50 … …

ACCE SSI NG 2 - DI ME NSI ONAL AR R AY

300 302 304 306 308 310 312 314 316 318 320 322 324 326 328 330 332 334 336 338 340 342 344 346 348 350 352 354 356 358 360 362 364 366 368 370 372 374 376 378 380 382 384 386 388 390 392 394 396 398 400 402 404 406 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 Column Row

ACCE SSI NG 2 - DI ME NSI ONAL AR R AY

  • Computer can perform only one operation at any time (fetch-decode-execute cycle).
  • Thus, to access List [ 3 ][ 1 ] element (without pointer) two operations are involved:
    • First to determine row List [3]
    • Second to determine column List[3][1]
  • But using pointer we can reach element of 4 th^ row 2 nd column (directly) by increment our pointer value (which is a single operation).
    • ptr++; // 4th^ row 2nd column
    • ptr+1; // 4 th row 3rd column
    • ptr+2; // 4 th row 4th column