Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehSuryadi Budiaman Telah diubah "9 tahun yang lalu
1
1 Pertemuan 8 Array & Pointer of Class Matakuliah: T0044/Pemrograman Berorientasi Obyek Tahun: 2005 Versi: 1.0
2
2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan prinsip kerja array of class Mendemonstrasikan prinsip kerja pointer of class
3
3 Outline Materi Array Class Pointer of Class Operator new & delete Operator '&' dan '->'
4
4 Array Class Arrays in C++ –No range checking –Cannot be compared meaningfully with == –No array assignment (array names const pointers) –Cannot input/output entire arrays at once One element at a time Example:Implement an Array class with –Range checking –Array assignment –Arrays that know their size –Outputting/inputting entire arrays with > –Array comparisons with == and !=
5
5 Array Class Copy constructor –Used whenever copy of object needed Passing by value (return value or parameter) Initializing an object with a copy of another –Array newArray( oldArray ); –newArray copy of oldArray –Prototype for class Array Array( const Array & ); Must take reference Otherwise, pass by value and invoke infinite loop –Tries to make copy by calling copy constructor …
6
6 Kasus Program Array Class Pelajarilah Program Array Class pada buku Deitel chapter 8 Page 556 Buatlah Analisa dari Program tersebut menangani masalah array pada C++ dengan Array Class: –Range checking –Array assignment –Arrays that know their size –Outputting / inputting entire arrays with > –Array comparisons with == and !=
7
7 Pointer of Class Pointer sebagai Data Members Pointers tdk dpt di-initialized dalam class declaration Normally, Initialized pada derived class constructor Penggunaan pointers to class data members Umumnya melalui function yg menggunakan pointers to class data members sebagai debuging function yg meng-access 2 bytes pertama dari data member tiap object & mengambil nilainya
8
8 Pointer of Class #include class First { public: int value; First() {value = 0;} int GetValue() {return value;} }; class Second { int id; public: First *obj1; Second() {id = 0;} int GetName(){ return id;} }; int main() { Second Obj2; int num; num = Obj2.GetName(); return 0; } Saat Obj2 dicreate, constructor Second tidak memanggil constructor First. Declaration of Pointer to class First
9
9 sebagai debuging function #include class Pointer { public:int data1, data2; int value; int id; void foo1() {} void foo2() {} }; void SetValue(Pointer& object) {int Pointer::*ip = &Pointer::value; object.*ip = 3; } void SetId(int *member) {*member = 7; } void main() { int Pointer::*ip1 = &Pointer::data1; int Pointer::*ip2 = &Pointer::data2; Pointer obj1; //access 2 public data members via reference obj1.*ip1 = 5; obj1.*ip2 = 10; //access data member via SetValue() method SetValue(obj1); //create 2 pointers to object member int *pointer1 = &obj1.value; int *pointer2 = &obj1.id; //pass the pointers to SetValue() SetId(pointer1); SetId(pointer2); } Debug result: Obj1 = {5,10,7,7} Data1 = 5 Data2 = 10 Value = 7 (after Fuction SetId run, it was 3) Id = 7
10
10 Latihan //Array of Objek #include class Bilangan { private: int tot, Num; public: Bilangan () {Num = ++tot;} void show ( int i ) { cout << “Bilangan # “ << i << “ of “ << tot << “objek\n”; } int Ret_Num () { return Num; } }; void main() {Bilangan A[10]; for ( int i=0; i<10; i++ ) Bilangan :: show ( A[i].Ret_Num () ) }
11
11 Latihan // Pointer to Objek #include class Square { private: float Panjang, Lebar; public: void Masuk_Data ( float pj, float lb ) { Panjang = pj; Lebar = lb; } void Tampil_Data { cout << “Panjang Square = “ << Panjang << endl; cout << “Lebar Square = “ << Lebar << endl; } }; void main() { Square B[3]; Square *s; B[0].Masuk_Data (10, 5, 8 ); s = &B[1]; s -> Masuk_Data (8, 4, 6); s++; s -> Masuk_Data ( 20,10, 8 ); s--; s -> Tampil_Data ( ); s--; s -> Tampil_Data ( ); }
12
12 Tugas 8 Tugas 8A: Mahasiswa membuat ringkasan tentang Array of class dan Pointer of class Tugas 8B: Mahasiswa menganalisis program kasus Array Class yang diberikan oleh dosen Tugas 8C: Mahasiswa mencari output Latihan program dengan menggunakan Object Array dan Pointer to Object
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.