Lớp stack thừa kế từ lớp s_list 4 Lớp queue thừa kế từ lớp stack

Một phần của tài liệu C++ và lập trình hướng đối tượng_chương 10 pdf (Trang 25 - 38)

4. Lớp queue thừa kế từ lớp stack

Các lớp stack và queue không có các thuộc tính riêng. Hai phương thức quan trọng của các lớp này là: virtual int store(void *item) ; // Cất vào một phần tử

virtual void *retrieve () ; // Lấy ra một phần tử

Chú ý là: Lớp stack hoạt động theo nguyên tắc LIFO (vào sau ra trước) còn lớp queue hoạt động theo nguyên tắc FIFO (vào trước ra trước) .

Chương trình sau minh hoạ cách dùng liên kết bội, phương thức ảo và con trỏ kiểu void để quản lý các kiểu dữ liệu khác nhau.

Hoạt động của chương trình như sau:

+ Trước tiên lần lượt đưa địa chỉ của biến đối tượng ts1, chuỗi “HA NOI”, biến nguyên a, biến đối tượng ts2 và biến thực x vào ngăn xếp s1 và hàng đợi q1.

+ Thực hiện phép gán các biến đối tượng: s2 = s1 ;

q2 = q1 ;

+ Lấy các phần tử trong ngăn xếp s2 theo trình tự ngược với lúc đưa vào. + Lấy các phần tử trong hàng đợi q2 theo trình tự như lúc đưa vào.

/*

CT10_05.CPP

Lop vat chua (container) Lop danh sach moc noi Lop ngan xep

Lop hang doi Chu y:

1. constructor sao chep cua lop dan suat 2. toan tu gan cua lop dan suat

3. co the dung cac phuong thuc khac de viet constructor va destructor 4. Dung con tro this

*/ #include <stdio.h> #include <iostream.h> #include <iomanip.h> #include <conio.h> #include <alloc.h> #include <dos.h> //Lop container class container { protected:

unsigned long count; //so pt trong thung chua void (*errhandler)();

public:

container();

container(const container &c); // Ham tao sao chep void operator=(const container &c); // Gan

unsigned long getcount(); // Cho biet so phan tu // Dinh ham xl loi

void seterrorhandler(void (*userhandler)()); // 4 phuong thuc thuan ao

virtual int store(void *item)=0;//Cat mot phan tu vao thung virtual void *examine()=0; // Xem gia tri mot phan tu

virtual void *retrieve ()=0; // Lay mot pt ra

virtual void empty()=0; // Lam cho thung tro nen rong };

// Cai dat

// Ham xl loi mac dinh void defaulthandler(); void defaulthandler()

{

puts("\nContainer error: memory allocation failure"); }

container::container () { (adsbygoogle = window.adsbygoogle || []).push({});

count=0; errhandler= defaulthandler; }

container::container(const container &c) {

count=c.count; errhandler=c.errhandler; }

// Gan

void container::operator=(const container &c) {

count=c.count; errhandler=c.errhandler; }

// Cho biet so pt

unsigned long container::getcount() {

return count; }

// Dinh ham xl loi

void container::seterrorhandler(void (*userhandler)()) {

errhandler=userhandler; }

// Lop danh sach moc noi don class s_list:public container

{

protected:

//Cau truc mot nut trong ds struct listnode

{

void *dataptr; listnode *next; 528

};

listnode *head; listnode *tail; private:

// phuong thuc sao chep void copy(const s_list &s1); public:

s_list();

s_list(const s_list &s1); ~s_list();

void operator=(const s_list &s1); // 4 phuong thuc ao

virtual int store(void *item)=0; // Cat mot phan tu vao // thung

virtual void *examine()=0; // Xem gia tri mot phan tu virtual void *retrieve ()=0; // Lay mot pt ra

virtual void empty(); // Lam cho thung tro nen rong };

//Cai dat

void s_list::copy(const s_list &s1) {

head=NULL; tail=NULL; listnode *temp = s1.head; while(temp!=NULL) (adsbygoogle = window.adsbygoogle || []).push({});

if(head==NULL) {

head= new listnode;

if(head==NULL) errhandler(); tail=head;

} else

{

tail->next = new listnode;

if(tail->next == NULL) errhandler(); tail = tail->next; } tail->dataptr= temp->dataptr; tail->next=NULL; temp = temp->next; } } // constructor s_list::s_list() : container() { head=NULL; tail=NULL; }

s_list::s_list(const s_list &s1):container(s1) {

copy(s1); 530

}

s_list::~s_list() {

this->empty(); }

void s_list::operator=(const s_list &s1) { this->empty(); count=s1.count; copy(s1); } void s_list::empty() { listnode *q,*p;

p = head; head=NULL; tail=NULL; while (p!=NULL) { q=p; p=p->next; delete q; } } // Lop stack

class stack:public s_list {

stack();

stack(const stack &st);

void operator=(const stack &st);

virtual int store(void *item); // Cat mot phan tu vao thung virtual void *examine(); // Xem gia tri mot phan tu

virtual void *retrieve(); // Lay mot pt ra };

stack::stack():s_list() {

}

stack::stack(const stack &st):s_list(st) {

}

void stack::operator=(const stack &st) {

this->s_list::operator=(st); //Dung toan tu gan cua s_list }

int stack::store(void *item) // Cat mot phan tu vao thung {

//Dua vao dau danh sach listnode *p; p= new listnode ; if(p==NULL) return 1; count++; p->dataptr=item; p->next=head; head=p; return 0; }

{

if(count==0) return NULL; else (adsbygoogle = window.adsbygoogle || []).push({});

return head->dataptr; }

void *stack::retrieve() // Lay mot pt ra {

if(count==NULL) return NULL; else

{

listnode *p; void *value; value = head->dataptr; p=head; head = p->next; delete p; count--; return value; } } // Lop queue

class queue:public stack {

public: queue();

queue(const queue &q);

void operator=(const queue &q); 532

virtual int store(void *item); // Cat mot phan tu vao thung };

queue::queue(): stack() {

}

queue::queue(const queue &q):stack(q) {

}

void queue::operator=(const queue &q) {

this->stack::operator=(q); //Dung toan tu gan cua stack }

int queue::store(void *item) {

// Dat vao cuoi listnode *q; q=new listnode; if(q==NULL)return 1; // Bo sung q->next=NULL; q->dataptr=item; if(count==0) { head=q; tail=q; } else

{ tail->next=q; tail=q; } count++; return 0; } class TS { private: char ht[25]; int sobd; float td; public: void nhap() {

cout << "\nHo ten: " ; fflush(stdin);

gets(ht);

cout << "So bao danh: " ; cin >> sobd;

cout << "Tong diem: " ; cin >> td;

}

void xuat() {

cout << "\nHo ten: " << ht;

cout << "\nSo bao danh: " << sobd;

cout << "\nTong diem: " << setiosflags(ios::showpoint) << setprecision(1)<<setw(5)<< td; } }; // Ham main void main() { stack s1,s2; queue q1,q2; TS ts1,ts2,ts; int a=123,b; float x=3.14,y; char *str; clrscr(); ts1.nhap(); ts2.nhap(); //Gui vao s1.store(&ts1); q1.store(&ts1);

s1.store("HA NOI"); q1.store("HA NOI"); s1.store(&a); q1.store(&a);

s1.store(&ts2); q1.store(&ts2); s1.store(&x); q1.store(&x); (adsbygoogle = window.adsbygoogle || []).push({});

//Lay ra tu ngan xep theo nguyen tac LIFO cout <<"\n\nLay ra tu ngan xep:" ;

s2=s1;

y = *((float*)s2.retrieve());

cout << "\nSo thuc = " <<setiosflags(ios::showpoint) << setprecision(2)<< y;

ts = *((TS*)s2.retrieve()); ts.xuat();

b = *((int*)s2.retrieve()); cout << "\nSo nguyen = " << b; str = (char*)s2.retrieve();

cout << "\nChuoi ky tu: " << str; ts = *((TS*)s2.retrieve());

ts.xuat();

//Lay ra tu hang doi theo nguyen tac FIFO cout <<"\n\nLay ra tu hang doi:" ;

q2=q1;

ts = *((TS*)q2.retrieve()); ts.xuat();

str = (char*)q2.retrieve(); cout << "\nChuoi ky tu: " << str; b = *((int*)q2.retrieve());

cout << "\nSo nguyen = " << b; ts = *((TS*)q2.retrieve()); ts.xuat();

y = *((float*)q2.retrieve()); 536

cout << "\nSo thuc = " << setiosflags(ios::showpoint) << setprecision(2)<< y;

getch(); }

Đ 5. Các lớp sắp xếp

Trong tệp C_SORT.H dưới đây sẽ chứa 4 lớp sắp xếp: sort, select_sort, quick_sort và heap_sort. tổng quát hơn. So với các lớp sắp xếp trong mục Đ7 chương 6 thì các lớp ở đây tổng quát hơn ở chỗ:

+ Các lớp trong mục Đ7 chương 6 chỉ cho phép sắp xếp một dẫy số nguyên theo thứ tự tăng dần.

+ Các lớp dưới đây cho phép sắp xếp một dẫy phần tử có kiểu bất kỳ (nguyên, thực, cấu trúc, lớp, ...) và theo một tiêu chuẩn sắp xếp bất kỳ.

Một phần của tài liệu C++ và lập trình hướng đối tượng_chương 10 pdf (Trang 25 - 38)