F3031 Object Oriented Programming
F3031 Object Oriented Programming
1.0
2.0
3.0
4.0
2.0 CLASSES AND OBJECTS
2.6 Write program using Templates
OBJECTIVES TOPIC 2.6
1
2
3
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 membuatKek coklat Kek buah Acuan kek
…
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.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 }
OR
Cont..
Below is an example of a program to calculate area of a rectangle in
integer and double values.#include <iostream.h> template <class segi4> void luas(segi4 panjang, segi4 lebar) { segi4 segi;
Generic
segi= panjang * lebar; cout<<segi<<'\n';
Function
} 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); }
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'; } luas (i,j) luas (y,z )
Function With Two Generic Function
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> 2 types of generic data 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() { Output perbandingan(2,0.11); perbandingan(0.99,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 integer 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 …
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 Generic Types Penyaratan jenis generik dengan jelas
The program example below is used to calculate the area of a rectangle … by using specifc generic type conditions for integer type data.
#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;
Penyaratan jenis
segi= panjang * lebar;
generik integer dengan jelas/
cout<< "Luas segiempat dalam nilai integer: "<<segi<<'\n'; }
Explicit overloading generic type for integer Cont..
… 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
… template <class segi4> void luas(segi4 panjang, segi4 lebar){ : } luas(y,z) luas(i,j) Nilai integer i dan j dihantar ke fungsi Nilai integer y dan z generik yang mempunyai pra-takrifan dihantar ke fungsi generik jenis data integer yang umum void luas(int i, int j) void luas(segi4 y, segi4 z) {
{ : : } }
Generic Function Limitation
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;
PENERANGA
}
N Fungsi di atas tidak boleh ditempatkan semula oleh
void outdata(double d)
fungsi generik kerana ia tidak membuat perkara yang
{ cout << d*3.1416; sama. }
GENERIC CLASS
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.
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{ : : }
Nama_Kelas <jenisData> Nama_Objek;
DataType is data type that will be referred by the class during operation. …
This example shows how calculation for triangle and rectangle is done … by using generic class.
#include <iostream.h> void main() template<class type1> { int ukur1,ukur2,pilihan; class Bentuk { char terus; type1 u1, u2, pilihan, luas; Bentuk<double> segi3; Bentuk<double> segi4; public: Luas(type1 ,type1 ,type1 ); cout<<"Pilih 1 utk mengira luas }; segiempat\n"; cout<<"Pilih 2 utk mengira luas template<class type1> segitiga\n"; Bentuk<type1>::Luas(type1 pilihan, type1 u1,type1 u2){ if (pilihan ==1) { luas=u1 * u2; cout<<"luas segiempat: "<<luas<<’\n’; }
Cont…
if(pilihan ==2) { luas = ((u1*u2) /2); cout<<"luas segitiga: "<<luas<<’\n’; } }
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; } } cout<<" Mahu membuat pengiraan lain? (Y/N) \n"; cin>>terus; } while (terus=='Y'||terus=='y'); }
Output …
template<class type1> class Bentuk Pilihan 1 Segi4.luas(1,ukur1,ukur2)
Pilihan 2
Segi3.luas(2,ukur1,ukur2) template<class type1> 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) { : : if (pilihan == 2) { luas=ukur1 * ukur2; cout<<"luas segitiga: "<<luas<<’\n’; } } …
PENERANGAN
Dalam contoh aturcara ini, kelas generik bentuk digunakan untuk membentuk dua 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 menghantar argumen-argumen 1, ukur1 dan ukur2 ke dalam fungsi luas() yang terdapat dalam kelas generik bentuk dan memberi nilai 1 pada pembolehubah
pilihan, nilai ukur1 pada pembolehubah u1 dan nilai ukur2 pada pembolehubah u2.
Kemudian nilai-nilai argumen yang telah diberi nilai akan digunakan untuk mengira
nilai luas segiempat. Hal yang sama akan dilakukan jika pengguna membuat pilihan
dua di mana segi3.luas(2, ukur1.ukur2) akan menghantar argumen-argumennya ke
dalam fungi luas() yang terdapat pada kelas generik bentuk untuk memberi nilaiargumen-argumen tersebut dengan pembolehubah yang ada pada fungsi luas() dan
menggunakan nilai-nilai yang diberi nilai untuk mengira luas segitiga pula.
Types Of Generic
1. Generic class can consist more than one generic data type.Data
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> void main() #include<stdlib.h> { buku<int,double> a; #include<string.h> int pilih,unit; template <class Type1,class Type2> char tambah; class buku double kira1,kira,harga; { Type1 pilih,unit; kira=0,kira1=0; Type2 harga,kira1,jumlah; cout<<"KAUNTER TIKET TEATER SI CINDAI\ public: n"; Type2 Harga(Type1 a,Type2 b){ cout<<"Masukkan pilihan kategori :\n"; unit=a; cout<<"1.Dewasa\n"; harga=b; cout<<"2.Kanak-kanak\n"; jumlah=harga*unit; return jumlah; } };
Cont…
… do { cout<<"Pilihan anda:"; cin>> pilih; switch(pilih){ case 1: harga=10.00; break; case 2: harga=5.00; Output 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'); cout<<"Jumlah Bayaran:RM"<<kira1; }
#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); cout<<"Panjang (dalam nilai double): "<<y<<'\ n'; cout<<"Lebar (dalam nilai double): "<<z<<'\n'; cout<<"Luas segiempat dalam nilai double: "; a.luas (y,z);