Pointer Lanjutan Pertemuan 11 Bahasa C C POINTER
Pointer (Lanjutan) Pertemuan 11 Bahasa C & C++
POINTER MENUNJUK SUATU ARRAY Contoh : #include “stdio. h” #include “conio. h” void main() { static int tgl_lahir[] = { 13, 9, 1982 }; int *ptgl; ptgl = tgl_lahir; /* ptgl berisi alamat array */ printf(“Diakses dengan pointer”); printf(“Tanggal = %in”, *ptgl); printf(“Bulan = %in”, *(ptgl + 1)); printf(“Tahun = %in”, *(ptgl + 2)); printf(“n. Diakses dengan array biasan”); printf(“Tanggal = %in”, tgl_lahir[0]); printf(“Bulan = %in”, tgl_lahir[1]); printf(“Tahun = %in”, tgl_lahir[2]); getch(); }
MEMBERI NILAI ARRAY DENGAN POINTER Contoh #include “stdio. h” #include “conio. h” void main() { int x[5], *p, k; clrscr(); p = x; x[0] = 5; /* x[0] diisi dengan 5 sehingga x[0] = 5 */ x[1] = x[0]; /* x[1] diisi dengan x[0] sehingga x[1] = 5 */ x[2] = *p + 2; /* x[2] diisi dengan x[0] + 2 sehingga x[2] = 7 */ x[3] = *(p+1) – 3; /* x[3] diisi dengan x[1] - 3 sehingga x[3] = 2 */ x[4] = *(x + 2); /* x[4] diisi dengan x[2] sehingga x[4] = 7 */ for(k=0; k<5; k++) printf(“x[%i] = %in”, k, x[k]); getch(); }
Demonstrates use of an array of pointers to functions. (C++) Contoh : // demonstrates use of an array of pointers to functions #include <iostream. h> void Square (int&, int&); void Cube (int&, int&); void Swap (int&, int &); void Get. Vals(int&, int&); void Print. Vals(int, int); enum BOOL { FALSE, TRUE }; int main() { int val. One=1, val. Two=2; int choice, i; const Max. Array = 5; void (*p. Func. Array[Max. Array])(int&, int&); for (i=0; i<Max. Array; i++) { cout << "(1)Change Values (2)Square (3)Cube (4)Swap: "; cin >> choice; switch (choice) { case 1: p. Func. Array[i] = Get. Vals; break; case 2: p. Func. Array[i] = Square; break; case 3: p. Func. Array[i] = Cube; break; case 4: p. Func. Array[i] = Swap; break; default: p. Func. Array[i] = 0; } } for (i=0; i<Max. Array; i++) { p. Func. Array[i](val. One, val. Two); Print. Vals(val. One, val. Two); } return 0; } Lanjut
Output: (1)Change Values (2)Square (3)Cube (4)Swap: 1 (1)Change Values (2)Square (3)Cube (4)Swap: 2 (1)Change Values (2)Square (3)Cube (4)Swap: 3 (1)Change Values (2)Square (3)Cube (4)Swap: 4 (1)Change Values (2)Square (3)Cube (4)Swap: 2 New Value for Val. One: 2 New Value for Val. Two: 3 x: 2 y: 3 x: 4 y: 9 x: 64 y: 729 x: 729 y: 64 x: 7153 y: 4096
Using typedef with Pointers to Functions (C++) l The construct void (*)(int&, int&) is cumbersome, at best. You can use typedef to simplify this, by declaring a type VPF as a pointer to a function returning void and taking two integer references
Contoh : // Using typedef to make pointers to functions more _readable #include <iostream. h> void Square (int&, int&); void Cube (int&, int&); void Swap (int&, int &); void Get. Vals(int&, int&); typedef void (*VPF) (int&, int&) ; void Print. Vals(VPF, int&); enum BOOL { FALSE, TRUE }; int main() { int val. One=1, val. Two=2; int choice; BOOL f. Quit = FALSE; VPF p. Func; while (f. Quit == FALSE) { cout << "(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: "; cin >> choice; switch (choice) { case 1: p. Func = Get. Vals; break; case 2: p. Func = Square; break; case 3: p. Func = Cube; break; case 4: p. Func = Swap; break; default: f. Quit = TRUE; break; } if (f. Quit == TRUE) break; Print. Vals ( p. Func, val. One, val. Two); } return 0; } void Print. Vals( VPF p. Func, int& x, int& y) { cout << "x: " << x << " y: " << y << endl; p. Func(x, y); cout << "x: " << x << " y: " << y << endl; }
Output: (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 1 x: 1 y: 2 New value for Val. One: 2 New value for Val. Two: 3 x: 2 y: 3 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 3 x: 2 y: 3 x: 8 y: 27 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 2 x: 8 y: 27 x: 64 y: 729 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 4 x: 64 y: 729 x: 729 y: 64 (0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 0
Pointers to Member Functions l To create a pointer to member function, use the same syntax as with a pointer to function, but include the class name and the scoping operator (: : ). Thus, if p. Func points to a member function of the class Shape, which takes two integers and returns void, the declaration for p. Func is the following: void (Shape: : *p. Func) (int, int);
Contoh //Pointers to member functions using virtual methods #include <iostream. h> enum BOOL {FALSE, TRUE}; class Mammal { public: Mammal(): its. Age(1) { } ~Mammal() { } virtual void Speak() const = 0; virtual void Move() const = 0; protected: int its. Age; }; class Dog : public Mammal { public: void Speak()const { cout << "Woof!n"; } void Move() const { cout << "Walking to heel. . . n"; } }; class Cat : public Mammal { public: void Speak()const { cout << "Meow!n"; } void Move() const { cout << "slinking. . . n"; } }; class Horse : public Mammal { public: void Speak()const { cout << "Winnie!n"; } void Move() const { cout << "Galloping. . . n"; } };
int main() { void (Mammal: : *p. Func)() const =0; Mammal* ptr =0; int Animal; int Method; BOOL f. Quit = FALSE; while (f. Quit == FALSE) { cout << "(0)Quit (1)dog (2)cat (3)horse: "; cin >> Animal; switch (Animal) { case 1: ptr = new Dog; break; case 2: ptr = new Cat; break; case 3: ptr = new Horse; break; default: f. Quit = TRUE; break; } if (f. Quit) break; cout << "(1)Speak (2)Move: "; cin >> Method; switch (Method) { case 1: p. Func = Mammal: : Speak; break; default: p. Func = Mammal: : Move; break; } (ptr->*p. Func)(); delete ptr; } return 0; }
- Slides: 11