1/21
Bài 12
CÁC KHÁI NIỆM NÂNG CAO
VỀ CLASS
Hàm friend
2/21
Để một hàm không phải thành viên truy xuất được
các thành viên riêng của lớp phải khai báo bằng từ
khóa friend.
Cần khai báo prototype của hàm bạn trong phần
public của class. Ví dụ
class cl {
//
public:
friend void frnd(cl ob);
//
};
3/21
class myclass {
int a, b;
public:
myclass(int i, int j) { a=i; b=j; }
friend int sum(myclass x); // sum() is a friend of myclass
};
int sum(myclass x)
{
return x.a + x.b;
}
int main()
{
myclass n(3, 4);
cout << "\a"; // ring the bell
}
int main()
{
timer a(10), b("20"), c(1, 10);
a.run(); // count 10 seconds
b.run(); // count 20 seconds
c.run(); // count 1 minute, 10 seconds
return 0;
}
Từ khóa this
6/21
Mỗi khi một hàm thành viên được gọi nó được
truyền một con trỏ chỉ đến đối tượng gọi hàm.
Con trỏ this là một tham số không tường minh đối
với tất cả các hàm thành viên, vì vậy trong hàm
thành viên this có thể được dùng để tham chiếu đến
đối tượng gọi nó.
class cl {
int i;
void f() { };
//
};
Bên trong f( ), câu lệnh gán cho i giá trị 10:
i = 10;
Thực ra câu lệnh trên là viết tắt của :
this->i = 10;
7/21
#include <iostream>
using namespace std;
tham số)
{
Hành vi liên hệ với class
}
10/21
#include <iostream>
using namespace std;
class three_d {
int x, y, z; // 3-D coordinates
public:
three_d() { x = y = z = 0; }
three_d(int i, int j, int k) {x = i; y = j; z = k; }
three_d operator+(three_d op2); // op1 ẩn
three_d operator=(three_d op2); // op1ẩn
void show() ;
};
// Quá tải +.
three_d three_d::operator+(three_d op2)
{
three_d temp;
temp.x = x + op2.x; //x chính là this->x
temp.y = y + op2.y;
temp.z = z + op2.z;
return temp;
}
// Quá tải phép gán.
three_d three_d::operator=(three_d op2)
{
x = op2.x;
y = op2.y;
thừa kế {
Thân class nhận thừa kế
}
13/21
class xe {
int banh;
int nguoi;
public:
void set_banh(int num) { banh = num; }
int get_banh() { return banh; }
void set_ng(int num) { nguoi = num; }
int get_ng() { return nguoi; }
};
class xe_tai : public xe {
int hanghoa;
public:
void set_hanghoa(int size) { hanghoa = size; }
int get_hanghoa() { return hanghoa; }
void show();
};
14/21
#include <iostream>
using namespace std;
// Define a base class for vehicles.
class road_vehicle {
int wheels;
int passengers;
public:
void set_wheels(int num) { wheels = num; }
int get_wheels() { return wheels; }
cout << "wheels: " << get_wheels() << "\n";
cout << "passengers: " << get_pass() << "\n";
cout << "type: ";
switch(get_type()) {
case van: cout << "van\n";
break;
case car: cout << "car\n";
break;
case wagon: cout << "wagon\n";
}
}
int main()
{
truck t1, t2;
automobile c;
t1.set_wheels(18);
t1.set_pass(2);
t1.set_cargo(3200);
t2.set_wheels(6);
t2.set_pass(3);
t2.set_cargo(1200);
t1.show();
cout << "\n";
t2.show();
cout << "\n";
c.set_wheels(4);
c.set_pass(6);
c.set_type(van);
c.show();
return 0;
derived ob;
ob.set(2, 3); // derived hiểu và dùng
ob.show(); // derived hiểu và dùng
ob.setk();
ob.showk();
return 0;
}
Đa hình
18/21
Đa hình là thuật ngữ mô tả một quá trình trong đó
các hiện thực khác nhau của hàm có thể được
truy xuất qua cùng một tên.
"Một giao tiếp, Đa phương thức"
Các con trỏ chỉ đến các kiểu thừa kế
Con trỏ lớp cơ sở và con trỏ lớp thừa kế được liên
hệ với nhau.
19/21
class B_class {
char author[80];
public:
void put_author(char *s) { strcpy(author, s); }
void show_author() { cout << author << "\n"; }
} ;
class D_class : public B_class {
char title[80];
public:
void put_title(char *num) {
strcpy(title, num);
}
void show_title() {
21/21
class base {
public:
virtual void who() { // chỉ ra một virtual
cout << "Base\n";
}
};
class first_d : public base {
public:
void who() { // định nghĩa lại who() quan hệ
//với first_d
cout << "First derivation\n";
}
};
class second_d : public base {
public:
void who() { // định nghĩa lại who() quan hệ
//second_d
cout << "Second derivation\n";
}
};
first_d first_obj;
second_d second_obj;
p = &base_obj;
p->who(); // truy xuất who của base
p = &first_obj;
p->who(); // truy xuất who của first_d
p = &second_obj;
p->who(); // truy xuất who của second_d
return 0;