Download presentation
Presentation is loading. Please wait.
Published byJanice Neblett Modified over 9 years ago
1
pointer
2
Kegunaan pointer yang utama adalah untuk menyimpan memory address dari sebuah variable (data type atau object dari class). Selain menyimpan address dari variable, pointer juga dapat digunakan untuk menyimpan address dari sebuah fungsi (function pointer)
3
Deklarasi : ---------------------------------------------------- (* )( ); ---------------------------------------------------- ----------------------------------------------------
4
Pointer and Virtual Function Pointer to Objects this Pointer Pointer to Derived Classes Virtual Functions
5
Pointer to Object #include<iostream.h> class item {int code; public: void show(void) {code = 88; cout<<"code:"<<code<<"\n";}}; void main() {item *p, g;//remember that * is notation for pointer p = &g;//copy the address of object g to p p->show();//access the function using pointer g.show();// access the function directly using the object }
6
this pointer #include "iostream.h" class data { int A;//global inside the class public: data(int x){A = x;}//constructor void ngawur(void) {cout<<"Kok bisa ya?!"<<endl;} void display(void) { int A;//local variabel A = 10; cout A A<<endl; cout ngawur(); }}; void main() {data d(100), e(200); d.display(); cout<<endl; e.display(); }
7
Pointer to Derived Classes Pointer to objects of a base class are type- compatible with pointers to objects of derived class. Single pointer variable can be made to point to objects belonging to different classes. For example: if B is a base class and D is a derived class from B, then a pointer declared as a pointer to B can also be pointer to D.
8
#inlcude “iostream.h” class BC {public : int b; void show() { cout << “b = “ << b << “\n”; }; }; class DC : public BC {public : int d; void show() {cout << “b = “ << b << “\n”; cout << “d = “ << d << “\n”; };}; void main() {BC *base_ptr, base; base_ptr = &base; base_ptr b = 100; base_ptr show(); DC derived; base_ptr = &derived; base_ptr->b = 300; ((DC *)base_ptr)->d = 500; ((DC *)base_ptr)->show(); }
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.