pointer (lanjutan)

11
Pointer (Lanjutan) Pertemuan 11 Bahasa C & C++

Upload: yamin

Post on 13-Jan-2016

53 views

Category:

Documents


5 download

DESCRIPTION

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”); - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Pointer (Lanjutan)

Pointer (Lanjutan)

Pertemuan 11

Bahasa C & C++

Page 2: Pointer (Lanjutan)

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 = %i\n”, *ptgl);printf(“Bulan = %i\n”, *(ptgl + 1));printf(“Tahun = %i\n”, *(ptgl + 2));printf(“\nDiakses dengan array biasa\n”);printf(“Tanggal = %i\n”, tgl_lahir[0]);printf(“Bulan = %i\n”, tgl_lahir[1]);printf(“Tahun = %i\n”, tgl_lahir[2]);getch();}

Page 3: Pointer (Lanjutan)

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] = %i\n”, k, x[k]);getch();}

Page 4: Pointer (Lanjutan)

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 GetVals(int&, int&); void PrintVals(int, int); enum BOOL { FALSE, TRUE }; int main(){int valOne=1, valTwo=2; int choice, i; const MaxArray = 5;void (*pFuncArray[MaxArray])(int&, int&);for (i=0;i<MaxArray;i++){ cout << "(1)Change Values (2)Square (3)Cube (4)Swap: "; cin >> choice; switch (choice) { case 1:pFuncArray[i] = GetVals; break; case 2:pFuncArray[i] = Square; break; case 3:pFuncArray[i] = Cube; break; case 4:pFuncArray[i] = Swap; break; default:pFuncArray[i] = 0; } } for (i=0;i<MaxArray; i++) { pFuncArray[i](valOne,valTwo); PrintVals(valOne,valTwo); } return 0; }

Lanjut

Page 5: Pointer (Lanjutan)

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 ValOne: 2

New Value for ValTwo: 3

x: 2 y: 3

x: 4 y: 9

x: 64 y: 729

x: 729 y: 64

x: 7153 y:4096

Page 6: Pointer (Lanjutan)

Using typedef with Pointers to Functions (C++)

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

Page 7: Pointer (Lanjutan)

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 GetVals(int&, int&);typedef void (*VPF) (int&, int&) ;void PrintVals(VPF,int&, int&);enum BOOL { FALSE, TRUE };int main(){ int valOne=1, valTwo=2; int choice; BOOL fQuit = FALSE; VPF pFunc; while (fQuit == FALSE) { cout << "(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: "; cin >> choice; switch (choice) { case 1:pFunc = GetVals; break; case 2:pFunc = Square; break; case 3:pFunc = Cube; break; case 4:pFunc = Swap; break; default:fQuit = TRUE; break; } if (fQuit == TRUE) break; PrintVals ( pFunc, valOne, valTwo); } return 0; } void PrintVals( VPF pFunc,int& x, int& y) { cout << "x: " << x << " y: " << y << endl; pFunc(x,y); cout << "x: " << x << " y: " << y << endl; }

Page 8: Pointer (Lanjutan)

Output:

(0)Quit (1)Change Values (2)Square (3)Cube (4)Swap: 1

x: 1 y: 2

New value for ValOne: 2

New value for ValTwo: 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

Page 9: Pointer (Lanjutan)

Pointers to Member Functions

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 pFunc points to a member function of the class Shape, which takes two integers and returns void, the declaration for pFunc is the following:

void (Shape::*pFunc) (int, int);

Page 10: Pointer (Lanjutan)

Contoh

//Pointers to member functions using virtual methods#include <iostream.h>enum BOOL {FALSE, TRUE};class Mammal{ public: Mammal():itsAge(1) { } ~Mammal() { } virtual void Speak() const = 0; virtual void Move() const = 0; protected: int itsAge; }; 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"; }};

Page 11: Pointer (Lanjutan)

int main() { void (Mammal::*pFunc)() const =0; Mammal* ptr =0; int Animal; int Method; BOOL fQuit = FALSE; while (fQuit == 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: fQuit = TRUE; break; } if (fQuit) break; cout << "(1)Speak (2)Move: "; cin >> Method; switch (Method) { case 1: pFunc = Mammal::Speak; break; default: pFunc = Mammal::Move; break; } (ptr->*pFunc)(); delete ptr; } return 0; }