• Tidak ada hasil yang ditemukan

F3031 Object Oriented Programming

N/A
N/A
Protected

Academic year: 2018

Membagikan "F3031 Object Oriented Programming"

Copied!
24
0
0

Teks penuh

(1)

F3031 Object Oriented Programming

1.0

2.0

(2)

2.0 CLASSES AND OBJECTS

2.6

Write program using

(3)

OBJECTIVES TOPIC 2.6

1

2

(4)

1. Template can be assumed as one that can be used to develop a function or class.

2. C++ use template to achieve polymorphism that is during compilation.

3. One example of template in real world is a cake mould. A cake mould can be used for making a chocolate or fruit cake.

TEMPLATE

S

Boleh digunakan untuk membuat

Kek coklat

(5)

4. By using template, it allows programmer create generic function and generic class function.

5. Generic function is a draft for a function that can be used to generate a few same functions in diferent version.

6. The advantages of a template are that coding can be shortened and made easier.

If using generic class, new classes can built without coding the defnition, only by using the current generic class.

(6)

1. Generic function defnes a general set of operations that can be used on various types of data.

2. Generic function is created using the keyword template, followed by a formal template parameter list, which is enclosed within

angle bracket (< >). Each parameter represents data types must be began with class keyword. After that, function name for

generic function will be defned.

3. Below is a general form for generic function defnition.

GENERIC

FUNCTION

template <class JenisData> nama_Fungsi( ) {

//badan fungsi }

template <class JenisData> nama_Fungsi( ) {

//badan fungsi }

template <class JenisData> nama_Fungsi( )

{

//badan fungsi

template <class JenisData> nama_Fungsi( )

{

//badan fungsi

(7)

Below is an example of a program to calculate area of a rectangle in integer and double values.

Cont...

#include <iostream.h>

template <class segi4>

void luas(segi4 panjang, segi4 lebar) { segi4 segi;

segi= panjang * lebar; cout<<segi<<'\n';

}

void main(){ int i=10,j=20;

double y=10.1,z=4.2;

cout<<"Panjang (dalam nilai integer): "<<i<<'\n'; cout<<"Lebar (dalam nilai integer): "<<j<<'\n';

cout<<"Luas segiempat dalam nilai integer: "luas(i,j); cout<<"Panjang (dalam nilai double): "<<y<<'\n';

cout<<"Lebar (dalam nilai double): "<<z<<'\n'; cout<<"Luas segiempat dalam nilai double: ";

luas (y,z);

(8)

Cont...

template< class segi4>

void luas(segi4 panjang,segi4 lebar){

: : }

Apabila nilai integer i dan j dihantar ke fungsi generik

Apabila nilai double y dan z dihantar ke fungsi

generik

template <class segi4> void luas(segi4 i, segi4 j) { segi4 segi;

segi= i * j;

cout<<segi<<'\n'; }

template <class segi4> void luas(segi4 y, segi4 z) { segi4 segi;

segi= y * z;

cout<<segi<<'\n'; }

(9)

1. You can defne more than one generic data type in a template statement by using coma (,) to separate generic data types.

2. The program given below used to compare 2 values.

#include <iostream.h>

template<class banding1, class banding2> void perbandingan( banding1 x, banding2 y){ if (x>y)

cout<<" Nilai "<<x<<" lebih besar daripada nilai "<<y<<'\n'; else

cout<<" Nilai "<<y<<" lebih besar daripada nilai "<<x<<'\n'; }

void main() {

perbandingan(2,0.11); perbandingan(0.99,10); }

2 types of generic data

Output

(10)

Apabila nilai 2 dan 0.11 dihantar ke fungsi generik

template<class banding1,class banding2> void perbandingan(banding1 x,banding2 y)

{

:

}

perbandingan(2,0.11); perbandingan(0.99,10)

;

Apabila nilai 0.99 dan 10 dihantar ke fungsi generik

void perbandingan( banding1 2, banding2 0.11) {

Memegang data double

void perbandingan( banding1 0.99, banding2,10) {

: :

} Memegang data integer

Memegang data double Apabila nilai 2 dan 0.11 dihantar

ke fungsi generik

Cont

(11)

 

1. When you call generic function, argument of function will determine types of data that will be used in function.

2. However, C++ allows you to do pre-defnition data types by

determining types of data that you want program to manipulate.

Explicitly Overloading

(12)

#include <iostream.h>

template <class segi4>

void luas(segi4 panjang, segi4 lebar) { segi4 segi;

segi= panjang * lebar; cout<<segi<<'\n';

}

void luas(int panjang, int lebar) { int segi;

segi= panjang * lebar;

cout<< "Luas segiempat dalam nilai integer: "<<segi<<'\n'; }

Penyaratan jenis generik integer dengan jelas/

Explicit overloading generic type for integer

The program example below is used to calculate the area of a rectangle by using specifc generic type conditions for integer type data.

Cont..

(13)

void main(){ int i=10,j=20;

double y=10.1,z=4.2;

cout<<"Panjang (dalam nilai integer): "<<i<<'\n'; cout<<"Lebar (dalam nilai integer): "<<j<<'\n'; luas (i,j);

cout<<"Panjang (dalam nilai double): "<<y<<'\n'; cout<<"Lebar (dalam nilai double): "<<z<<'\n'; cout<<"Luas segiempat dalam nilai double: "; luas (y,z);

}

Output

(14)

template <class segi4> void luas(segi4

panjang, segi4 lebar){ :

}

luas(i,j) luas(y,z)

Nilai integer i dan j dihantar ke fungsi generik yang mempunyai pra-takrifan jenis data integer

Nilai integer y dan z

dihantar ke fungsi generik yang umum

void luas(int i, int j) {

: }

void luas(segi4 y, segi4 z) {

: }

(15)

1. Generic function is almost like an overloaded function except it has more limitation.

2. Generic function must do the same action for all version - only data types can be diferent.

3. This program shows error when outdata() function do not do the same thing.

void outdata (int i) {

cout << i; }

void outdata(double d) {

cout << d*3.1416;

Fungsi di atas tidak boleh ditempatkan semula oleh fungsi generik kerana ia tidak membuat perkara yang sama.

PENERANGA

N

(16)

1. When you create generic class, you built class that defnes all algorithm used by class, but the actual data types that is being

manipulated will be specifed as parameter when object for the class is created.

2. Generic class is useful when class uses logical that can be made as general conclusion.

3. Example: The same algorithm to maintain integer rows will work for character rows.

4. When you create generic class, it can implement operation that you defned.

5. Compiler will generate correct object types automatically; based on types you’ve specifed when object is created.

(17)

6. Generic class defnition begins with the keyword template followed by parameter list for that template that written in < >. Then

followed by identifer that represents name of that generic class, and followed by class members that written in {}.

7. Below is a general form for generic class.

template <class JenisData> class Nama_Kelas{

: : }

template <class JenisData> class Nama_Kelas{

: : }

8. When you have built generic class, you built specifc object for that class by using a general form as below:

Nama_Kelas <jenisData> Nama_Objek;

Nama_Kelas <jenisData> Nama_Objek;

DataType is data type that will be referred by the class during operation.

(18)

This example shows how calculation for triangle and rectangle is done by using generic class.

#include <iostream.h> template<class type1> class Bentuk {

type1 u1, u2, pilihan, luas;

public:

Luas(type1 ,type1 ,type1 ); };

cout<<"luas segiempat: "<<luas<<’\n’; }

if(pilihan ==2) {

luas = ((u1*u2) /2);

cout<<"luas segitiga: "<<luas<<’\n’;

void main()

{ int ukur1,ukur2,pilihan; char terus;

Bentuk<double> segi3; Bentuk<double> segi4;

cout<<"Pilih 1 utk mengira luas segiempat\n";

cout<<"Pilih 2 utk mengira luas segitiga\n";

Cont…

(19)

do{

cout<<"Pilihan anda: "; cin>>pilihan;

switch(pilihan){ case 1: {

pilihan = 1;

cout<<"nilai lebar: "; cin>>ukur1;

cout<<"nilai tinggi: "; cin>>ukur2;

segi4.Luas(1,ukur1,ukur2); break;

}

case 2:{ pilihan= 2;

cout<<"nilai tapak: "; cin>>ukur1;

cout<<"nilai tinggi: "; cin>>ukur2;

cout<<"Luas segitiga "; segi3.Luas(2,ukur1,ukur2); break;

} }

(20)

template<class type1> class Bentuk

Bentuk<type1>::Luas(type1 1,type1 ukur1, type1 ukur2)

{ if (pilihan ==1) {

luas=ukur1 * ukur2;

cout<<"luas segiempat: "<<luas<<’\n’; }

: :

template<class type1>

Bentuk<type1>::Luas(type1 2,type1 ukur1, type1 ukur2)

cout<<"luas segitiga: "<<luas<<’\n’; }

(21)

objek iaitu objek segi3 dan objek segi4 melalui pernyataan : Bentuk<double>segi3;

Bentuk<double>segi4;

Apabila pengguna memilih pilihan satu, segi4.luas(1, ukur1, ukur2) akan

(22)

1. Generic class can consist more than one generic data type.

2. Declaration of all data types needed by class made by using coma sign (,) in template determination.

3. Example below shown program to determine total price for theatre tickets

.

#include<iostream.h> #include<stdlib.h> #include<string.h>

template <class Type1,class Type2> class buku

{ Type1 pilih,unit;

Type2 harga,kira1,jumlah; public:

Type2 Harga(Type1 a,Type2 b){ unit=a;

harga=b;

jumlah=harga*unit; return jumlah; }

void main()

{ buku<int,double> a; int pilih,unit; char tambah;

double kira1,kira,harga; kira=0,kira1=0;

cout<<"KAUNTER TIKET TEATER SI CINDAI\ n";

cout<<"Masukkan pilihan kategori :\n"; cout<<"1.Dewasa\n";

(23)

do {

cout<<"Pilihan anda:"; cin>> pilih;

switch(pilih){ case 1:

harga=10.00; break;

case 2:

harga=5.00; break;

default:

cout<<"Input salah\n"; break;

}

cout<<"Bilangan tiket:"; cin>>unit;

kira1=kira1 + a.Harga(unit,harga);

cout<<"Mahu teruskan operasi? (Y/N)\n"; cin>>tambah;

}while(tambah=='Y'||tambah=='y');

Output

(24)

#include <iostream.h>

template<class segi4>

class Kira{

segi4 panjang, lebar; public:

luas (segi4, segi4); };

template <class segi4>

Kira <segi4>::luas(segi4 panjang, segi4 lebar) { segi4 segi;

segi= panjang * lebar; cout<<segi<<'\n';

}

void main(){ int i=10,j=20; double y=10.1,z=4.2;

Kira<double> a;

cout<<"Panjang (dalam nilai integer): "<<i<<'\ n';

cout<<"Lebar (dalam nilai integer): "<<j<<'\n'; cout<<"Luas segiempat dalam nilai integer: "; a.luas (i,j);

Referensi

Dokumen terkait

Gedung H, Kampus Sekaran-Gunungpati, Semarang 50229 Telepon: (024)

Sedangkan hasil data dari siswa yang tidak menggunakan asosiasi. media gambar dalam pembelajaran kanji dasar diperoleh

Tugas dari rumah detensi imigrasi ( RUDENIM ) Kota Pekanbaru ialah melaksanakan sebagian tugas pokok dan fungsi Kementerian Hukum dan Hak Asasi Manusia di

Berdasarkan hasil penelitian Christine (2010) penambahan larutan NaCl 2% dan larutan garam dapur Dolphin ® menunjukkan setting time yang lebih singkat dibandingkan dengan tanpa

“I told you I was going to take piano lessons at the Shreek School, remember.. Then you got this strange look on your face, and you

Permasalahan yang akan dibahas dalam skripsi ini adalah apakah dokter gigi umum dalam melakukan pemasangan behel gigi yang seharusnya merupakan kewenangan dari dokter gigi

Kegiatan pembelajaran remedial dengan pemanfaatan tutor sebayamelalui belajar kelompok untuk menjawab pertanyaan yang diberikan guru terkait materi kondisi geologi

Dari hasil analisis data yang telah dikumpulkan melalui kuesiner dan hasil uji Hipotesis, maka dapat disimpulkan sebagai berikut. Terdapat perbedaan signifikan persepsi