Week 6 Flashcards Preview

OOP (UTS) > Week 6 > Flashcards

Flashcards in Week 6 Deck (14)
Loading flashcards...
1
Q

4 jenis passing parameters?

A
  1. passing by value
  2. passing by address
  3. passing by reference
  4. passing by const reference
2
Q

what is reference?

A

reference adalah alias or synonym for another variable. server as alternative name for an object. harus di inisialisasi pada saat declared. Ex: int &count.

3
Q

Perbedaan pointer dengan reference?

A

Pointer:

  1. tidak perlu diinisialisasikan pada saat definisi
  2. dapat di assign nilai baru untuk object yang berbeda

Reference:

  1. harus diinisialisasikan pada saat definisi
  2. selalu refer ke object yang sama
4
Q

a constant reference variable var?

A

refer ke object yang nilai yang tidak dapat di ganti dengan var.

5
Q

r is a reference for n?

A

int& r = *p;

6
Q

operator * disebut?

A

dereferencing operator

7
Q

3 jenis memory?

A
  1. static memory untuk global dan static variables live
  2. heap memory dialokasikan secara dinamis pada saat execution time. (unnamed variable)
  3. stack memory untuk automatic variable dan function parameters
8
Q

3 jenis program data?

A
  1. static data, dialokasikan pada saat compiler time
  2. dynamic data, secara explicit di alokasikan dan deallocated pada saat program execution. dengan operator new and delete
  3. automatic data, secara otomatis dibuat pada function entry dan di hancurkan pada saat kembali ke function
9
Q

dangling pointer?

A

a pointer that does not point to anything. uninitialized and de-allocated pointer

10
Q

contoh dangling pointer?

A

int *p = new int;

11
Q

contoh passing by value?

A
void calculate (int a, int b){
       a = a+1;
       int c = a + b;
       cout << "c = " <<endl;
}
12
Q

contoh passing by address?

A
void calculate
       *a = *a+1;
       int c = *a + *b;
       cout << "c = " <<endl;
}
13
Q

contoh passing by reference?

A
void swap(int& x, int& y) {
   int temp = x;
   x = y;
   y = temp;
}
14
Q

contoh passing by const reference?

A
void calculate
        a = a+1; //error : cannot modify const object
        int c = a + b;
        cout << "c = " <<endl;
}