Một ví dụ về sử dụng template
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
class sv
{
private :
char ten[100];
float Diem;
public:
sv()
{
Diem=0;
}
sv(char a[],float D)
{
strcpy(ten,a);
Diem=D;
}
sv(sv&a)
{
Diem = a.Diem;
strcpy(ten,a.ten);
}
void set_sv(char a[],float D)
{
strcpy(ten,a);
Diem=D;
}
float get_diem()const
{
return Diem;
}
char* get_ten()
{
return ten;
}
friend ostream&operator <<(ostream&out,sv&);
friend istream&operator>>(istream&in,sv&);
operator float()
{
return float(Diem);
}
};
ostream&operator <<(ostream&out,sv&a)
{
cout<<"\n\n\t\t\tTen "<<a.ten<<endl;
cout<<"\t\t\tDiem "<<a.Diem<<endl;
}
istream&operator>>(istream&in,sv&a)
{
cout<<"\t\t\tNhap ten ";
cin.ignore();
cin.getline(a.ten,50);
cout<<"\t\t\tNhap diem ";
cin>>a.Diem;
}
int ucln(int a,int b)
{
int r;
while(b)
{
r = a%b;
a = b;
b=r;
}
return a;
}
class phanso
{
private:
float tu,mau;
public:
phanso(float a=1,float b=1)
{
if(b)
{
tu = a;
mau = b;
}
else
{
tu =1;
mau=1;
}
}
void set_phanso(float a,float b)
{
tu =a;
mau = b;
}
void nhap()
{
cout<<"\t\t\tNhap du lieu cho phan so "<<endl;
cout<<"\t\t\tTu ";
cin>>tu;
cout<<"\t\t\tMau ";
cin>>mau;
toigian();
}
void toigian()
{
int t=ucln(tu,mau);
tu = tu/t;
mau = mau/t;
}
operator float()
{
return float(tu/mau);
}
friend ostream&operator <<(ostream&out,phanso&a);
friend istream&operator >>(istream&in,phanso&a);
};
ostream&operator<<(ostream&out,phanso&a)
{
out<<a.tu<<"/"<<a.mau<<"->";
}
istream&operator >>(istream&in,phanso&a)
{
cout<<"\t\tTu ";
cin>>a.tu;
cout<<"\t\tMau ";
cin>>a.mau;
}
template <class T,int n>
class set
{
private:
T data[n];
int spt;
public:
set()
{
spt=0;
}
set(const set&a)
{
for(int i=0;i<a.spt;i++)
data[i]=a.data[i];
spt = a.spt;
}
void them(T&a);
bool search(T&a);
friend ostream& operator<<(ostream&out,set<T,n>&a);
friend set operator +(set&a,set&b);
friend set operator *(set&a,set&b);
friend set operator -(set&a,set&b);
set operator =(const set&b)
{
for(int i=0;i<b.spt;i++)
data[i]=b.data[i];
spt=b.spt;
return (*this);
}
};
template <class T,int n>
void set<T,n>::them(T&a)
{
if(spt<n)
data[spt++]=a;
else
cout<<"\t\tMang da day rui khong them duoc nua dau "<<endl;
}
template <class T,int n>
bool set<T,n>::search(T&a)
{
for(int i=0;i<spt;i++)
if(data[i]==a)
return true;
return false;
}
template <class T,int n>
ostream&operator<<(ostream&out,set<T,n>&a)
{
if(a.spt==0)
out<<" rong "<<endl;
for(int i=0;i<a.spt;i++)
{
out<<a.data[i];
if(i<a.spt-1)
cout<<"->";
}
}
template <class T,int n>
set<T,n> operator +(set<T,n>&a,set<T,n>&b)
{
set<T,n> r(a);
for(int i=0;i<b.spt;i++)
if(!a.search(b.data[i]))
r.them(b.data[i]);
return r;
}
template <class T,int n>
set<T,n> operator -(set<T,n>&a,set<T,n>&b)
{
set<T,n> r;
for(int i=0;i<a.spt;i++)
if(!b.search(a.data[i]))
r.them(a.data[i]);
return r;
}
template <class T,int n>
set<T,n> operator *(set<T,n>&a,set<T,n>&b)
{
set<T,n> r;
for(int i=0;i<a.spt;i++)
if(b.search(a.data[i]))
r.them(a.data[i]);
return r;
}
void main()
{
set<float,100> a;
set<float,100> c;
set<float,100> d;
set<float,100> e;
set<float,100> f;
set<sv,100> g;
set<phanso,100> b;
int n,m,l;
float r;
sv A;
phanso s;
cout<<"\t\t\tNhap so luong cac so thu ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<" nhap so thu "<<(i+1)<<":";
cin>>r;
a.them(r);
}clrscr();
cout<<"\t\t\tNhap so luong phan so ";
cin>>m;
for(int i=0;i<m;i++)
{
cout<<"\t\t\tNhap phan so thu "<<(i+1)<<endl;
cin>>s;
b.them(s);
c.them(s);clrscr();
}
clrscr();
cout<<"\t\t\tNhap so luong cac sinh vien ";
cin>>l;
for(int i=0;i<l;i++)
{
cout<<"\t\t\tNhap du lieu cho sinh vien thu "<<(i+1)<<endl;
cin>>A;
g.them(A);
clrscr();
}
clrscr();
textcolor(YELLOW+RED);
cprintf("%s","\t\t\tchuong trinh da gan cac so 1 cach tu dong ta duoc ");
cout<<"\n\nday so thuc vua nhap "<<endl;
cout<<a;
cout<<"\n\nday phan so vua nhap "<<endl;
cout<<b;
cout<<"\n\tDay sinh vien vua nhap "<<endl;
cout<<g;
getch();clrscr();
d = a+c;
cout<<"\n\n hop cua hai tap hop phan so va so thuc la "<<endl;;
cout<<d;
e=a*c;
cout<<"\n\n giao cua hai tap so thuc va phan so la "<<endl;
cout<<e;
cout<<"\n\nhieu cua hai tap so thuc va phan so la "<<endl;
f=a-c;
cout<<f;
getch();
}
Ví dụ về quá tải toán tử
#include <iostream.h>
#include <conio.h>
#include <math.h>
class PS
{
public:
long tu,mau;
PS()
{
tu=0;
mau=0;
}
~PS(){};
int uscln(long a,long b);
void rutgon();
void nhap();
void xuat();
PS operator+(PS &a);
PS operator-(PS &a);
PS operator*(PS &a);
PS operator/(PS &a);
};
int PS::uscln(long a,long b)
{
if(a!=0 && b!=0)
{
a=abs(a);
b=abs(b);
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
}
else
return 1;
}
void PS::rutgon()
{
int u;
u=uscln(tu,mau);
tu=tu/u;
mau=mau/u;
}
void PS::nhap()
{
Nhap:
cout<<"Nhap tu so ";
cin>>tu;
cout<<"Nhap mau so ";
cin>>mau;
if(mau==0)
{
cout<<"Mau phai khac 0"<<endl;
goto Nhap;
}
}
void PS::xuat()
{
rutgon();
if(mau<0)
{mau=-mau; tu=-tu;}
if(tu==0)
cout<<"0"<<endl;
else
if(mau==1)
cout<<tu<<endl;
else
cout<<tu<<"/"<<mau<<endl;
}
PS PS::operator+(PS &a)
{
a.tu=tu*a.mau+mau*a.tu;
a.mau=mau*a.mau;
return a;
}
PS PS::operator-(PS &a)
{
a.tu=tu*a.mau-mau*a.tu;
a.mau=mau*a.mau;
return a;
}
PS PS::operator*(PS &a)
{
a.tu=tu*a.tu;
a.mau=mau*a.mau;
return a;
}
PS PS::operator/(PS &a)
{
a.tu=tu*a.mau;
a.mau=mau*a.tu;
return a;
}