Praktikum ke Tujuh: Template Programming in C++

6 06 2010
#include <iostream>
#include <string>
using namespace std;

// Swap yang di-template-kan
// dapat dipanggil dengan argumen bermacam-macam tipe
// int a,b, double x,y;
// e.g. swap<int>(a, b) 
// atau swap<double>(x,y)

template <class T>
void swap(T& a, T& b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}



// Stack yang ditemplate-kan..
// Stack<int> StackInt;
// StackInt.push(3); StackInt.push(2);
// Stack<char> StackChar;
// StackChar.push('a'); 

const int MAX = 100;

template <class T>
class Stack
{
    protected:
        int top;
        T arr[MAX];
    
    public:
        Stack()
        :top(0)
        {}
        
        void push(T a)
        {
            arr[top++] = a;
        }
        T pop()
        {
            T a = arr[--top];
            return a;
        }
};

// Ini sebagai class untuk percobaan template
// e.g. swap<Pecahan>(x,y);
// e.g. Stack<Pecahan> StackPecahan;
// dilengkapi dengan operator overloading = dan <
// agar dapat dilakukan sorting 


class Pecahan
{
    protected:
        int a;
        int b;
    public:
        Pecahan(int a_p, int b_p)
        :a(a_p), b(b_p)
        {
                //cout<<"konstruktor 2 arg"<<endl;
        }
        
        Pecahan()
        :a(0), b(1)
        {
                //cout<<"konstruktor tanpa arg"<<endl;
        }
        
        Pecahan(int n)
        :a(n), b(1)
        {
                //cout<<"konstruktor 1 arg"<<endl;
        }
        
        
        void tampil()
        {
                cout<<a<<"/"<<b;
        }
        
        void set_a(int a)
        {
                this->a = a;
        }
        void set_b(int b)
        {
                this->b = b;
        }
        
        int get_a() const
        {
                return a;
        }
        int get_b() const
        {
                return b;
        }
             
       Pecahan operator=(const Pecahan& y)
       {
              this->a=y.a;
              this->b=y.b;
              return (*this);
       }
       
       
       //
       Pecahan operator++()
       {
              this->a = this->a + this->b;
              return (*this);
       }
       
       bool operator<(const Pecahan& p) const
       {
              return (this->a*p.b < this->b*p.a);
       }
       
};


// Algoritma sorting (selection sort) yang tertemplate-kan
// sel_sort<TIPE>(array, number);


template <class T>
void sel_sort(T* a, unsigned n) // Selection Sort
{
    int i,j, min;
    for(i=0;i<n;i++)
    {
        min = i;
        for(j=i+1; j<n; j++)
        {
                if(a[j]<a[min])
                  min = j;
        }
        swap(a[min], a[i]);
    }
}


// Contoh main
int main()
{
    int a = 3, b = 2;
    swap<int>(a,b);
   
    cout<<"a = "<<a<<" b = "<<b<<endl;
   
    double x=2.1, y = 3.4;
    swap<double>(x,y);
    
    cout<<"x = "<<x<<" y = "<<y<<endl;
    
    Stack<int> S;
    S.push(3);
    S.push(4);
    a = S.pop();
    cout<<"a = "<<a<<endl;

    int data[3] = {2,1,5};
    sel_sort(data,3);
    int i;
    for(i=0; i<3; i++)
        cout<<data[i]<<endl;

    Pecahan pec[3];
    pec[0] = Pecahan(1,2);
    pec[1] = Pecahan(2,3);
    pec[2] = Pecahan(1,1);

    mysort<Pecahan>(pec,3);
    for(i=0; i<3; i++)
    {
        pec[i].tampil();
        cout<<endl;
    }
    
    return 0;
}


Contoh lain menggunakan Visual Studio 2010:

#include "stdafx.h"
#include <iostream>
using namespace std;
 
template <class TypeName >
class Test
{
public:
	Test();
	~Test();
	TypeName Data(TypeName);
};

template <class TypeName>
TypeName Test<TypeName>::Data(TypeName a)
{
	return a;
}
 
// constructor
template <class TypeName>
Test<TypeName>::Test()
	{
		cout<<"Constructor, allocate..."<<endl;
	}
 
// destructor
template <class TypeName>
Test<TypeName>::~Test()
	{
		cout<<"Destructor, deallocate..."<<endl;
	}
// main
int main()
{
	Test<int> a;
	Test<double> b;
	Test<char> c;
	Test<char*> d; 

	cout<<"\nOne template fits all data type..."<<endl;
	cout<<"a, int = "<<a.Data(100)<<endl;
	cout<<"b, double = "<<b.Data(1.234)<<endl;
	cout<<"c, char = "<<c.Data('K')<<endl;
	cout<<"d, char* = "<<d.Data("The class template")<<"\n\n";

return 0;
}

function.cpp

#include <iostream>

using namespace std;
template <class T>
T Max(T  a, T  b)
{
       return (a > b)? a : b;
}

int main()
{
	
   cout<<"MyMax(10,20) = "<<Max(10,20)<<endl;
   cout<<"MyMax('Z','p') = "<<Max('Z','p')<<endl;
   cout<<"MyMax(1.234,2.345) = " <<Max(1.234,2.345)<<endl;
 	
}

Bahan praktikum bisa di download di sini.


Actions

Information

Leave a comment