LẬP TRÌNH HƯỚNG ĐỐI TƯỢNGBài 6: Khuôn hình... Khuôn hình hàm... suy diễn không tường minh từ các đối số tương ứng của nó... Tổng kết khuôn hình hàmxác tương ứng giữa các tham số.. Khuôn
Trang 1LẬP TRÌNH HƯỚNG ĐỐI TƯỢNG
Bài 6: Khuôn hình
Trang 2Nội dung
Trang 3Khuôn hình hàm
Trang 4Khuôn hình hàm là gì?
hay 1 nhóm hàm.
thuộc vào dạng dữ liệu.
lập trình mục đích chung.
suy diễn không tường minh từ các đối số tương ứng của nó.
Trang 6cout<<"min1 (n,p) ="<< min1(n,p)<<"\n"; //int min1(int, int)
cout<<"min1 (x,y) ="<< min1(x,y)<<"\n"; //float min1(float, float)
system("PAUSE");
return 1;
}
min1 (n,p) =4
Trang 8vect(int abs=0, int ord=0){x=abs; y=ord;}
void display() { cout<<x<<" "<<y<<"\n"; }
friend int operator<(vect, vect);
};
int operator < (vect a, vect b) {
return a.x*a.x+a.y*a.y< a.x*a.x+a.y*a.y;
Trang 11cout<<"Value of I J:"<<i<<" "<<j<<endl;
cout<<"Value of X Y:"<<x<<" "<<y<<endl;
swap1(i,j);
swap1(x,y);
cout<<"Value of I J after swap:"<<i<<" "<<j<<endl;
cout<<"Value of X Y after swap:"<<x<<" "<<y<<endl;
system("PAUSE");
Value of I J:10 20 Value of X Y:10.1 23.1 Value of I J after swap:20 10 Value of X Y after swap:23.1 10.1 Press any key to continue
Trang 12Thuật toán xây dựng các hàm thể hiện
của tham số thực và tham số hình thức
template <class T> T min1 (T a, T b) {
if (a<b) return a;
else return b;
}
int main() { int n; char c; unsigned int q;
const int r=10; int t[10]; int *adi;
min (n,c); //error min (n,q); //error min (n,r); //error system("PAUSE");
Trang 13Khởi tạo các biến dạng built-in
template <class T> void fct (T a) {
T x(3); //call constructor function of T with value 3
}
fct(int a) {int x(3); } ????
In C++
int x=3; int x(3);
Trang 14Các giới hạn của khuôn hình hàm
template <class T> void min1 (T a, T b) {
if (a<b) return a;
else return b;
}
Trang 15cout <<"compete (t)= "<<compete(t,5)<<"\n";
cout <<"compete (c)= "<<compete(c,6)<<"\n";
system("PAUSE");
n is called expression
parameter
Trang 18Cá biệt hóa một hàm thể hiện
char* min1 (char *cha, char* chb) {
if (strcmp(cha, chb)<0) return cha;
Trang 19Tổng kết khuôn hình hàm
xác tương ứng giữa các tham số.
chuyển kiểu tự động.
Trang 21Khuôn hình lớp là gì?
các lớp với các dạng tham số khác nhau (các thể hiện lớp)
Trang 23 Định nghĩa hàm thành phần trong khai báo lớp
template <class T> class point {
template <class T> void point<T>::display(){
cout<<"coordinate: "<<x<<" "<<y<<"\n";
}
Trang 25point (T abs=0, T ord=0) {x=abs; y=ord;}
void display(){cout<<"coordinate: "<<x<<" "<<y<<"\n";}
};
int main() {
point<int> ai(3,5); ai.display();
point<char> ac('d','y'); ac.display();
point<double> ad(3.5,2.3); ad.display();
system("PAUSE"); return 1;
} coordinate: 3 5coordinate: d y
Trang 26Tham số của khuôn hình lớp
try <float, point<int>, double>
try <point<int>, point<float>, char*>
Trang 29int main() {
table <int, 4> ti;
for (int i=0; i<4; i++)
Trang 31int main() {
point<int> ai(3,5); ai.display();
point<char> ac('d','y'); ac.display();
point<double> ad(3.5,2.3); ad.display();
system("PAUSE"); return 1;
}
coordinate: 3 5coordinate: 100 121coordinate: 3.5 2.3Press any key to continue
Trang 32Sự khác nhau của các thể hiện lớp
Type of t1 is different to type of t2
Type of ta is different to type of tb
Trang 34Khai báo bạn bè
template <class T> class point { };
template <class T int fct (T) { }
template <class T, class U> class try1 {
int x;
public:
friend class point<int>;
friend int fct(double);
friend class point<T>;
friend int fct(U);
Trang 35
Khai báo bạn bè
template <class T, class U> class try3 {
int x;
public:
template <class X> friend class point<X>;
template <class X> friend int fct(X);
};
Trang 370 1 3