Tính thừa kế trong cơ sở dữ liệu - Pdf 13


Chương 6 Tính kế thừa

Giới thiệu tính kế thừa


Điều khiển truy cập lớp cơ sở


Sử dụng các thành viên được bảo vệ


Hàm tạo, hàm hủy và tính kế thừa


Tính đa kế thừa

Chương 6 Tính kế thừa

165
I/ Giới thiệu tính kế thừa
(inheritance)



access_specifier
base_class_name
{

// body of class
} ;

base_class_name Tên lớp cơ sở
derived_class_name Tên lớp dẫn xuất
access_specifier
chỉ đònh truy cập bao gồm :
public
,
private

protected•
Từ khoá
public
báo cho trình biên dòch biết rằng lớp cơ sở sẽ được kế thừa sao
cho mọi thành viên chung của lớp cơ sở cũng sẽ là các
thành viên chung của lớp
dẫn xuất
. Tuy nhiên, mọi
thành viên riêng
của lớp cơ sở
vẫn còn riêng đối với nó

class
derived
:
public

base
{
int j;
public:
void set_j(int n);
int mul();
};

// Set value i in base.
void
base::set_i
(int n)
{
i = n;
}

// Return value of i in base.
int
base::get_i
() { return i; }

// Set value of j in derived.
void
derived::set_j
(int n)

lớp cơ sở là không thuộc riêng về một lớp dẫn xuất.
Lớp cơ sở có thể được kế
thừa bởi nhiều lớp khác
.

Ví dụ 1.2
Lớp cơ sở chung Fruit có 2 lớp dẫn xuất Apple và Orange
// An example of class inheritance.
#include <iostream.h.h>
#include <string.h>

enum yn {no, yes};
enum color {red, yellow, green, orange};

void out(enum yn x);
char *c[] = {"red", "yellow", "green", "orange"};

// Generic fruit class.
class
fruit
{
// in this base, all elements are public
public:
enum yn annual;
enum yn perennial;
enum yn tree;
enum yn tropical;
enum color clr;
char name[40];
};

public:
void seto(char *n, enum color c, enum yn j, enum yn sr, enum yn e);
void show();
};

void
Apple::seta
(char *n, enum color c, enum yn ck, enum yn crchy,
enum yn e)
{
strcpy(name, n);
annual = no;
perennial = yes;
tree = yes;
tropical = no;
clr = c;
cooking = ck;
crunchy = crchy;
eating = e;
}

void
Orange::seto
(char *n, enum color c, enum yn j, enum yn sr, enum yn e)
{
strcpy(name, n);
Chöông 6 Tính keá thöøa

169
annual = no;

cout << "Tree: "; out(tree);
cout << "Tropical: "; out(tropical);
cout << "Color: " << c[clr] << "\n";
cout << "Good for juice: "; out(juice);
cout << "Sour: "; out(sour);
cout << "Good for eating: "; out(eating); cout << "\n";
}
Chương 6 Tính kế thừa

170
void out(enum yn x)
{
if(x==no) cout << "no\n";
else cout << "yes\n";
}

int main()
{
Apple a1, a2;
Orange o1, o2;

a1.seta("Red Delicious", red, no, yes, yes);
a2.seta("Jonathan", red, yes, no, yes);
o1.seto("Navel", orange, no, no, yes);
o2.seto("Valencia", orange, yes, yes, no);

a1.show();


Từ khoá
private
chỉ đònh

các thành viên chung lớp cơ sở trở thành các
thành
viên riêng của lớp dẫn xuất,
nhưng những thành viên này vẫn còn được truy cập
bởi các hàm thành viên của lớp dẫn xuất.

Ví dụ 2.1
#include <iostream.h>

class base {
int x;
public:
void setx(int n) { x = n; }
void showx() { cout << x << '\n'; }
};

// Inherit as public.
class
derived
:
public

base
{
int y;

void showx() { cout << x << '\n'; }
};

// Inherit as public - this has an error!
class derived :
public
base {
int y;
public:
void sety(int n) { y = n; }

/* Cannot access private member of base class.
x is a private member of base and not available within derived. */
void show_sum() { cout <<
x
+ y << '\n'; } // Error!
void showy() { cout << y << '\n'; }
}; Ví dụ 2.3
Lớp dẫn xuất kế thừa lớp cơ sở với chỉ đònh private
// This program contains an error.
#include <iostream.h>

class base {
int x;
public:
void setx(int n) { x = n; }
void showx() { cout << x << '\n'; }

base_ob.setx(1); // is legal because base_ob is of type base Ví dụ 2.4
Lớp dẫn xuất kế thừa lớp cơ sở với chỉ đònh private, các thành viên vẫn
còn được
truy cập bên trong lớp dẫn xuất
.
// This program is fixed.
#include <iostream.h>

class base {
int x;
public:
void setx(int n) { x = n; }
void showx() { cout << x << '\n'; }
};
Chöông 6 Tính keá thöøa

174
// Inherit base as private.
class derived :
private
base {
int y;
public:
// setx is accessible from within derived
void setxy(int n, int m) {
setx(n)
; y = m; }

Chương 6 Tính kế thừa

175
Bài tập II

1. Xét đoạn chương trình sau
#include <iostream.h>

class mybase {
int a, b;
public:
int c;
void setab(int i, int j) { a = i; b = j; }
void getab(int &i, int &j) { i = a; j = b; }
};

class derived1 :
public
mybase {
// ...
};

class derived2 :
private
mybase {
// ...
};

của lớp cơ sở vẫn là riêng nhưng vẫn cho phép lớp dẫn xuất truy cập tới nó.


Chỉ đònh truy cập
protected
, tương đương với chỉ đònh
private
với một ngoại lệ
duy nhất là các
thành viên được bảo vệ
(protected members
)
của lớp cơ sở có thể
được truy cập đối với các thành viên của một lớp dẫn xuất từ lớp cơ sở đó.

Bên ngoài lớp cơ sở hoặc lớp dẫn xuất, các
thành viên được bảo vệ
không thể
được truy cập.

Dạng tổng quát của khai báo lớp :

class
class_name
{

// private members
protected :
// protected members
public :

177
Ví dụ 3.1
Truy cập các thành viên chung, riêng và được bảo vệ của lớp cơ sở
#include <iostream.h>

class samp {
// private by default
int a;
protected: // still private relative to samp
int b;
public:
int c;

samp(int n, int m) { a = n; b = m; }
int geta() { return a; }
int getb() { return b; }
};

int main()
{
samp ob(10, 20);

// ob.b = 99; Error! b is protected and thus private
ob.c = 30; // OK, c is public

cout << ob.geta() << ' ';
cout << ob.getb() << ' ' << ob.c << '\n';

return 0;
}

void setc(int n) { c = n; }

// this function has access to a and b from base
void showabc() {
cout << a << ' ' << b << ' ' << c << '\n';
}
};

int main()
{
derived ob;

// a and b are not accessible here because they are private to both base and
derived.
ob.setab(1, 2);
ob.setc(3);

ob.showabc();
return 0;
} Ví dụ 3.3
Các
thành viên được bảo vệ
được kế thừa bởi chỉ đònh
protected

// This program will not compile.
#include <iostream.h>

cout << a << ' ' << b << ' ' << c << '\n';
}
};

int main()
{
derived ob;

// ERROR: setab() is now a protected member of base.
ob.setab(1, 2); // setab() is not accessible here.
ob.setc(3);
ob.showabc();

return 0;
}


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status