! 149 void Date:: Display() //เมมเบอรฟง กชนั ของคลาส Date { สรา ง member function ชื่อ Display() สเตตเมนตใ นฟง กช นั ; เปนสมาชิกของคลาส Date โดยกําหนด สเตตเมนตใ นฟง กช นั ; ดว ยเครอ่ื งหมาย :: } void main() { สเตตเมนตใ นโปรแกรม; สเตตเมนตใ นโปรแกรม; } รปู แบบท่ี 3 กาํ หนดรายละเอียดของเมมเบอรฟงกชันอยูภายในคลาส ไมตองเขียนชื่อ คลาส และไมตองเขยี นเครอื่ งหมายแบง แยกขอบเขต :: (scope resolution operator) นาํ หนา ชือ่ เมมเบอรฟ ง กช นั เหมาะสาํ หรบั โปรแกรมทม่ี เี มมเบอรฟ ง กช นั จํานวนไมมาก และมีรายละเอียดของ ฟงกชันสั้น ๆ มรี ปู แบบดงั น้ี class Date { private: // use only in this class int Year; //ดาตา เมมเบอร int Month; //ดาตา เมมเบอร int Day; //ดาตา เมมเบอร public: //use internal and external of this class void SetDate(int Y, int M, int D); //เมมเบอรฟ ง กช ันของคลาส Date { สรา ง member function ชอ่ื SetDate() สเตตเมนตใ นฟง กช นั ; เปน สมาชกิ ของคลาส Date โดยสรา งไว } ในคลาส void Display() //เมมเบอรฟ ง กช นั ของคลาส Date { สรา ง member function ชอ่ื Display() สเตตเมนตใ นฟง กช นั ; เปน สมาชิกของคลาส Date โดยสรา งไว } ในคลาส }; ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 150 v{oid main() } สเตตเมนตใ นโปรแกรม; หมายเหตุ การสรา งคลาสและรายละเอยี ดของเมมเบอรฟ ง กช นั อาจใชหลายรูปแบบรวมกันได ♦!การสรา งและเรยี กใชอ อบจก็ ตจ ากคลาส กอนที่จะสรางออบเจ็กตใชในโปรแกรมไดจะตองสรางคลาสใหถูกตองกอนเพราะ คลาส(class) เปน ตน กําหนดของออบเจ็กต ซึ่งภายในคลาสจะตองกาํ หนด ดาตา เมมเบอรแ ละเมมเบอรฟ ง กช นั ไว และจะตองเขียน code ของเมมเบอรฟ ง กช นั ใหเ สรจ็ สมบรู ณก อ น จึงจะสามารถนาํ ชื่อคลาสไปสราง ออบเจก็ ตแ ละเรยี กใชอ อกเจก็ ตไ ด •! ตัวอยางโปรแกรม clas_ex1.cpp เปนตัวอยางการสรางคลาส การสรางออบเจ็กตและการ เรียกใชออบเจ็กตเบื้องตน โดยสรางเมมเบอรฟงกชันไวตอจากฟงกชัน main() ดังนี้ /*Program : clas_ex1.cpp Process : simple example of class*/ #include <iostream.h> #include <conio.h> class Date //create class Date { private: int Year; //data member int Month; //data member int Day; //data member public: void SetDate(int Y, int M, int D); //member function void Display(); //member function }; void main() //begin main program { Date birthday; //สรางออบเจ็กตชื่อ birthday จากคลาสชื่อ Date clrscr(); cout<<\"Display First OOP Programming\"<<endl; birthday.SetDate(2540,12,25); //เรยี กใชอ อปเจก็ ต birthday และฟงกชั่น SetDate() ทาํ งาน birthday.Display(); //เรยี กใชอ อปเจก็ ต birthday และฟงกชัน display() ทาํ งาน getch(); } //end main program ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 151 void Date::SetDate(int Y, int M, int D) //detail of member function { Year = Y; Month = M; Day = D; } void Date::Display() //detail of member function { cout<<\"Year : \"<<Year<<endl; cout<<\"Month: \"<<Month<<endl; cout<<\"Date : \"<<Day<<endl; } จากโปรแกรมตวั อยา ง สรุปไดสวนสําคญั ได ดงั น้ี 1. การสรา งคลาสหรอื กําหนดคลาสไมใชการสรางออบเจ็กต เปนเพยี งแตแ สดงใหท ราบวา เมอ่ื มกี ารสรา งออบเจก็ ตแ ลว ออบเจก็ ตท ไี่ ดจ ะมีสว นประกอบเหมอื นทม่ี อี ยใู นคลาส ซง่ึ ในโปรแกรมได สรา งออบเจก็ ตข น้ึ 1 ออบเจก็ ตช อ่ื วา birthday จากประโยค Date birthday; แสดงวา birthday เปน ออบเจก็ ตท ม่ี สี ว นประกอบเหมอื นทม่ี อี ยใู นคลาส Date 2. การเรยี กใชเ มมเบอรฟ ง กช นั ของออบเจก็ ต ตอ งเขยี นสเตตเมนตใ หถ กู ตอ ง คอื มี สวนประกอบ 3 สวนคือ ชื่อออบเจ็กต. ชื่อเมมเบอรฟงกชัน() พรอ ม argument ของฟงกชัน(ถามี) เชน ออบเจก็ ต ฟงกชัน birthday.SetDate(2540,12,25); อารกิวเมนตของฟงกชัน ออบเจก็ ต birthday.Display(); ฟงกชัน เครอ่ื งหมาย . มชี อ่ื เรยี กวา คลาสเมมเบอรแ อคเซสโอเปอเรเตอร (class member access operator) เปน โอเปอเรเตอรเ พอ่ื เรยี กใชส มาชกิ ของคลาส ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 152 •! ตัวอยางโปรแกรม clas_ex2.cpp แสดงถงึ การกาํ หนดใหดาตาเมมเบอรและเมมเบอร ฟง กช นั เปน ลกั ษณะ public ดังนั้นการเรียกใชออบเจ็กต birthday ในฟงกชัน main() จึง สามารถเรียกใชดาตาเมมเบอรของ class ไดดวย จึงสามารถกาํ หนดคาคงที่ให Year, Month, Day ไดโดยตรง ดังรายละเอียดโปรแกรมตอไปนี้ /*Program : clas_ex2.cpp Process : using public member of class*/ #include <iostream.h> #include <conio.h> class Date //create class Date { public: //use internal and external class int Year; //data member int Month; //data member int Day; //data member void SetDate(int Y, int M, int D); //member function void Display(); //member function }; void main() //begin main program { Date birthday; //create object name …birthday clrscr(); //set value to data member of class from object birthday.Year=2540; birthday.Month=12; birthday.Day = 25; cout<<\"Display Second OOP Programming\"<<endl; birthday.Display(); getch(); } //end main program void Date::SetDate(int Y, int M, int D) //detail of member function { Year = Y; Month = M; Day = D; } void Date::Display() //detail of member function { cout<<\"Year : \"<<Year<<endl; cout<<\"Month: \"<<Month<<endl; cout<<\"Date : \"<<Day<<endl; } ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 153 •! โปรแกรม oop_exam.cpp แสดงการสรางคลาสที่ชื่อวา VarClass ภายในคลาสประกอบไป ดวย ดาตาเมมเบอร CharVar, IntVar, FloatVar และมีเมมเบอรฟงกชันชื่อ SetValue และ DisplayData โดยเขียนไวภายในคลาส จากนั้นไดสรางออบเจ็กตชื่อ Obj_number ดังตอไปนี้ /*Program : oop_exam.cpp Process : Create New Class and Object*/ #include <iostream.h> #include <conio.h> class VarClass //Begin create class name is... VarClass { private: //data member char CharVar; int IntVar; float FloatVar; public: //member function void SetValue() //function in class { CharVar = 'A'; IntVar = 100; FloatVar= 3.14159; } void DisplayData() //function in class { cout<<\"Character : \"<<CharVar<<endl; cout<<\"Integer : \"<<IntVar<<endl; cout<<\"Float : \"<<FloatVar<<endl; } }; //end of class void main() //begin main program { VarClass Obj_number; //create object name is Obj_number clrscr(); Obj_number.SetValue(); Obj_number.DisplayData(); getch(); } // end main program ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 154 •! ตัวอยางโปรแกรม oop_exa2.cpp มผี ลการทํางานเหมอื นโปรแกรม oop_exam.cpp แต เขยี นเมมเบอรฟ งกช ันไวนอกคลาส และสรางไวหลังฟงกชัน main() มรี ปู แบบดงั น้ี /*Program : oop_exam.cpp Process : Create New Class and Object*/ #include <iostream.h> #include <conio.h> class VarClass //Begin create class name is... VarClass { private: //data member char CharVar; int IntVar; float FloatVar; public: void SetValue(); //member function void DisplayData(); //member function }; //end of class void main() //begin main program { VarClass Obj_number; //create object clrscr(); Obj_number.SetValue(); //call object and function Obj_number.DisplayData(); //call object and function getch(); } void VarClass::SetValue() //function in class { CharVar = 'A'; IntVar = 100; FloatVar= 3.14159; } void VarClass::DisplayData() //function in class { cout<<\"Character : \"<<CharVar<<endl; cout<<\"Integer : \"<<IntVar<<endl; cout<<\"Float : \"<<FloatVar<<endl; } ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 155 •! ตัวอยางโปรแกรม clas_ex3.cpp มกี ารสรา งเมมเบอรฟ ง กช นั GetDate() เพิ่มใน โปรแกรมเพอ่ื ทําหนา ทร่ี บั คา คงทท่ี างคยี บ อรด และสรางออบเจ็กตชื่อ Dday เพ่ิมขน้ึ อกี หนึ่งออบเจ็กต ดังนี้ /*Program : clas_ex3.cpp Process : using public member of class */ #include <iostream.h> #include <conio.h> class Date //create class Date { public: //use internal and external class int Year; //data member int Month; //data member int Day; //data member void GetDate(); //member function ...enter date void SetDate(int Y, int M, int D); //member function...send argumen void Display(); //member function...display date }; void main() //begin main program { Date birthday,Dday; //create 2 objects from Date class are birthday and Dday clrscr(); //set value to data member of class birthday.GetDate(); cout<<\"Display Date enter from keyboard by object :birthday\"<<endl; birthday.Display(); getch();clrscr(); cout<<endl<<\"Display Date from argument by object : Dday\"<<endl; Dday.SetDate(2530,11,29); Dday.Display(); getch(); } //end main program void Date::GetDate() //detail of member function GetDate() { cout<<\"Enter new Year<yyyy>: \";cin>>Year; cout<<\"Enter new Month<mm>: \";cin>>Month; cout<<\"Enter new Day<dd>: \";cin>>Day; } ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 156 void Date::SetDate(int Y, int M, int D) //detail of member function SetDate() { Year = Y; Month = M; Day = D; } void Date::Display() //detail of member function display() { cout<<\"Year : \"<<Year<<endl; cout<<\"Month: \"<<Month<<endl; cout<<\"Date : \"<<Day<<endl; } •! โปรแกรม oop_ex1.cpp แสดงการสรา งคลาสชอ่ื Calculate กําหนดใหมีสมาชิกเปน data member ลกั ษณะ private จาํ นวน 2 ตัว คือ first และ second กําหนดใหม ี member function จาํ นวน 4 ฟงกชัน คือ sum(), subtract(), multiply(), divide() เพอ่ื ทําหนาที่ในการ คาํ นวณการบวก,ลบ,คูณและหาร ของเลข 2 จาํ นวนตามลาํ ดับ ในโปรแกรมมกี ารสรา ง object จาํ นวน 1 objcect ชื่อ math แลว นําออบเจก็ ตน ม้ี าเรยี กใชใ นโปรแกรม ดังตอไปนี้ /*Program : oop_exp1.cpp Process : calculate */ #include <iostream.h> #include <conio.h> class Calculate //create class { private: float first; float second; public: float sum(float x, float y); float subtract(float a, float b); float multiply(float a, float b); float divide(float a, float b); }; //declaration prototype void input(); //Not member function //declar global variable float number1,number2; Calculate math; //create object ....math from Calculate class ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 157 void main() //begin main program { input(); cout<<\"\\nResult of sum = \"<<math.sum(number1,number2); cout<<\"\\nResult of subtract = \"<<math.subtract(number1,number2); cout<<\"\\nResult of multiply = \"<<math.multiply(number1,number2); cout<<\"\\nResult of devide = \"<<math.divide(number1,number2); getch(); } //end main program void input() // Normal function enter 2 number , non member of class { clrscr(); cout<< \"input first number : \";cin>>number1; cout<< \"input first number : \";cin>>number2; } //member function sum() of class Calculate float Calculate::sum(float first, float second) { return first+second; } //member function subtract() of class Calculate float Calculate::subtract(float first, float second) { return first-second; } //member function multiply() of class Calculate float Calculate::multiply(float first, float second) { return first*second; } //member function divide() of class Calculate float Calculate::divide(float first, float second) { return first/second; } ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 158 •! ตัวอยางโปรแกรม oop_ex2.cpp แสดงการคํานวณการตดั เกรด โดยการสรางคลาสชื่อ Grade และสรางออบเจ็กตชื่อ Evalue มกี ระบวนการทํางานของ member function อยู 3 ฟงกชัน คือ GetScore() ทาํ หนาที่รับคะแนนระหวางภาคและปลายภาค รวมคะแนน, Calculate() ทาํ หนาที่นาํ คะแนนรวมไปคาํ นวณตัดเกรด และ Display() ทาํ หนาที่แสดงผล คะแนนรวมและเกรดที่ไดรบั ดังรายละเอียดในโปรแกรมตอไปนี้ /*Program: grade2.cpp Process: calculate grade from total score by OOP Programming*/ #include <iostream.h> #include <conio.h> class Grade //create class ...Grade { private: float midterm; //data member float final; float total; char gd; public: void GetScore(); //member function void Calculate(); void Display(); }; Grade Evalue; //Create object ....Evalue void main() //begin main program { clrscr(); cout<< \"Program Calculate Grade\"<<endl; //use object and function of class Evalue.GetScore(); Evalue.Calculate(); Evalue.Display(); getch(); } void Grade::GetScore() //member function of Grade class { cout<< \"Enter midterm score: \";cin>>midterm; cout<< \"Enter midterm score: \";cin>>final; total=midterm+final; } ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 159 void Grade::Calculate() //member function of Grade class { //calculate grade use if...else if.. if (total<0 || total>100) //check enter error score gd = '*'; else if (total>=0 && total<=49) gd='F'; else if(total>=50 && total<=59) gd='D'; else if(total>=60 && total<=69) gd='C'; else if(total>=70 && total<=79) gd='B'; else gd='A'; } void Grade::Display() //member function of Grade class { cout<< \"Total of your score: \\a\"<<total<<endl; cout<< \"You get grade : \"<<gd<<endl; if (gd=='*') cout<< \"Your score = \"<<total<< \" is error range !!!\"<<endl; } •! ตัวอยางโปรแกรม exp_oop1.cpp แสดงการเขยี นโปรแกรมแบบ OOP โดยมเี มมเบอร ฟงกชัน 2 ฟงกชัน คือ input() และ display() โดยทฟ่ี ง กช นั input() ทาํ หนา ทร่ี บั ขอ มลู ทาง แปนพิมพ และฟงกชัน display() ทําหนา ทแ่ี สดงผลขอ มลู ทก่ี รอกทางแปน พมิ พไ ว /*Program: EXP_OOP1.CPP Process: Input,Display data function*/ #include <iostream.h> #include <conio.h> #include <stdio.h> class TEST //create class { public: char name[30]; //data member char address[50]; int age; public: //member function void input(); void display(); }; ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 160 TEST Info; //create object ...Info... from class void main() //begin main program { clrscr(); Info.input(); Info.display(); } void TEST::input() //Create member function of class TEST { cout<<\"Enter your information: \"<<endl; cout<<\"Name: \"; gets(name); cout<<\"Address: \"; gets(address); cout<<\"Age: \"; cin>>age; } void TEST::display() //Create member function of class TEST { clrscr(); cout<<\"*********************\"<<endl; cout<<\"Your information... \"<<endl; cout<<\"*********************\"<<endl; cout<<\"Name: \"<<name<<endl; cout<<\"Address: \"<<address<<endl; cout<<\"Age: \"<<age <<endl; cout<<\"*********************\\a\\a\"<<endl; getch(); } •! ตัวอยางโปรแกรม Draw_cir.cpp เปน โปรแกรมสรา งออบเจก็ ต ชื่อ TheCircle จากคลาส Circle โดยกําหนดเมมเบอรฟงกชันเพื่อกาํ หนดตาํ แหนง วาดวงกลมโดยการกําหนดคา SetCircle() และรับคาตําแหนง ของวงกลม InputCircle() พรอมแสดงผลการวาดวงกลม Display() ดังตอไปนี้ /*Program : Draw_cir.cpp Process : set position and draw circle graphics by OOP */ #include <iostream.h> #include <stdlib.h> #include <graphics.h> ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 161 การเขียนโปรแกรมแบบ OOP #include <conio.h> class Circle { private: int X, Y, Radius; public: void SetCircle(int,int,int); void InputCircle(); void Display(); }; void main() { Circle TheCircle; //create object ....TheCircle clrscr(); /* request auto detection */ int gdriver = DETECT, gmode, errorcode; /* initialize graphics mode */ initgraph(&gdriver, &gmode, \"\"); TheCircle.SetCircle(300,200,200); //set X,Y and Radius TheCircle.Display(); //Display Circle TheCircle.SetCircle(300,200,100); //set X,Y and Radius TheCircle.Display(); //Display Circle getch();clrscr(); TheCircle.InputCircle(); //Enter X,Y and Radius TheCircle.Display(); //Display Circle getch(); closegraph; } void Circle::SetCircle(int Xc, int Yc, int Rc) { X=Xc; Y=Yc; Radius=Rc; } void Circle::Display() { circle(X,Y,Radius); } ! ศิริชัย นามบุรี
! 162 void Circle::InputCircle() { closegraph; cout<< \"Enter data for circle:\"<<endl; cout<< \"Center point : X = \";cin>>X; cout<< \"Center point : Y = \";cin>>Y; cout<< \"Radius : \";cin>>Radius; } ♦ต! วั อยา งการเขยี นโปรแกรมแบบ OOP จดั การ Array และ Structure เราสามารถจดั การขอมลู ท่เี ปน array โดยสรางใหเปนขอมลู ชนิด Object ได ดงั ตวั อยา ง โปรแกรมตอ ไปน้ี ตัวอยางโปรแกรม OOP_ARR.CPP แสดงการใช array ทีเ่ ปน Object ชื่อ Object คือ result [5] สรางจาก class ที่ชื่อ TEST โดยจอง array ไวทั้งหมด 5 ชอ ง มี member function ของ class คือ input(), summation(), display() โ ดยที่ฟงกชัน input() ทาํ หนาที่รับขอมูลเขาไปเก็บใน data ที่ชื่อ number1, number2 ฟงกชัน summation() ทาํ หนา ทรี วมจํานวน number1+number2 เก็บไวใน sum และฟงกชัน display() ทาํ หนาที่แสดงขอมูล array ใน data ที่ชื่อ number1, number2 ที่ไดกรอกไว 5 จํานวน /*Program: OOP_ARR.CPP Process: Uses array of Object for Input,Summation,Display data function*/ #include <iostream.h> #include <conio.h> #include <stdio.h> class TEST //create class { public: //data members int number1; int number2; int sum; public: //member function void input(); void display(); void summation(); }; ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 163 การเขียนโปรแกรมแบบ OOP TEST result[5]; //Create Object ....is Array result[5] void main() { clrscr(); for(int i=0;i<=4;i++) { result[i].input(); } cout<<endl<<\"Display number 1, number 2 in array\"<<endl; for(i=0;i<=4;i++) { result[i].display(); } //Calculate Summation number1+number2 in array for(i=0;i<=4;i++) { result[i].summation(); } getch(); cout<<endl<<\"Display Summation: \"<<endl; for(i=0;i<=4;i++) { cout<<\"Sum[\"<<i<<\"]\"<<result[i].sum<<endl; } getch(); } //end main program void TEST::input() //Create member function of class TEST { cout<<\"Enter your number: \"<<endl; cout<<\"Number1: \";cin>>number1; cout<<\"Number2: \";cin>>number2; } void TEST::display() //Create member function of class TEST { cout<<\"Your number : \"<<endl; cout<<\"Number1: \"<<number1<<endl; cout<<\"Number2: \"<<number2<<endl<<endl; } ! ศิริชยั นามบุรี
! 164 void TEST::summation() { sum=number1+number2; } •! ตัวอยางโปรแกรม OOP_STRU.CPP เปน ตวั อยา งโปรแกรมเขยี นแบบ OOP เพื่อจัดการ ขอ มลู ทเ่ี ปน อารเ รยข องโครงสรา ง มีดังตอไปนี้ /*Program: OOP_STRU.CPP Process: Used OOP manage Array of Structure */ #include <iostream.h> #include <conio.h> #include <stdio.h> //for gets() function struct PER //Create Structure Data Type ....Global { char code[5]; char name[30]; char position[20]; float salary; }; class EMP //Create Class for origin of Object { public: PER person[5]; //data member is ...array of PER public: void Input(); //member function void Display(); void Report(); }; EMP Employee; //Creat Object from class ...EMP void main() { clrscr(); Employee.Input(); Employee.Display(); getch();clrscr(); ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 165 Employee.Report(); getch(); } void EMP::Input() //Detail of member function { int i; for(i=0;i<=4;i++) { cout<<\"Code: \";gets(person[i].code); cout<<\"Name: \";gets(person[i].name); cout<<\"Position: \";gets(person[i].position); cout<<\"Salary: \";cin>>person[i].salary; } } void EMP::Display() //Detail of member function { int i; clrscr();cout<<\"Display Information\"<<endl; for(i=0;i<=4;i++) { cout<<\"Record#\"<<i+1<<endl; cout<<person[i].code<<endl; cout<<person[i].name<<endl; cout<<person[i].position<<endl; cout<<person[i].salary<<endl; getch();clrscr(); } } void EMP::Report() //Detail of member function { int r=6,i; clrscr(); gotoxy(25,1);cout<<\"Report Salary Expense\"; gotoxy(25,2);cout<<\"Sirichai Export Co.ltd\"; gotoxy(1,3);cout<<\"----------------------------------------------------\"; gotoxy(1,4);cout<<\"Code Name - Surname Position Salary\"; gotoxy(1,5);cout<<\"----------------------------------------------------\"; for(i=0;i<=4;i++) { gotoxy(1,r);cout<<person[i].code; gotoxy(8,r);cout<<person[i].name; gotoxy(25,r);cout<<person[i].position; gotoxy(40,r);cout<<person[i].salary; ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 166 r++; if(r>=22) { getch(); r=6; } } } ♦!ฟง กช นั ชนดิ คอนสตรกั เตอรแ ละดสิ ตรกั เตอร จากตัวอยางที่ผานมาในการกาํ หนดคาใหแกดาตาเมมเบอรของคลาสในขณะเรียกใช ออปเจก็ ตน น้ั เราจะกาํ หนดคาคงที่ใหขณะที่มีการเรียกใช เชน Dday.SetDate(2530,11,29); หรอื มกี ารรบั คา ทางคยี บ อรด เพอ่ื กาํ หนดคา ดาตาเมมเบอร การกาํ หนดคาใหแกดาตาเมมเบอรสามารถกาํ หนดใหม คี า เรม่ิ ตน แบบอตั โนมตั ไิ ด โดยการใช เมมเบอรฟ ง กช นั ชนดิ ทเ่ี รยี กวา คอนสตรักเตอร (constructor) ซึ่งจะเปนเมมเบอรฟงกชันที่ทาํ งานโดย อั ต โ น มั ติ ทั น ที ที่ อ อ บ เ จ็ ก ต ถู ก ส ร า ง ข้ึ น เ พ ร า ะ ฉ ะ น้ั น จ ะ ต อ ง กํ า ห น ด ค า เ ร่ิ ม ต น ข อ ง ดาตาเมมเบอรไ วเ ปน คา เรม่ิ ตน ดว ย การสรา งเมมเบอรฟ ง กช นั ชนดิ คอนสตรกั เตอร สรางไวใน class กาํ หนดชอ่ื ใหเ หมอื นกบั ชอ่ื ของ class และกาํ หนดคาคงที่ใหแก ดาตาเมมเบอรภ ายในเครอ่ื งหมาย { } ดงั รปู แบบตวั อยา งในโปรแกรม cons_oop.cpp •! ตัวอยางโปรแกรม cons_oop.cpp แสดงการสรา ง constructor เพื่อกําหนดคาเริ่มตน อัตโนมัติเมื่อมีการใชออปเจ็กต มีรายละเอียดดังนี้ /*Program : cons_oop.cpp Process : create constructor member function of class*/ #include <iostream.h> #include <conio.h> class Date //create class Date { private: int Year; //data member int Month; //data member int Day; //data member public: Date() //constructor member function…function name same as class name { Year = 1997; //คาคงที่ของ data member ที่เปนคาเริ่มตน Month=12; Day=31; } ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 167 void SetDate(int Y, int M, int D); //member function void Display(); //member function }; void main() //begin main program { Date birthday; clrscr(); cout<<\"Display Constructor member function : OOP Programming\"<<endl; //use constructor cout<<endl<<\"Display Date from constructor member function\"<<endl; birthday.Display(); //set value of data member cout<<endl<<\"Display Date from setting value\"<<endl; birthday.SetDate(2540,10,22); birthday.Display(); getch(); } //end main program void Date::SetDate(int Y, int M, int D) //detail of member function { Year = Y; Month = M; Day = D; } void Date::Display() //detail of member function { cout<<\"Year : \"<<Year<<endl; cout<<\"Month: \"<<Month<<endl; cout<<\"Date : \"<<Day<<endl; } ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 168 ผลการทํางานของโปรแกรม Display Constructor member function : OOP Programming Display Date from constructor member function //คา ทไ่ี ดเ กดิ จากการใช constructor Year : 1997 Month: 12 Date : 31 Display Date from setting value //คา ทไ่ี ดเ กดิ จากการกาํ หนดคา Year : 2540 Month: 10 Date : 22 เมอ่ื มกี ารสรา ง constructor ซึ่งจะทาํ ใหอ อบเจก็ ตส ามารถมคี า เรม่ิ ตน ทาํ งานไดโ ดยอตั โนมตั ไิ ด การยกเลิก constructor หรือยกเลิกการทํางานของออบเจก็ ตโ ดยอตั โนมตั ิ เพื่อยกเลิกการใชหนวยความ จาํ จะตอ งสรา งเมมเบอรฟ ง กช นั ชนดิ ทเ่ี รยี กวา ดิสตรกั เตอร (destructor) ซึ่งจะมีชื่อเดียวกับคอนสต รกั เตอรเ พยี งแตม เี ครอ่ื งหมาย ~ กาํ กับที่หนาชื่อฟงกชันไมมีพารามิเตอรและไมมีรายละเอียดในเครื่อง หมาย { } เชน class Date //create class Date { private: int Year; //data member int Month; //data member int Day; //data member public: Date() //constructor member function…function name same as class name { Year = 1997; //คาคงที่ของ data member ที่เปนคาเริ่มตน Month=12; Day=31; } ~Date() { }; //สรางดิสตรักเตอร void SetDate(int Y, int M, int D); //member function }; void Display(); //member function หมายเหตุ ทั้งฟงกชันประเภท คอนสตรกั เตอรแ ละดสิ ตรกั เตอร จะไมมีการสงคาออกจาก ฟงกชัน ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 169 ♦ค! ุณสมบัติการสืบทอด (inheritance) การสบื ทอด (inheritance) เปนคุณสมบัติสาํ คญั ของการเขยี นโปรแกรมเชงิ วตั ถเุ ปา หมาย (OOP) หมายถึง การถายทอดคุณสมบัติตาง ๆ ของออบเจก็ ตท เ่ี ปน ตน กาํ เนดิ หรอื บรรพบรุ ษุ ซง่ึ เราเรยี ก วา แอนเซสเตอร (ancestor) ไปยังออบเจ็กตทีเ่ ปน ลูกหลานหรือผูสบื ทอด เราเรยี กวา ดีเซนแด็นต (desendant) ไดห ลายออบเจก็ ต ทาํ ใหเกิดความสัมพันธระหวางคลาสและออบเจ็กตเปน ลําดับชั้นเหมือนกับการลาํ ดบั ความสมั พนั ธข องเครอื ญาติ การถายทอดคุณสมบัติของคลาสจะเริ่มจากคลาสเริ่มตน ที่เรยี กวา เบสคลาส (base class) หรอื แอนเซสเตอร (ancestor) ไปยังคลาสใหมที่สืบเนื่องจากคลาสเริ่มตน เรียกคลาสที่สืบเนื่องนี้วา ดี ไรฟคลาส (derived class) หรอื ดีเซนแด็นต (desendant) ดีไรฟคลาส (derived class) จะสืบทอดคุณสมบัติทั้งหมดของ ดาตา เมมเบอรแ ละเมมเบอร ฟงกชันที่มีอยูใน เบสคลาส (base class) ยกเวน คอนสตรกั เตอรแ ละดสิ ตรกั เตอร ลักษณะความสัมพันธระหวาง base class กับ derived class จะอยใู นรปู แบบ 4 ลักษณะ ดงั น้ี 1. หนึ่งดีไรฟคลาส สืบทอดคุณสมบัติจาก หนึ่งเบสคลาส ดงั รปู ! เบสคลาส D ! ดไี รฟค ลาส A 2. หลายดีไรฟคลาส สืบทอดคุณสมบัติจาก หนึ่งเบสคลาส ดงั รปู ! เบสคลาส D ! ดไี รฟค ลาส A ! ดไี รฟค ลาส B ! ดไี รฟค ลาส C ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 170 3. หนึ่งดีไรฟคลาส สืบทอดคุณสมบัติจาก หลายเบสคลาส ดงั รปู ! เบสคลาส A ! เบสคลาส B ! เบสคลาส C ! ดไี รฟค ลาส D 4. หลายดีไรฟคลาส สืบทอดคุณสมบัติตอเนื่องกันไปหลายลาํ ดับจาก หนึ่งเบสคลาส ดงั รปู ! เบสคลาส D ! ดไี รฟค ลาส A ! ดไี รฟค ลาส B ! ดีไรฟคลาส C รปู แบบการสรา ง ดีไรฟคลาส (derived class) จะมีรูปแบบและสวนประกอบตางๆ เหมอื นกับ การสรา ง เบสคลาส มีรปู แบบ ดงั น้ี class ชอ่ื ดไี รฟค ลาส : private หรอื public หรอื protected ชอ่ื เบสคลาส { private หรอื public หรอื protected : ดาตาเมมเบอร; ดาตาเมมเบอร; ดาตาเมมเบอร; private หรอื public หรอื protected : เมมเบอรฟ ง กช นั ; เมมเบอรฟ ง กช น่ั ; }; ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 171 ตัวอยางเชน การสรา งเบสคลาส ชื่อ Draw และสรา งดีไรฟคลาส ชื่อ Point class Draw //เบสคลาส { protected: int X, Y, Color; public: Draw(int InitX, int InitY, int InitColor) //คอนสตรกั เตอร { X = InitX; Y = InitY; Color = InitColor; } ~Draw() //ดสิ ตรกั เตอร { closegraph(); } }; class Point : public Draw //ดีไรนคลาสชื่อ Point สบื ทอดมาจากคลาส Draw { public: //คอนสตรักเตอรฟงกชันชื่อ Point Point(int InitX, int InitY, Int InitColor) : Draw(InitX, InitY, InitColor) {}; void Display(); void Hide(); }; การกําหนดเบสคลาสและดไี รฟค ลาสจากตวั อยา ง มคี วามหมายดงั น้ี - Draw เปน เบสคลาส มีคลาส Point เปน ดไี รฟค ลาส แสดงวาคลาส Point จะสามารถสืบ ทอดคณุ สมบตั ติ าง ๆ มาจากคลาส Draw ได - class Point : public Draw เปน ประโยคกําหนดใหคลาส Point เปนดีไรฟคลาสของคลาส Draw คําวา public กําหนดอยหู นาชอ่ื เบสคลาส Draw หมายถึง ออบเจก็ ตท ส่ี รา งโดยใช ดีไรฟคลาสนั้น จะสามารถเรียกใชสมาชิก (ดาตา เมมเบอรแ ละเมมเบอรฟ ง กช นั ) ของเบสคลาสในสวน ท่เี ปน public ไดทั้งหมด ตามตวั อยา ง ถา สรา งออบเจก็ ตช อ่ื Apoint จากดีไรฟคลาส Point Point Apoint; แสดงวา ออบเจต็ ก Apoint เปนออบเจ็กตที่สรางจากดีไรฟคลาสที่ชื่อ Point จะ สามารถเรียกใชฟงกชันในคลาส Draw ไดทุกฟงกชัน - protected: ในคลาส Draw หมายถึง สมาชิกของคลาส (อาจเปน ดาตา เมมเบอรห รอื เมมเบอร ฟงกชันก็ได) ที่อยูภายใตคําวา protected จะยอมใหดีไรฟคลาสของคลาสนี้เทานั้นที่สามารถเรียกใชได ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 172 ในตัวอยางแสดงวา นอกจากการเรียกใชสมาชิกในคลาส Draw ไดแลว คลาส Point จะสามารถเรียก ใชด าตาเมมเบอร X, Y และ Color ของคลาส Draw ไปใชไดดวย เนื่องจากคลาส Point เปน ดไี รฟค ลาส ของ Draw นน่ั เอง (เรียกวาลักษณะการสืบทอดคุณสมบัต)ิ ♦!ตวั อยา งการสบื ทอดคณุ สมบตั ใิ นโปรแกรม โปรแกรม INHER_EX.CPP แสดงการสืบทอดคุณสมบัติ (ฟงกชันและขอมูล) ของคลาสใน รปู แบบ หนึ่งเบสคลาสสืบทอดคุณสมบัติไปสูหลายดีไรฟคลาส มโี ครงสรา งการสืบทอด ดงั น้ี ! !(bavseehiccllaess) มี member function ! set_wheels() ! get_wheel() ! set_pass() ! get_pass() ! !(deส!!rรivาtteงr1du,octbkc2jleacsst1) ! !!(dสearรui!าtvoงemdcoocbbljaielscest2) ! มี member function มี member function ! set_cargo() ! set_type() ! get_cargo() ! get_type() ! show() ! show() จากโครงสรางการสืบทอดของเบสคลาส vehicle ไปสูดีไรฟคลาส คือ truck , automobile ไดมีเรียกใช object ที่สรางจากคลาส truck และ automobile แตเ รยี กใชค ณุ สมบัติหรือสบื ทอดคณุ สมบตั ิทเี่ ปน member function ของคลาส vehicle ซึ่งเปนเบสคลาส ในโปรแกรมดงั น้ี t1.set_wheels(18); t1.set_cargo(3200); t2.set_wheels(6); t2.set_pass(3); c.set_wheels(4); c.set_pass(6); •! ตวั อยางโปรแกรม INHER_EX.CPP เปนโปรแกรมแสดงการใชคุณสมบัติ Inheritance ของการเขียนโปรแกรมแบบ OOP ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 173 /*Program : INHER_EX.CPP Process : show inheritance from base class to derived class class vehicle --> class turck, automobile */ #include <iostream.h> #include <conio.h> class vehicle //base class { int wheels; int passengers; public: //declared public function in class void set_wheels(int num); int get_wheels(); void set_pass(int num); int get_pass(); }; class truck:public vehicle //derive class of class vehicle { int cargo; public: void set_cargo(int size); int get_cargo(); void show(); }; enum type_auto{car,van,wagon}; //set enumerated data type class automobile:public vehicle //derive class of class vehicle { type_auto car_type; //enumerated variable public: void set_type(enum type_auto t); enum type_auto get_type(); void show(); }; void vehicle::set_wheels(int num) { wheels=num; } int vehicle::get_wheels() { return wheels; } ! ศิริชัย นามบุรี การเขียนโปรแกรมแบบ OOP
! 174 การเขียนโปรแกรมแบบ OOP void vehicle::set_pass(int num) { passengers=num; } int vehicle::get_pass() { return passengers; } //begin created member function of truck class void truck::set_cargo(int num) { cargo=num; } int truck::get_cargo() { return cargo; } void truck::show() { cout<< \"Wheels : \"<<get_wheels()<<\"\\n\"; cout<< \"Passengers : \"<<get_pass()<<\"\\n\"; cout<< \"Cargo capacity in cubic feet : \"<<cargo<<\"\\n\"; } //begin created member function of automobile class void automobile::set_type(enum type_auto t) { car_type=t; } type_auto automobile::get_type() { return car_type; } void automobile::show() { cout<<\"Wheels : \"<<get_wheels()<<\"\\n\"; cout<<\"Passengers: \"<<get_pass()<<\"\\n\"; ! ศิริชยั นามบุรี
! 175 cout<<\"Type : \"; switch(get_type()) { case van: cout<<\"Van\\n\";break; case car: cout<<\"Car\\n\";break; case wagon: cout<<\"Wagon\\n\";break; } } void main() //Begin main program { clrscr(); cout<<\"Display Inheritance from Class vehicle to truck and automobile....\"<<endl<<endl; truck t1,t2; //created object...t1 and t2 from class truck automobile c; //created object...c from class automobile //used object and called function member t1.set_wheels(18); //use inheritance set_wheels() from based class...vehicle t1.set_pass(2); //use inheritance set_pass() from based class...vehicle t1.set_cargo(3200); //use function set_cargo() from derived class...truck t2.set_wheels(6); //use inheritance set_wheels from based class...vehicle t2.set_pass(3); //use inheritance set_pass() from based class...vehicle t2.set_cargo(1200); //use function set_cargo() from derived class...truck t1.show(); //use function show() derived class...truck cout<<endl; t2.show(); //use function show() derived class...truck c.set_wheels(4); //use inheritance set_wheels() from base class...vehicle c.set_pass(6); //use inheritance set_pass() from base class...vehicle c.set_type(van); //use function set_type() from derived class...automobile cout<<endl; c.show(); //use function show() from derived class...automobile getch(); } ผลการ run โปรแกรม เปน ดงั น้ี Display Inheritant from Class vehicle to truck and automobile.... การเขียนโปรแกรมแบบ OOP Wheels : 18 ! ศิริชยั นามบุรี
! 176 Passengers : 2 Cargo capacity in cubic feet : 3200 Wheels : 6 Passengers : 3 Cargo capacity in cubic feet : 1200 Wheels : 4 Passengers: 6 Type : Van •! ตัวอยางโปรแกรม Dra_cir2.cpp แสดงการถายทอดคุณสมบัติ (inheritance) จากเบส คลาส Draw ไปสูดีไรฟคลาสที่ชื่อ Point /*Program : Dra_cir2.cpp Process : display and hide point from derived class */ #include <iostream.h> #include <stdlib.h> #include <graphics.h> #include <conio.h> class Draw //base class { protected: //ใชไดเฉพาะในเบสคลาสและดีไรฟคลาส int X, Y, Color; public: Draw(int InitX, int InitY, int InitColor) //construture { X=InitX; Y=InitY; Color=InitColor; } ~Draw() //destructure function การเขียนโปรแกรมแบบ OOP { closegraph(); //close graphic mode } ! ศิริชัย นามบุรี
! 177 }; class Point : public Draw //derived class { public: Point(int InitX,int InitY,int InitColor):Draw(InitX,InitY,InitColor){} void Display(); void Hide(); }; void main() { /* request auto detection graphic display hardware*/ int gdriver = DETECT, gmode, errorcode; /* initialize graphics mode */ initgraph(&gdriver, &gmode, \"\"); //create object from derived class and inheritance attribute Point Apoint(320,240,WHITE); //สรางออบเจ็กตชื่อ Apoint Apoint.Display(); //function for display white point on screen getch(); Apoint.Hide(); //function for display background color point on screen Apoint.Display(); getch(); } void Point::Display() { putpixel(X,Y,Color); } void Point::Hide() { putpixel(X,Y,getbkcolor()); } ♦แ! บบฝกหัดทายบท ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
! 178 1. ใหนักศึกษาเขียนโปรแกรมแบบ OOP เพื่อคํานวณคาและแสดงผลลัพธพื้นที่ของรูปเรขาคณิต โดย ใหสรางเมมเบอรฟงกชันของคลาสเพื่อทาํ หนาที่คาํ นวณคาของพื้นที่และแสดงผล ดงั ตอ ไปน้ี - พื้นที่รูปสี่เหลี่ยม - พื้นที่รูปสามเหลี่ยม - พื้นที่วงกลม 2. ใหเ ขยี นโปรแกรมเพอ่ื จดั เกบ็ ขอ มลู ประวตั พิ นกั งานไมเ กนิ 100 คน มีรายละเอียดขอมูลไดแก รหัสพนักงาน, ชื่อพนักงาน และเงนิ เดอื น โดยกําหนดใหม ี member function สาํ คัญ ๆ ดงั น้ี - Input() ทาํ หนา ทร่ี บั ขอ มลู รหัสพนักงาน, ชอ่ื พนกั งานและเงนิ เดอื น - Cal_Net_Tax() ทาํ หนาที่คาํ นวณภาษีหัก ณ ทจ่ี า ยจากเงนิ เดอื น 7% และคาํ นวณเงนิ เดอื น คงเหลือสุทธิ - Cal_Total() ทาํ หนาที่คํานวณเงนิ เดอื นรวม, ภาษรี วม, เงินคงเหลือสุทธิรวมของพนักงาน ทุกคน - Report() ทาํ หนาที่แสดงรายงานที่มีรายละเอียดขอมูล รหัสพนักงาน, ชื่อพนักงาน, เงนิ เดอื น, ภาษีหัก ณ ที่จาย, เงินเดือนเหลอื สทุ ธขิ องพนักงานแตละคน และยอดสรปุ รวมเงนิ เดอื น, ภาษีหัก ณ ทจ่ี า ยรวม และเงนิ เดอื นสทุ ธริ วม 3. ใหเ ขยี นโปรแกรมคํานวณการตดั เกรดแบบองิ เกณฑ โดยใชว ธิ กี ารเขยี นโปรแกรมแบบ OOP ใหโ ปรแกรมมคี วามสามารถ ดงั ตอ ไปน้ี - รับขอมูลรหัสนักศึกษา, ชื่อนักศึกษา, คะแนนระหวางภาค, คะแนนปลายภาค - คํานวณการตดั เกรดตามเกณฑ คะแนนดงั น้ี 0-49 เกรด F 50-59 เกรด D 60-69 เกรด C 70-79 เกรด B 80-100 เกรด A - แสดงรายละเอียดที่กรอกทั้งหมด พรอ มแสดงคะแนนรวมและเกรดทไ่ี ด ขอ กาํ หนด ใหโปรแกรมสามารถเก็บขอมูลนักศึกษาพรอมกันไดไมเกิน 100 คน ! ศิริชยั นามบุรี การเขียนโปรแกรมแบบ OOP
บทที่ 12 การจดั การขอมูลแบบไฟล ❐ ความหมายของไฟล ไฟล( File) หมายถึง โครงสรา งของกลมุ ขอ มลู ทจ่ี ดั เกบ็ ในรปู แบบทค่ี อมพวิ เตอรส ามารถนํามา ใชงานได ไฟลอ าจถกู จัดเกบ็ อยใู นหนวยความจําหรอื สอ่ื บนั ทกึ ขอ มลู เชน แผนดิสก ฮารด ดสิ ก หรอื CD-ROM ซึ่งสามารถนําขอมูลในไฟลที่บันทึกในสื่อดังกลาวกลับมาใชใหมไดอีก นอกจากนั้นขอมูลใน ไฟลส ามารถสงออกไปแสดงผลทจ่ี อภาพ หรอื เครอ่ื งพมิ พ โดยอาศยั การจดั การดว ยโปรแกรมทม่ี คี าํ สั่ง เกี่ยวกับการจัดการไฟลโดยเฉพาะ ❐ ชนิดของไฟลใน C++ 1. Text File หมายถึง ไฟลที่ประกอบดวยตัวอักษร เครอ่ื งหมายตา ง ๆ ที่ใชในขอความ มีการ กําหนดรหสั การจบแตล ะบรรทดั ดว ยรหสั แปน enter หรอื รหสั การขน้ึ บรรทดั ใหม( carriage return) และมี เครื่องหมายจบไฟล (end of file maker) โดยทั่วไป Text File จะเปน ไฟลท ส่ี รา งขน้ึ จากโปรแกรม ประเภท Text Editor หรอื Word Processor เชน ไฟลชนิด .CPP ก็สรางจาก Text Editor ของ C++ ดงั นน้ั จงึ สามารถ Text File ไปเปด ในโปแกรมประเภท Word Processor ได ใชคาํ สั่ง Type ของ DOS ดูรายละเอียดในไฟลได 2. Binary File หมายถึง ไฟลทป่ี ระกอบดว ยของมลู ประเภทโครงสรา ง (Structure) หรอื มกี ารบบี ลดขนาด (Compress) ซึ่งทาํ ใหขอมูลมีลักษณะแตกตางจากขอมูลใน Text File การสรา ง Binary File จะสรางจากโปรแกรมสาํ หรบั ไฟลชนดิ นน้ั โดยเฉพาะ เพื่อความสะดวกในการอาน และเขียนไฟล เชน ไฟลใ นโปรแกรม dBASE, Lotus เปน ตน ❐ การดําเนนิ การเกย่ี วกบั ไฟล ใน C++ มีคลาสที่ทาํ หนาที่เกี่ยวกับการรับขอมูลและการนาํ ขอ มลู ออกจากไฟล (File I/O) อยู 3 คลาส ไดแก - คลาส ifstream ทาํ หนาที่รับขอมูลจากไฟลเขามาในหนวยความจํา - คลาส ofstream ทาํ หนา ทน่ี าํ ขอมูลจากหนวยความจําสงออกไปที่ไฟล - คลาส fstream ทาํ หนาที่ทั้งรับขอมูลจากไฟลเขามาในหนวยความจาํ และนําขอมูลจากหนวย ความจําออกไปที่ไฟล ศิริชยั นามบรุ ี การจดั การขอ มลู แบบไฟล
180 ทั้ง 3 คลาส คือ ifstream, ofstream, stream เก็บอยูในไฟล fstream.h ดงั นน้ั โปรแกรมทม่ี กี าร ดาํ เนินการเกี่ยวกับขอมูลประเภทไฟล จะตองมีการ include เฮดเตอรไ ฟล ที่ชื่อ fstream.h ดว ยเสมอ ในการดาํ เนินการเกี่ยวกับไฟล ทั้ง Text File และ Binary File มกี ิจกรรมหลกั ๆ ทต่ี อ งดาํ เนนิ การ คือ การบันทึกขอมูลจากหนวยความจําเขาเก็บในไฟล และการอานขอมูลจากไฟลมาไวในหนวยความจาํ ❐ วิธีดาํ เนนิ การกบั ไฟลข อ มลู สตรงิ (Text File) 1. การบันทึกสตริงเขาไฟลในดิสก ทาํ ไดโดยการสรางออบเจ็กตใหเปนสมาชิกของคลาส ofstream และกาํ หนดชื่อไฟล ตามรปู แบบตวั อยา ง ofstream WriteTextFile(“A:INFO.TXT”); class object ชื่อไดรฟและชอื่ ไฟล จากรปู แบบ ofstream คือชื่อคลาส WriteTextFile คอื ชอ่ื ออบเจก็ ตท ส่ี รา งขน้ึ เพอ่ื ทาํ หนาที่บันทึกไฟลและในวงเล็บ (“test1.txt”) คือชื่อไฟล ซึ่งเปน Text File ที่จะใชบันทึกขอความ ถา ไมร ะบไุ ดเรกทอรจ่ี ะบันทกึ ลงในไดเรกทอรปี จจุบนั เราสามารถเขียนออกเจก็ ตสาํ หรับทาํ หนาที่ บันทึกสตริงเขาไฟลในลักษณะอื่นๆ ได เชน ofstream SaveTextFile(“A:Test.txt”); ofstream write_text_file(“A:\\DATA\\test.dat”); วิธีการบันทึกสตริงเขาเก็บไวในไฟล เขยี นไดด งั น้ี WriteTextFile<<“Hello, I love C++ and Computer\\n”; หมายถึงใหบันทึกขอความ Hello, I love C++ and Computer ไวในไฟล data.txt เปน ตน ตัวอยางโปรแกรม TEXTFILE.CPP แสดงการบันทึกขอความเขาเก็บไวในไฟล ชื่อ INFO.TXT ในไดเรกทอรี A: การแสดงผลลพั ธข องโปรแกรมจะทําการบันทกึ ขอความที่กาํ หนดไวในไฟล INFO.TXT ซึ่งเราสามารถดูเนื้อหาในไฟลได โดยการใชคําสั่ง ใน DOS prompt A:\\>TYPE INFO.TXT [กด enter] หรอื เปดไฟลโดยใช Editor ของ C++ โดยใชคําสั่ง File / Open พิมพชื่อไฟล A:INFO.TXT แลว OK หรอื enter ศิริชยั นามบรุ ี การจดั การขอ มลู แบบไฟล
181 /*Program: TextFile.CPP Process: write text line to text file A:INFO.TXT */ #include <iostream.h> #include <fstream.h> void main() { //create WriteTextFile object from ofstream class ofstream WriteTextFile(\"A:INFO.TXT\"); WriteTextFile<<\" Hello, How are you today? \\n\"; WriteTextFile<<\" How do you feel about C++? \\n\"; WriteTextFile<<\" I hope that you will fall in love it.\\n\"; WriteTextFile<<\" Are you O.K. \"; } ขอ สงั เกต การใชคลาส ofstream สรา งออบเจก็ ต เพื่อบันทึกสตริงก ออปเจก็ ตจ ะทาํ การเปด บนั ทึกขอ มลู แลวปด ไฟลใ หโดยอัตโนมตั ิ 2. การอานสตริงจากไฟลในดิสก ทาํ ไดโดยการ 2.1 ตอ งสรา งออบเจก็ ตใ หเ ปน สมาชกิ ของคลาส ifstream และกาํ หนดชื่อไฟลที่ตองการ อา นขอ มลู ตามรปู แบบดงั น้ี ifstream ReadTextFile(“A:INFO.TXT”); class object ช่ือไดรฟแ ละชอื่ ไฟล หมายถึง สรา งออบเจก็ ตช อ่ื ReadTextFile เปนสมาชิกของคลาสifstream เพอ่ื ดาํ เนนิ การอา นสตรงิ จากไฟลช อ่ื ชอ่ื Test1.txt จากไดเรกทอรป่ี จ จบุ นั 2.2 ใชฟงกชัน getline() ที่เปนสมาชิกของคลาส istream ซึ่งเปนดีไรฟคลาสของ ifstream ฟงกชัน getline() ทาํ หนา ท่อี า นอกั ษรจากไฟลม าเกบ็ ไวใ นตวั แปรประเภท charทีละบรรทัด (‘\\n’) จาํ นวนสูงสุดตามที่กาํ หนด ตามรปู แบบดงั น้ี ReadTextFile.getline(ALine,Maxchar); object function argument หมายถึง ออบเจก็ ต ReadTextFile มีการอานสตริงจากไฟล Test1.txt ดว ย ฟงกชัน getline() อานมาทีละบรรทัดมาเก็บไวที่ตัวแปร ALine จํานวนที่เก็บไดสูงสุดในแตละบรรทัดเทา กับ Maxchar ศิริชยั นามบรุ ี การจดั การขอ มลู แบบไฟล
182 •! ตัวอยางโปแกรม READTXT1.CPP ตอไปนี้ ทาํ หนาที่ในการอานขอมูลจาก Text File ชื่อ INFO.TXT ขน้ึ มาทลี ะบรรทดั แตละบรรทัดไมเกิน 80 ตัวอักษร จนกวาจะจบไฟลหรอื จน กวาจะอานขอมูลไมไดอีกแลว /*Program: TXTREAD1.CPP Process: read text file to string of character or to line */ #include <fstream.h> #include <iostream.h> #include <conio.h> void main() { char text[80]; clrscr(); //ReadTextFile() is object that user create from ofstream class ifstream ReadTextFile(\"A:INFO.TXT\"); while(ReadTextFile) { ReadTextFile.getline(text,80); cout<<text<<endl; } cout<<\"this is text that read from A:INFO.TXT file\\a\"; getch(); } หมายเหตุ while(ReadTextFile) เปน คาํ สั่งแบบลูป(Looping) ทาํ หนาที่อานขอมูลจนจบไฟล โดยฟงกชัน getline() จะอา นขอ มลู 1 บรรทดั มาเกบ็ ไวท ต่ี วั แปร text แลวแสดงออกไปที่จอภาพดวยคาํ สั่ง cout<< ตามเงอ่ื นไข ตรวจสอบการจบไฟลด ว ยออบเจก็ ต ReadTextFile มีการทาํ งานดงั น้ี - ถาจบไฟล ReadTextFile จะใหคาเปน 0 - ถายังไมจบไฟล ReadTextFile จะใหคาที่ไมใช 0 ศิริชัย นามบรุ ี การจดั การขอ มลู แบบไฟล
183 ❐ วิธีดาํ เนนิ การกบั ไฟลอกั ษร 1. การบันทึกตัวอักษรเขาไฟลในดิสก ทาํ ไดโดยใชฟงชชัน put() ซึ่งเปนฟงกชันในคลาส ostream ใชสาํ หรับบันทึกอักษรเขาไฟลครั้งละ 1 ตวั อกั ษร มีรูปแบบของฟงกชัน put()ดงั น้ี Parameters ofstream& put(char ch); class function ฟงกชัน put() จะทาํ หนาที่สง ch ไปบันทึกในไฟลที่กาํ หนด ตัวอยางโปรแกรม CharFil1.CPP ใชฟงกชัน put() ทาํ การบันทึกตัวอักษรจากขอความครั้งละ 1 ตัวอักษรบันทึกลงในไฟล A:INFO2.TXT /*Program: CharFil1.CPP Process: write character to file */ #include <fstream.h> #include <string.h> void main() { char Str[]=\"I love to learn C++ Language\"; ofstream WriteTextFile(\"A:INFO2.TXT\"); for(int n=0;n<strlen(Str);n++) WriteTextFile.put(Str[n]); } ศิริชัย นามบรุ ี การจดั การขอ มลู แบบไฟล
184 •! ตัวอยางโปรแกรม WriteCha.CPP ทําหนา ทร่ี บั ขอ มลู ครง้ั ละตวั อกั ษรและเขยี น ลงไฟลA:INFO3 จนกวาจะกดแปน Enter จากนั้นอานขอมูลจากไฟล /*Program: WriteCha.CPP Process: write for each character to text file by put() function */ #include <fstream.h> #include <iostream.h> #include <conio.h> #include <string.h> void main() { char ch; clrscr(); ofstream WriteTextFile(\"A:INFO3.TXT\"); cout<<\"**** Please key character ....press enter to stop ****\"; cout<<endl; while(ch!=13) { ch=getche(); //input 1 character to ch WriteTextFile.put(ch); //write to file } WriteTextFile.close(); clrscr(); //ReadTextLine() is object that user create from ifstream char chr; ifstream ReadTextLine(\"A:INFO3.TXT\"); while(ReadTextLine); { ReadTextLine.get(chr); cout<<chr; } cout<<endl<<endl<<\"....this is text that read from INFO3.TXT getch(); } ศิริชัย นามบรุ ี การจดั การขอ มลู แบบไฟล
185 ❐ วิธีดาํ เนนิ การกบั ไบนารไี ฟล (Binary File) Binary File เปนไฟลที่ประกอบดวยของมูลประเภทโครงสราง (Structure) หรือมีการบีบลด ขนาด (Compress) ไฟลประเภทนี้จะใชกับขอมูลชนิด structure มีการจัดการ ดงั น้ี 1. การเขียนขอมูลลงไฟลในดิสก ใชฟงกชัน write() ซึ่งเปนฟงกชันในคลาส ostream ฟงกชัน write() มรี ปู แบบดงั น้ี ostream& write(const char* , int n); ฟงกชัน write() จะนําตวั อกั ษรจากพารามเิ ตอรต วั ท่ี 1 จํานวน n ตัวอักษาไปเขียนไฟลใน ไฟล โดยในจาํ นวน n ตวั นร้ี วมอกั ษร Null ไวแลว 2. การอานขอมูลจากไฟล ทาํ ไดโดยใชฟงกชัน read() ซึ่งเปนฟงกชันในคลาส istream มรี ปู แบบดงั น้ี istream& read(const char* , int n); ฟงกชัน read() จะอา นอกั ษรจากไฟลมาเกบ็ ไวทพ่ี ารามเิ ตอรตัวท่ี 1 โดยมจี ํานวน อักษรตามที่กําหนดไวใ นพารามเิ ตอรต วั ท่ี 2 •! ตวั อยา งโปรแกรม STD_FILE.CPP มีการสราง class และขอ มลู ชนดิ structure เพื่อนําไปบันทึกในไฟลขอมูลในไดรฟ A: ชื่อ student.dat โดย บันทึกขอมูลไดเพียงครั้งละ 1 ชดุ หรอื 1 เรคอรด (record) /*Program: STD_FILE.CPP Process:Create Class Create Binary File Read Binary File Create Menu() Function */ #include <fstream.h> #include <iostream.h> #include <conio.h> #include <string.h> //prototype function void Menu(); void Create(); ศิริชัย นามบรุ ี การจดั การขอ มลู แบบไฟล
186 void Display(); //creat class class STD { protected: // data member char code[9]; char name[40]; int age; public: void InputData() //member function { cout<<\"Enter Code : \";cin>>code; cout<<\"Enter Name : \";cin>>name; cout<<\"Enter Age : \";cin>>age; } void ShowData() //memdber function { cout<<\"Enter Code : \"<<code<<endl; cout<<\"Enter Name : \"<<name<<endl; cout<<\"Enter Age : \"<<age<<endl; getch(); } }; STD Student; //create Object student from STD class //begin main function void main() { Menu(); } void Menu() //user defined function { char choice=' '; while(choice!='3') ศิริชัย นามบรุ ี การจดั การขอ มลู แบบไฟล
187 การจดั การขอ มลู แบบไฟล { clrscr(); gotoxy(25,2);cout<<\"Main Menu Student Data\"; gotoxy(30,4);cout<<\" 1. Create File and Input Data\"; gotoxy(30,5);cout<<\" 2. Displa Data File \"; gotoxy(30,6);cout<<\" 3. Exit \"; gotoxy(30,8);cout<<\"Select choice : \";choice=getch(); switch(choice) { case '1': Create(); break; case '2': Display(); break; case '3': break; } } } void Create()//user defined function { clrscr(); Student.InputData(); ofstream WriteFile(\"A:student.dat\"); //create file WriteFile.write((char*)&Student,sizeof(Student)); //write data to file } void Display()//user defined function { clrscr(); ifstream ReadFile(\"A:student.dat\"); //open file ReadFile.read((char*)&Student,sizeof(Student)); //read data from file Student.ShowData(); ReadFile.close(); } ศิริชัย นามบรุ ี
188 ❐ วิธีดาํ เนนิ การกบั ไฟลพ อยนเ ตอร (File Pointer) เน่อื งจากการจัดการกับขอ มูลของไฟลประเภท Binary หลาย ๆ ออบเจก็ ตห รอื หลาย ๆ เรคอรด ไมสามารถทําไดหากใชเฉพาะฟงกชัน write() หรอื read() เพียงอยางเดียว แตจ ะตอ งใช File Pointer ใน การจัดการ โดยใชฟงกชัน open() เปด ไฟลใ นโหมดตา ง ๆ ซึ่งมีรูปแบบดังนี้ void open(const char* name, int mode, int prot =filebuff::openprot); โดยท่ี name คือ ชอื่ ไฟลท่ตี องการเปด mode คือ โหมดการเปด เพอ่ื วตั ถปุ ระสงคต า ง ๆ ดงั ตารางดงั น้ี โหมด ความหมาย in เปด ไฟลเ พอ่ื อา นขอ มลู out เปดไฟลเพื่อบันทึกขอมูล app เร่มิ อา นหรอื บันทึกไฟลตอนทา ยไฟล ate ลบไฟลกอนที่จะอานหรือเซฟไฟล nocreate จะเกิดความผิดพลาถาเปดไฟลที่ไมมีอยู noreplace จะเกิดความผิดพลาดถาเปดไฟลที่มีอยูเพื่อเซฟขอมูล binary เปดไฟลไบนารี โดยท่ี โหมดเหลา นส้ี ามารถใชร ว มกนั ไดโ ดยใชเ ครอ่ื งหมาย | คน่ั ระหวา งแต ละโหมด เชน open(“TEST.DAT”,ios::in|ios::out); prot คือ วธิ กี ารตดิ ตอ กบั DOS ซึ่งตามปกติถาไมกําหนด ใหต ดิ ตอ กบั DOS เพ่ือบนั ทึกหรืออานขอ มลู จากไฟล การสรา งไฟลแ ละการอา นไฟลพ อยนเ ตอร มขี น้ั ตอนสาํ คัญ ดังนี้ 1. สรางคลาสหนดขอมูลและฟงกชัน สรางคลาสเพื่อกาํ หนดขอ มลู ชนดิ structure ภายในคลาส ศิริชยั นามบรุ ี การจดั การขอ มลู แบบไฟล
189 พรอ มทง้ั สรา ง Member Function ตา ง ๆ ทต่ี อ งการในโปรแกรม เชน class Person { protect: char Name[50] int Age; public: void GetData() //ฟง กช นั ทําหนา ทร่ี บั ขอ มลู { cout<< “Enter Name : “; cin>>Name; cout<< “Enter Age : “; cin>>Age; } void ShowData() //ฟง กช นั ทําหนา ทแ่ี สดงขอ มลู { cout<<”Name : “<<Name<<endl; cout<<”Name : “<<Age<<endl; } }; 2. สรา งออบเจก็ ตท เ่ี ปน record ขอมูลจากคลาสที่สรางไวในขอ 1 Person Student; //Person คือ คลาส และ Student คอื ออบเจก็ ต 3. การสรา งออบเจก็ ตป ระเภทไฟล คือ การสรา งออบเจก็ ตป ระเภทไฟลจากคลาส fstream fstream File; //fsteram คือ คลาส และ File คือออบเจ็กตที่กําหนดขน้ึ 2. การเปด ไฟลใ นโหมดตา ง ๆ ดวยฟงกชัน open() โดยกําหนดโหมดตามความตอ งการ ดงั ตวั อยาง File.open(“A:TEST.DAT”,ios::app|ios::out|ios::in); 3. การเขียนหรือบันทึกขอมูลลงไฟล ดวยฟงกชัน write() ดงั น้ี File.write((char*)&Student,sizeof(Student)); 4. การอานขอมูลจากไฟล ดวยฟงกชัน read() ดงั น้ี File.read((char*)&Student,sizeof(Student)); 5. การกาํ หนดตําแหนงของขอมูลในไฟล ดวยฟงกชัน tellg(), seekg(), tellp(), seekp() การจัดการขอมูลในไฟล ณ ตาํ แหนง ตา ง ๆ ที่กาํ หนดของไฟล เรยี กวา ไฟลพอยนเ ตอร ตองใชฟงกชันตาง ๆ ทีเ่ กย่ี วขอ งกบั การระบุตําแหนงขอมูลในไฟล มรี ปู แบบ ดงั น้ี ศิริชัย นามบรุ ี การจดั การขอ มลู แบบไฟล
190 long tellg(); ใหค า ตําแหนง ของไฟลพ อยนเ ตอรซ ง่ึ เปน ตําแหนง อา นขอ มลู istream& seekg(sreampos pos) ใหเ ลอ่ื นไฟลพ อยนเ ตอรไ ปยงั ตําแหนง ทต่ี อ งการอา น ขอ มลู เชน File.seekg(0) หมายถึงเลื่อนไปยังตําแหนง เรคอรด หมายเลข 0 ซง่ึ เปน ตําแหนงแรกของไฟล ซง่ึ เรม่ิ จาก 0, 1, 2, ... streampos tellp(); ใหค า ตําแหนง ของไฟลพ อยนเ ตอรซ ง่ึ เปน ตําแหนง บันทกึ ขอ มลู ostream& seekp(streampos); จะเลอ่ื นไปทต่ี าํ แหนง ของไฟลพ อยนเ ตอรซ ง่ึ เปน ตาํ แหนงบนั ทึกขอ มลู ตวั อยา งโปรแกรม File_poin แสดงการใช File pointer จัดเก็บขอมูลพรอ มกันไดหลายเรคอรด (Record) บันทึกขอมูล, อานขอมูลและคนหาขอมูลจากไฟล /*Program: FIL_POIN.CPP Process:Create Class Create Binary File by pointer Create Menu() Function Write,Read, Search Record */ #include <fstream.h> #include <iostream.h> #include <conio.h> #include <string.h> #include <ctype.h> //prototype function void Menu(); void InputData(); void ReportData(); void SearchByCode(); //creat class class STD { public: // data member char code[9]; char name[40]; int age; public: void InputData() //member function ศิริชยั นามบรุ ี การจดั การขอ มลู แบบไฟล
191 { cout<<\"\\nEnter Code : \";cin>>code; cout<<\"Enter Name : \";cin>>name; cout<<\"Enter Age : \";cin>>age; } void ShowData() //memdber function { cout<<\"Code : \"<<code<<endl; cout<<\"Name : \"<<name<<endl; cout<<\"Age : \"<<age<<endl; } }; //global variable STD Student; //create Object student from STD class fstream File; //creat Object File from fstream class //begin main function void main() { Menu(); } void Menu() { char choice=' '; while(choice!='4') { clrscr(); gotoxy(25,2);cout<<\"Main Menu Student Data <<A:Student.dat>>\"; gotoxy(30,4);cout<<\"1. Appen Data \"; gotoxy(30,5);cout<<\"2. Report Data \"; gotoxy(30,6);cout<<\"3. Search Data from File \"; // gotoxy(30,7);cout<<\"4. Search Data from File \"; gotoxy(30,7);cout<<\"4. Exit \"; gotoxy(30,10);cout<<\"Select choice : \";choice=getch(); ศิริชัย นามบรุ ี การจดั การขอ มลู แบบไฟล
192 switch(choice) { case '1': InputData(); break; case '2': ReportData(); break; case '3': SearchByCode(); case '4': break; } } } void InputData() { char ans=' '; File.open(\"A:student.dat\",ios::app|ios::out|ios::in); clrscr(); cout<<\" Enter information of student: \"<<endl; while(toupper(ans)!='N') { Student.InputData(); File.write((char*)&Student,sizeof(Student)); cout<<\"Append data any more? <y/n> ? \"; ans=getch();cout<<endl; } File.close(); } void ReportData() { int all_rec=0,rec_no=1; //skip pointer of record at record #0 clrscr(); ศิริชัย นามบรุ ี การจดั การขอ มลู แบบไฟล
193 การจดั การขอ มลู แบบไฟล File.open(\"A:student.dat\",ios::app|ios::out|ios::in); File.seekg(0,ios::end); all_rec=File.tellg()/sizeof(Student); File.seekg(0,ios::beg); cout<<\"Report all record form file ....\"<<endl; cout<<\"Total record : \"<<all_rec<<endl; File.read((char*)&Student,sizeof(Student)); //read first record while(!File.eof()) { cout<<\"record# \"<<rec_no<<endl; Student.ShowData(); File.read((char*)&Student,sizeof(Student)); //read next record cout<<\"press any key...\"<<endl<<endl; getch();rec_no++; } cout<<\"end of file\\a\";getch(); File.close(); } void SearchByCode() { char co[9]; int recno=0; cout<<\"\\n\\nEnter code to search: \";cin>>co; File.open(\"A:student.dat\",ios::in); File.seekg(0,ios::beg); File.read((char*)&Student,sizeof(Student)); //read record search no# while(!File.eof()&&strcmp(co,Student.code)) { //File.seekg((i+1)*sizeof(Student)); File.read((char*)&Student,sizeof(Student)); //read record search no# } if((strcmp(co,Student.code)==0)) { cout<<\"\\nRecord found #: \"<<File.tellg()/sizeof(Student)<<endl; Student.ShowData(); } else cout<<\"Record not found!!!\\a\\a\"; ศิริชยั นามบรุ ี
194 cout<<\"\\npress any key to continue...\";getch(); File.close(); } ❐ แบบฝกหัดทายบท 1. ใหเ ขยี นโปรแกรมเพอ่ื เกบ็ ประวตั พิ นกั งานของบรษิ ทั แหง หนง่ึ รายละเอียดขอมูลที่จัดเก็บไดแก - รหัสพนักงาน - ชื่อสกุล - ที่อยู - อายุ - เงนิ เดอื น - ตาํ แหนง จากรายละเอียดของขอมูลใหเขียนโปรแกรมโดยใชขอมูลแบบไฟล ใหโ ปรแกรมมฟี งกช ันตา ง ๆ ในโปรแกรมทาํ งานตามหนาที่ ดงั น้ี - บันทึกประวัติพนักงาน - แสดงรายงานรายละเอียดประวัติของพนักงานทุกคนได - แสดงรายงานเงินเดือนพนักงานแตละคนและ รายละเอยี ดการหักภาษี ณ ที่จาย 3% ของ พนักงาน พรอ มยอดสรปุ คา เฉลย่ี เงนิ เดอื นของพนกั งาน - คน หาประวตั พิ นกั งานเปน รายบคุ ล โดยการกรอกรหสั พนกั งานเพอ่ื คน หาได 2. ใหน กั ศึกษาเขยี นโปรแกรมเพื่อคาํ นวณการตดั เกรดนกั ศกึ ษา โดยจดั เก็บขอ มูลเปนชนิดไฟลขอมูล กาํ หนดเงอ่ื นไขการตดั เกรด ดงั น้ี คะแนนระหวา ง 1-49 เกรด F คะแนนระหวา ง 50-59 เกรด D คะแนนระหวา ง 60-69 เกรด C คะแนนระหวา ง 70-79 เกรด B คะแนนระหวา ง 80-100 เกรด A โปรแกรมมคี วามสามารถดงั น้ี - บันทึกรายละเอียด รหสั , ชื่อนักศึกษา, คะแนนระหวา งภาค, คะแนนปลายภาค ลงไฟลขอมูล ศิริชัย นามบรุ ี การจดั การขอ มลู แบบไฟล
195 - รวมคะแนนจากแฟม ขอ มลู เพอ่ื นําไปคาํ นวณตดั เกรด - คํานวณการตดั เกรดตามเกณฑท ก่ี าํ หนดไว - คาํ นวณจํานวนคนทไ่ี ดร บั เกรดแตล ะเกรด - แสดงรายงานผลรายละเอยี ดขอ มลู การตดั เกรดทง้ั หมด พรอ มการสรปุ จํานวนผไู ดร ับในแตล ะ เกรด โดยทก่ี ารเขยี นโปรแกรมจะตอ งสรา งเปน ฟง กช นั แบง ตามหนา ทก่ี ารทํางานในโปรแกรมใหเ หมาะ สม ศิริชัย นามบรุ ี การจดั การขอ มลู แบบไฟล
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197