SlideShare a Scribd company logo
1 of 18
Download to read offline
BAHASA PEMROGRAMAN
(IKP213)
Pertemuan 6: Template C++, 6 Desember 2011
Function Overload
2

        void printData(int value) {
            cout<<"The value is "<<value<<endl;
        }
       Bagaimana untuk mencetak tipe string? int?
       Overload: Buat beberapa fungsi dengan signature
        berbeda
        void printData(string value) {
            cout<<"The value is "<<value<<endl;
        }



                                   STL – IKP213
Template
3

        template<typename T>
        void printData(T value) {
            cout<<"The value is "<<value<<endl;
        }

       Mendefinisikan T sebagai sebuah tipe template
       T akan diinstantiasi sesuai kebutuhan
        double d = 10.01;
        string s = "Template";
        printData(d);
        printData(s);


                                    STL – IKP213
Template Parameter
4


       Sintaks penulisan
        template<typename name>
        template<class name>
       Pemanggilan fungsi / class akan diinstantiasi sesuai tipe
        yang digunakan
        template<typename T>
        void func(T value) {
          const T& ref=value;
          T* p=new T;
          T temp(23);
        }

                                     STL – IKP213
Template Parameter
5


       Jika ada definisi func<int>, akan digenerate
        sebuah instantiasi fungsi
        void func(int value) {
          const int& ref=value;
          int* p=new int;
          int temp(23);
        }
       Semua kemunculan T digantikan int


                                  STL – IKP213
Template Parameter
6


       Jika ada definisi func<string>, akan muncul
        kesalahan kompilasi
        void func(string value) {
          const string& ref=value;
          string* p=new string;
          string temp(23);
        }
       Karena string   temp(23)   tidak valid


                                      STL – IKP213
Standard Template Library (STL)
7


       Koleksi containers dan algorithms
       Containers
         Class untuk menampung sekumpulan objek
         vector, list, queue, deque, priority_queue, stack, map,
          multimap, set, multiset




                                      STL – IKP213
STL
8


       Deklarasi
        vector<int> dataInt(5);
        vector<string> dataStr(5);
       Assignment
        dataInt[0] = 10;
        dataStr[3] = "Budi";
       Menambahkan data
        vector<string> vectS;
        vectS.push_back("Kata");
        vectS.push_back("siapa");
        vectS.push_back("STL susah");

                                 STL – IKP213
Loop by Index
9

     int ii;
     for (ii=0; ii < SS.size(); ii++) {
         cout << SS[ii] << endl;
     }




                              STL – IKP213
Loop by Iterator
10

      vector<string>::const_iterator cii;


      for (cii=SS.begin(); cii!=SS.end(); cii++) {
          cout << *cii << endl;
      }




                                  STL – IKP213
Loop by Reverse Iterator
11

      vector<string>::reverse_iterator rii;


      for (rii=SS.rbegin(); rii!=SS.rend(); ++rii) {
          cout << *rii << endl;
      }




                                  STL – IKP213
Matriks
12

      // Vector length of 3 initialized to 0
      vector<int> vI1Matrix(3,0);

      // Vector length of 4 initialized to hold another
      // vector vI1Matrix which has been initialized
      // to 0
      vector< vector<int> > vI2Matrix(4, vI1Matrix);

      // Vector of length 5 containing two dimensional
      // vectors
      vector< vector< vector<int> > > vI3Matrix(5,
        vI2Matrix);


                               STL – IKP213
Dalam satu baris
13

      vector< vector< vector<int> > > vI3Matrix(2,
        vector< vector<int> > (3, vector<int>(4,0)) );




                               STL – IKP213
Iterator Matriks
14

      // Declare two dimensional array
      vector< vector<int> > vI2Matrix;
      vector<int> A, B;
      vector< vector<int> >::iterator iter_ii;
      vector<int>::iterator iter_jj;


      A.push_back(10); A.push_back(20);
      A.push_back(30);
      B.push_back(100); B.push_back(200);
      B.push_back(300);


      vI2Matrix.push_back(A);
      vI2Matrix.push_back(B);   STL – IKP213
Iterator Matriks
15

      cout << "Using Iterator:" << endl;


      for(iter_ii=vI2Matrix.begin();
          iter_ii!=vI2Matrix.end();
          iter_ii++) {
          for(iter_jj=(*iter_ii).begin();
              iter_jj!=(*iter_ii).end();
              iter_jj++) {
                cout << *iter_jj << endl;
          }
      }


                                   STL – IKP213
Operator-operator Lain
16


        empty()
        size()
        resize(n)
        front(), v[0]
        back()
        at(index), v[index]
        erase(), clear()


                               STL – IKP213
Pustaka
17


        STL Tutorial,
         http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html
        STL Tutorial, http://www.mochima.com/tutorials/STL.html
        STL Reference, http://www.cplusplus.com/reference/stl/
        http://tjerdastangkas.blogspot.com/search/label/ikp213




                                        STL – IKP213
AKHIR PERTEMUAN 6
Selasa, 6 Desember 2011

More Related Content

What's hot

Most Common JavaScript Mistakes
Most Common JavaScript MistakesMost Common JavaScript Mistakes
Most Common JavaScript MistakesYoann Gotthilf
 
Prof.js
Prof.jsProf.js
Prof.jsuupaa
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Loïc Knuchel
 
Java осень 2012 лекция 6
Java осень 2012 лекция 6Java осень 2012 лекция 6
Java осень 2012 лекция 6Technopark
 
Алексей Кутумов, C++ без исключений, часть 3
Алексей Кутумов,  C++ без исключений, часть 3Алексей Кутумов,  C++ без исключений, часть 3
Алексей Кутумов, C++ без исключений, часть 3Platonov Sergey
 
exercise of basic computer programming.docx
exercise of basic computer programming.docxexercise of basic computer programming.docx
exercise of basic computer programming.docxmiftah88
 
Функциональное реактивное программирование
Функциональное реактивное программированиеФункциональное реактивное программирование
Функциональное реактивное программированиеDmitriy Kiriyenko
 
matrix operation using operator overloading
matrix operation using operator overloadingmatrix operation using operator overloading
matrix operation using operator overloadingmaria azam
 
Linked List Implementation of Stack in C
Linked List Implementation of Stack in CLinked List Implementation of Stack in C
Linked List Implementation of Stack in CKasun Ranga Wijeweera
 
Strutture dati 08-reshape
Strutture dati 08-reshapeStrutture dati 08-reshape
Strutture dati 08-reshapeStudiabo
 

What's hot (14)

Docuemnto 6
Docuemnto 6Docuemnto 6
Docuemnto 6
 
Most Common JavaScript Mistakes
Most Common JavaScript MistakesMost Common JavaScript Mistakes
Most Common JavaScript Mistakes
 
Prof.js
Prof.jsProf.js
Prof.js
 
Netb si and
Netb si andNetb si and
Netb si and
 
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
Comprendre la programmation fonctionnelle, Blend Web Mix le 02/11/2016
 
Java осень 2012 лекция 6
Java осень 2012 лекция 6Java осень 2012 лекция 6
Java осень 2012 лекция 6
 
Los fantastico
Los fantasticoLos fantastico
Los fantastico
 
Алексей Кутумов, C++ без исключений, часть 3
Алексей Кутумов,  C++ без исключений, часть 3Алексей Кутумов,  C++ без исключений, часть 3
Алексей Кутумов, C++ без исключений, часть 3
 
exercise of basic computer programming.docx
exercise of basic computer programming.docxexercise of basic computer programming.docx
exercise of basic computer programming.docx
 
Функциональное реактивное программирование
Функциональное реактивное программированиеФункциональное реактивное программирование
Функциональное реактивное программирование
 
Convert bilangan
Convert bilanganConvert bilangan
Convert bilangan
 
matrix operation using operator overloading
matrix operation using operator overloadingmatrix operation using operator overloading
matrix operation using operator overloading
 
Linked List Implementation of Stack in C
Linked List Implementation of Stack in CLinked List Implementation of Stack in C
Linked List Implementation of Stack in C
 
Strutture dati 08-reshape
Strutture dati 08-reshapeStrutture dati 08-reshape
Strutture dati 08-reshape
 

Viewers also liked

120109 Arbeidsmarkt Presentatie Roc Ecabo(1)
120109 Arbeidsmarkt Presentatie  Roc Ecabo(1)120109 Arbeidsmarkt Presentatie  Roc Ecabo(1)
120109 Arbeidsmarkt Presentatie Roc Ecabo(1)Andre_Vondeling
 
Global Conferencing Trends
Global Conferencing TrendsGlobal Conferencing Trends
Global Conferencing TrendsInterCall
 
Rupert.Reading.Jan 2015
Rupert.Reading.Jan 2015 Rupert.Reading.Jan 2015
Rupert.Reading.Jan 2015 Faye Brownlie
 
Baile alumnado 2º ciclo 2013
Baile alumnado 2º ciclo 2013Baile alumnado 2º ciclo 2013
Baile alumnado 2º ciclo 2013XXX XXX
 
Social Realism
Social RealismSocial Realism
Social Realismp102
 
PHP & XML: SimpleXML, DOMDocument
PHP & XML: SimpleXML, DOMDocumentPHP & XML: SimpleXML, DOMDocument
PHP & XML: SimpleXML, DOMDocumentValentin Bora
 
Charla uso tecnologías 3 er ciclo
Charla uso tecnologías 3 er cicloCharla uso tecnologías 3 er ciclo
Charla uso tecnologías 3 er cicloXXX XXX
 
Office 365 + Windows Azure (del 2)
Office 365 + Windows Azure (del 2)Office 365 + Windows Azure (del 2)
Office 365 + Windows Azure (del 2)Wictor Wilén
 
ForchuTeck SAP Solution
ForchuTeck SAP SolutionForchuTeck SAP Solution
ForchuTeck SAP Solutionguest6e809a8
 
Rupert - AFL - Jan, 2014
Rupert - AFL - Jan, 2014Rupert - AFL - Jan, 2014
Rupert - AFL - Jan, 2014Faye Brownlie
 
Make a Wave - Branding Intro webinar - PatchworkPresent
Make a Wave - Branding Intro webinar - PatchworkPresentMake a Wave - Branding Intro webinar - PatchworkPresent
Make a Wave - Branding Intro webinar - PatchworkPresentOgunte CIC
 
Kamloops.#2.nov.2012
Kamloops.#2.nov.2012Kamloops.#2.nov.2012
Kamloops.#2.nov.2012Faye Brownlie
 
UX / CX in the context of creative & marketing industry
UX / CX in the context of creative & marketing industryUX / CX in the context of creative & marketing industry
UX / CX in the context of creative & marketing industryKaKi Law
 
Electric energy scientific development, main source and consumers
Electric energy scientific development, main source and consumersElectric energy scientific development, main source and consumers
Electric energy scientific development, main source and consumersP K Agarwal
 

Viewers also liked (20)

120109 Arbeidsmarkt Presentatie Roc Ecabo(1)
120109 Arbeidsmarkt Presentatie  Roc Ecabo(1)120109 Arbeidsmarkt Presentatie  Roc Ecabo(1)
120109 Arbeidsmarkt Presentatie Roc Ecabo(1)
 
Per ancash
Per ancashPer ancash
Per ancash
 
PROYECTO WorkCentre
PROYECTO WorkCentrePROYECTO WorkCentre
PROYECTO WorkCentre
 
Global Conferencing Trends
Global Conferencing TrendsGlobal Conferencing Trends
Global Conferencing Trends
 
Rupert.Reading.Jan 2015
Rupert.Reading.Jan 2015 Rupert.Reading.Jan 2015
Rupert.Reading.Jan 2015
 
Baile alumnado 2º ciclo 2013
Baile alumnado 2º ciclo 2013Baile alumnado 2º ciclo 2013
Baile alumnado 2º ciclo 2013
 
Social Realism
Social RealismSocial Realism
Social Realism
 
PHP & XML: SimpleXML, DOMDocument
PHP & XML: SimpleXML, DOMDocumentPHP & XML: SimpleXML, DOMDocument
PHP & XML: SimpleXML, DOMDocument
 
Charla uso tecnologías 3 er ciclo
Charla uso tecnologías 3 er cicloCharla uso tecnologías 3 er ciclo
Charla uso tecnologías 3 er ciclo
 
Office 365 + Windows Azure (del 2)
Office 365 + Windows Azure (del 2)Office 365 + Windows Azure (del 2)
Office 365 + Windows Azure (del 2)
 
ForchuTeck SAP Solution
ForchuTeck SAP SolutionForchuTeck SAP Solution
ForchuTeck SAP Solution
 
Rupert - AFL - Jan, 2014
Rupert - AFL - Jan, 2014Rupert - AFL - Jan, 2014
Rupert - AFL - Jan, 2014
 
Make a Wave - Branding Intro webinar - PatchworkPresent
Make a Wave - Branding Intro webinar - PatchworkPresentMake a Wave - Branding Intro webinar - PatchworkPresent
Make a Wave - Branding Intro webinar - PatchworkPresent
 
Al- Andalus
Al- AndalusAl- Andalus
Al- Andalus
 
Tle4904 343973
Tle4904 343973Tle4904 343973
Tle4904 343973
 
Kamloops.#2.nov.2012
Kamloops.#2.nov.2012Kamloops.#2.nov.2012
Kamloops.#2.nov.2012
 
UX / CX in the context of creative & marketing industry
UX / CX in the context of creative & marketing industryUX / CX in the context of creative & marketing industry
UX / CX in the context of creative & marketing industry
 
Electric energy scientific development, main source and consumers
Electric energy scientific development, main source and consumersElectric energy scientific development, main source and consumers
Electric energy scientific development, main source and consumers
 
Naresh
NareshNaresh
Naresh
 
Sph 107 Ch 11
Sph 107 Ch 11Sph 107 Ch 11
Sph 107 Ch 11
 

More from Anung Ariwibowo (20)

isd314-06-association-mining
isd314-06-association-miningisd314-06-association-mining
isd314-06-association-mining
 
ikp213-unifikasi
ikp213-unifikasiikp213-unifikasi
ikp213-unifikasi
 
ikp213-06-horn-clause
ikp213-06-horn-clauseikp213-06-horn-clause
ikp213-06-horn-clause
 
ikp213-01-pendahuluan
ikp213-01-pendahuluanikp213-01-pendahuluan
ikp213-01-pendahuluan
 
ikd312-05-sqlite
ikd312-05-sqliteikd312-05-sqlite
ikd312-05-sqlite
 
ikd312-05-kalkulus-relasional
ikd312-05-kalkulus-relasionalikd312-05-kalkulus-relasional
ikd312-05-kalkulus-relasional
 
ikd312-04-aljabar-relasional
ikd312-04-aljabar-relasionalikd312-04-aljabar-relasional
ikd312-04-aljabar-relasional
 
ikd312-03-design
ikd312-03-designikd312-03-design
ikd312-03-design
 
ikd312-02-three-schema
ikd312-02-three-schemaikd312-02-three-schema
ikd312-02-three-schema
 
ikp213-02-pendahuluan
ikp213-02-pendahuluanikp213-02-pendahuluan
ikp213-02-pendahuluan
 
ikh311-08
ikh311-08ikh311-08
ikh311-08
 
ikh311-07
ikh311-07ikh311-07
ikh311-07
 
ikh311-06
ikh311-06ikh311-06
ikh311-06
 
ikh311-05
ikh311-05ikh311-05
ikh311-05
 
ikp321-svn
ikp321-svnikp321-svn
ikp321-svn
 
ikh311-04
ikh311-04ikh311-04
ikh311-04
 
ikp321-05
ikp321-05ikp321-05
ikp321-05
 
imsakiyah-jakarta-1433-09
imsakiyah-jakarta-1433-09imsakiyah-jakarta-1433-09
imsakiyah-jakarta-1433-09
 
ikh311-03
ikh311-03ikh311-03
ikh311-03
 
ikp321-04
ikp321-04ikp321-04
ikp321-04
 

ikp213-06-template-c++

  • 1. BAHASA PEMROGRAMAN (IKP213) Pertemuan 6: Template C++, 6 Desember 2011
  • 2. Function Overload 2 void printData(int value) { cout<<"The value is "<<value<<endl; }  Bagaimana untuk mencetak tipe string? int?  Overload: Buat beberapa fungsi dengan signature berbeda void printData(string value) { cout<<"The value is "<<value<<endl; } STL – IKP213
  • 3. Template 3 template<typename T> void printData(T value) { cout<<"The value is "<<value<<endl; }  Mendefinisikan T sebagai sebuah tipe template  T akan diinstantiasi sesuai kebutuhan double d = 10.01; string s = "Template"; printData(d); printData(s); STL – IKP213
  • 4. Template Parameter 4  Sintaks penulisan template<typename name> template<class name>  Pemanggilan fungsi / class akan diinstantiasi sesuai tipe yang digunakan template<typename T> void func(T value) { const T& ref=value; T* p=new T; T temp(23); } STL – IKP213
  • 5. Template Parameter 5  Jika ada definisi func<int>, akan digenerate sebuah instantiasi fungsi void func(int value) { const int& ref=value; int* p=new int; int temp(23); }  Semua kemunculan T digantikan int STL – IKP213
  • 6. Template Parameter 6  Jika ada definisi func<string>, akan muncul kesalahan kompilasi void func(string value) { const string& ref=value; string* p=new string; string temp(23); }  Karena string temp(23) tidak valid STL – IKP213
  • 7. Standard Template Library (STL) 7  Koleksi containers dan algorithms  Containers  Class untuk menampung sekumpulan objek  vector, list, queue, deque, priority_queue, stack, map, multimap, set, multiset STL – IKP213
  • 8. STL 8  Deklarasi vector<int> dataInt(5); vector<string> dataStr(5);  Assignment dataInt[0] = 10; dataStr[3] = "Budi";  Menambahkan data vector<string> vectS; vectS.push_back("Kata"); vectS.push_back("siapa"); vectS.push_back("STL susah"); STL – IKP213
  • 9. Loop by Index 9 int ii; for (ii=0; ii < SS.size(); ii++) { cout << SS[ii] << endl; } STL – IKP213
  • 10. Loop by Iterator 10 vector<string>::const_iterator cii; for (cii=SS.begin(); cii!=SS.end(); cii++) { cout << *cii << endl; } STL – IKP213
  • 11. Loop by Reverse Iterator 11 vector<string>::reverse_iterator rii; for (rii=SS.rbegin(); rii!=SS.rend(); ++rii) { cout << *rii << endl; } STL – IKP213
  • 12. Matriks 12 // Vector length of 3 initialized to 0 vector<int> vI1Matrix(3,0); // Vector length of 4 initialized to hold another // vector vI1Matrix which has been initialized // to 0 vector< vector<int> > vI2Matrix(4, vI1Matrix); // Vector of length 5 containing two dimensional // vectors vector< vector< vector<int> > > vI3Matrix(5, vI2Matrix); STL – IKP213
  • 13. Dalam satu baris 13 vector< vector< vector<int> > > vI3Matrix(2, vector< vector<int> > (3, vector<int>(4,0)) ); STL – IKP213
  • 14. Iterator Matriks 14 // Declare two dimensional array vector< vector<int> > vI2Matrix; vector<int> A, B; vector< vector<int> >::iterator iter_ii; vector<int>::iterator iter_jj; A.push_back(10); A.push_back(20); A.push_back(30); B.push_back(100); B.push_back(200); B.push_back(300); vI2Matrix.push_back(A); vI2Matrix.push_back(B); STL – IKP213
  • 15. Iterator Matriks 15 cout << "Using Iterator:" << endl; for(iter_ii=vI2Matrix.begin(); iter_ii!=vI2Matrix.end(); iter_ii++) { for(iter_jj=(*iter_ii).begin(); iter_jj!=(*iter_ii).end(); iter_jj++) { cout << *iter_jj << endl; } } STL – IKP213
  • 16. Operator-operator Lain 16  empty()  size()  resize(n)  front(), v[0]  back()  at(index), v[index]  erase(), clear() STL – IKP213
  • 17. Pustaka 17  STL Tutorial, http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html  STL Tutorial, http://www.mochima.com/tutorials/STL.html  STL Reference, http://www.cplusplus.com/reference/stl/  http://tjerdastangkas.blogspot.com/search/label/ikp213 STL – IKP213
  • 18. AKHIR PERTEMUAN 6 Selasa, 6 Desember 2011