บทที่ 10 introduction to oop - home.kku.ac.th · pdf fileบทที่ 11...

26
Introduction to Object Oriented Programming (OOP) บทที11 Introduction to OOP 11.1 ความเปนมาของเทคนิคการเขียนโปรแกรม เมื่อมีการพัฒนาเครื่องคอมพิวเตอรขึ้นในชวงประมาณป 1950 โปรแกรมเมอรในยุคแรกสุด ใช วิธีการเขียนโปรแกรมโดยการโยกสวิตชไฟฟาที่มีอยูจํานวนมากที่แผงวงจรไฟฟา เพื่อสลับไป มา ระหวางการปดและการเปด เพื่อใหเกิดรหัสคําสั่ง 0 และ 1 โปรแกรมเมอรตองใชเวลา ความ รอบคอบ และความอดทนเปนอยางยิ่ง ตอมาไดมีการคิดคนพัฒนาวิธีการเขียนโปรแกรมใหสะดวก และรวดเร็วขึ้น จึงทําใหเกิดภาษาระดับตํ่าขึ้น คือ ภาษาแอสเซมบลี(Assembly) ในเวลาตอมาก็เกิด ภาษาระดับสูง ภาษาแรกขึ้นคือ ฟอรแทน (Fortran) ภาษาระดับสูง หมายถึงภาษาที่มีการกําหนด ชนิดขอมูล มีการควบคุมลําดับการทํางานอยางเปน ระบบ เขียนรหัส ดวยภาษาอังกฤษ มีการใช รูปแบบคําสั่ง ที่ใกลเคียงกับภาษาที่ใชในการสื่อสาร ทั่วไป ภาษาระดับสูงจึงทําใหงานเขียน โปรแกรมของโปรแกรมเมอรงายขึ้น คนทั่วไปก็สามารถศึกษา การเขียนโปรแกรมนี้ได ปญหาที่เกิดขึ้นหลังจากนี้คือ การเขียนโปรแกรมที่มีความซับซอนและมีความยาวมาก ภาษาคอมพิวเตอรที่มีอยูเมื่อนํามาเขียนโปรแกรมทําใหเกิดความสับสนและวกวน (spaghetti code) ซึ่งมีผล ทําใหการติดตามแกไขและปรับปรุงโปรแกรมยุงยากใชเวลามากเกินไปเชนการเขียน โปรแกรมดวย ภาษา BASIC เปนตน จึงทําใหมีการคิดคนวิธีการแกปญหาดังกลาว ดวยเหตุนี้จึงทํา ใหเกิดภาษา คอมพิวเตอรที่ใชเทคนิคการเขียนที่เรียกวา Structured Programming โดยวิธีการนีผูเขียนจะสนใจวา โปรแกรมทํางานอะไรมากกวาการที่จะสนใจวาโปรแกรมนั้นทํางานอยางไร หมายถึงวาตองคํานึงถึง กอนวาในโปรแกรมมีการทํางานตางๆ อะไรบาง มีการจัดแบงการทํางาน ของโปรแกรมเปนสวน ตาม ประเภทของงาน จึงทําใหผูเขียนโปรแกรมสามารถแบงงานกันทํา สามารถนํางานยอย ที่มีอยูแลวไป ใชในโปรแกรมอื่น อีกโดยไมตองสรางใหม เชน การเขียน โปรแกรมดวยภาษาระดับสูงตาง ไดแก PASCAL, COBOL, TURBO, BASIC, C เปนตน ในปจจุบัน ถึงแมวาวิธีการของ Structure Programming จะเพิ่มประสิทธิภาพของการเขียน โปรแกรมไดดีขึ้น แตตลอดเวลาที่ผานมามีการพัฒนาขีดความสามารถของฮารดแวรคอมพิวเตอร อยาง ไมมีขอจํากัด จึงทําใหการเขียนโปรแกรมหรือซอฟตแวรตองมีการพัฒนาควบคูไปดวย ลักษณะของ โปรแกรมสมัยปจจุบัน มีขนาดยาวมาก เปนลานๆบรรทัดมีความซับซอนยุงยากตองใช ทีมงานนัก โปรแกรมเมอรในการพัฒนาซอฟตแวรเปนจํานวนมาก จึงทําใหมีการคิดคนวิธีการ เขียนโปรแกรมแบบ ใหม ที่มีประสิทธิภาพยิ่งขึ้น เหมาะกับการพัฒนาซอฟตแวรขนาดใหญ วิธี ดังกลาวไดแก วิธีที่เรียกวาการโปรแกรมแบบกําหนดวัตถุเปาหมาย หรือโอโอพี OOP (Object Oriented Programming) ชวยลดความซํ้าซอนของโปรแกรมไดอยางมีประสิทธิภาพหากมีขนาด ตั้งแต 100,000 บรรทัดขึ้นไป 322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 1

Upload: buikhuong

Post on 28-Feb-2018

242 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

บทที่ 11 Introduction to OOP

11.1 ความเปนมาของเทคนคิการเขียนโปรแกรม เม่ือมีการพัฒนาเคร่ืองคอมพิวเตอรข้ึนในชวงประมาณป 1950 โปรแกรมเมอรในยุคแรกสุดใช วิธีการเขียนโปรแกรมโดยการโยกสวิตชไฟฟาท่ีมีอยูจํานวนมากท่ีแผงวงจรไฟฟา เพ่ือสลับไปมา ระหวางการปดและการเปด เพื่อใหเกิดรหัสคําส่ัง 0 และ 1 โปรแกรมเมอรตองใชเวลา ความรอบคอบ และความอดทนเปนอยางยิ่ง ตอมาไดมีการคิดคนพัฒนาวิธีการเขียนโปรแกรมใหสะดวกและรวดเร็วข้ึน จึงทําใหเกิดภาษาระดับต่ําข้ึน คือ ภาษาแอสเซมบล้ี (Assembly) ในเวลาตอมาก็เกิดภาษาระดับสูง ภาษาแรกข้ึนคือ ฟอรแทน (Fortran) ภาษาระดับสูง หมายถึงภาษาที่มีการกําหนดชนิดขอมูล มีการควบคุมลําดับการทํางานอยางเปน ระบบ เขียนรหัส ดวยภาษาอังกฤษ มีการใชรูปแบบคําส่ัง ท่ีใกลเคียงกับภาษาที่ใชในการส่ือสาร ท่ัวไป ภาษาระดับสูงจึงทําใหงานเขียนโปรแกรมของโปรแกรมเมอรงายข้ึน คนท่ัวไปก็สามารถศึกษา การเขียนโปรแกรมนี้ได

ปญหาท่ีเกิดข้ึนหลังจากนี้คือ การเขียนโปรแกรมท่ีมีความซับซอนและมีความยาวมาก ภาษาคอมพิวเตอรท่ีมีอยูเม่ือนํามาเขียนโปรแกรมทําใหเกิดความสับสนและวกวน (spaghetti code) ซ่ึงมีผล ทําใหการติดตามแกไขและปรับปรุงโปรแกรมยุงยากใชเวลามากเกินไปเชนการเขียนโปรแกรมดวย ภาษา BASIC เปนตน จึงทําใหมีการคิดคนวิธีการแกปญหาดังกลาว ดวยเหตุนี้จึงทําใหเกิดภาษา คอมพิวเตอรที่ใชเทคนิคการเขียนท่ีเรียกวา Structured Programming โดยวิธีการนี้ ผูเขียนจะสนใจวา โปรแกรมทํางานอะไรมากกวาการท่ีจะสนใจวาโปรแกรมนั้นทํางานอยางไร หมายถึงวาตองคํานึงถึง กอนวาในโปรแกรมมีการทํางานตางๆ อะไรบาง มีการจัดแบงการทํางานของโปรแกรมเปนสวน ๆ ตาม ประเภทของงาน จึงทําใหผูเขียนโปรแกรมสามารถแบงงานกันทํา สามารถนํางานยอย ๆ ท่ีมีอยูแลวไป ใชในโปรแกรมอื่น ๆ อีกโดยไมตองสรางใหม เชน การเขียนโปรแกรมดวยภาษาระดับสูงตาง ๆ ไดแก PASCAL, COBOL, TURBO, BASIC, C เปนตน

ในปจจุบัน ถึงแมวาวิธีการของ Structure Programming จะเพิ่มประสิทธิภาพของการเขียน โปรแกรมไดดีข้ึน แตตลอดเวลาท่ีผานมามีการพัฒนาขีดความสามารถของฮารดแวรคอมพิวเตอรอยาง ไมมีขอจํากัด จึงทําใหการเขียนโปรแกรมหรือซอฟตแวรตองมีการพัฒนาควบคูไปดวย ลักษณะของ โปรแกรมสมัยปจจุบัน มีขนาดยาวมาก เปนลานๆบรรทัดมีความซับซอนยุงยากตองใชทีมงานนัก โปรแกรมเมอรในการพัฒนาซอฟตแวรเปนจํานวนมาก จึงทําใหมีการคิดคนวิธีการเขียนโปรแกรมแบบ ใหม ท่ีมีประสิทธิภาพยิ่งข้ึน เหมาะกับการพัฒนาซอฟตแวรขนาดใหญ วิธีดังกลาวไดแก วิธีท่ีเรียกวาการโปรแกรมแบบกําหนดวัตถุเปาหมาย หรือโอโอพี OOP (Object Oriented Programming) ชวยลดความซ้ําซอนของโปรแกรมไดอยางมีประสิทธิภาพหากมีขนาดตั้งแต 100,000 บรรทัดข้ึนไป

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 1

Page 2: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

11.2 ความหมายของ OOP OOP (Object Oriented Programming) เปนวิธีการเขียนโปรแกรมซ่ึงจัดดําเนินการกับกลุม

ของ ออบเจ็กต (Object) ท่ีมีอยูในโปรแกรม ออบเจ็กต เปนชนิดของขอมูลซ่ึงประกอบไปดวยกลุมของ ขอมูล(data) และกลุมของ

ฟงกชัน(Function) โดยการใชขอมูลและฟงกชันเหลานี้ แตละออบเจ็กตจะทํางานหนึ่งงานไดสมบูรณ (ท้ังนี้เนื่องจากตัวออบเจ็กตเองประกอบไปดวยขอมูลและฟงกชัน)

ออบเจ็กต เปนสมาชิกของ ตัวแปรคลาส (class variable) มีลักษณะเปนโมดูล (modularity) ซ่ึง ประกอบไปดวย ตัวแปร ชนิดตาง ๆ ท่ีสัมพันธกัน และประกอบดวย ฟงกชัน ตาง ๆ โดยที่ คลาส(class) จะหอหุมขอมูลและฟงกชันรวมไวดวยกันมีลักษณะท่ีเรียกวา encapsulation ดังนั้นจึงมีความสะดวกในการใชงาน สามารถปองกันสวนอ่ืน ๆ ของโปรแกรมไมใหเขาถึงตัวแปรชนิด โลคอลภายใน คลาสไดอยางดีเยี่ยม

ดังนั้นการเขียนโปรแกรมแบบ OOP คือ การสรางและ/หรือการเรียกใชออบเจ็กตใหทํางาน ตามท่ีเราตองการ ในการเรียกใชออบเจ็กตนั้น เราจะสนใจเฉพาะการทํางานของออบเจ็กตเทานั้น ไม จําเปนตองสนใจรายละเอียดภายในของออบเจ็กตวาเปนอยางไร การใชออบเจ็กตของโปรแกรมจะมีลักษณะคลายกับการใชส่ิงของในชีวิตประจําวันของเรา เชน การใชโทรทัศน เราสามารถใชไดโดยไมจําเปนตองรูวาภายในเคร่ืองโทรทัศนมี "สวนประกอบ" อะไร บาง และไมจําเปนตองรูวาแตละสวนประกอบทํางานอยางไร เราจะรูเพียงแควิธีใช เชน วิธีเปด วิธี เปล่ียนชอง วิธีปรับเสียง วิธีปรับสี วิธีตั้งเวลา วิธีปดเคร่ือง เปนตน ลักษณะของโปรแกรมแบบ OOP มีลักษณะคลายกับการใชโทรทัศนในชีวิตประจําวัน ซ่ึงจะไดศึกษาถึงวิธีการสรางและวิธีการใช OOP ตอไป 11.2.1 คุณสมบัติของ OOP

ลักษณะของ OOP มีคุณสมบัติสําคัญ 3 ประการ ดังน้ี 1. มีการรวมขอมูลเขากับฟงกชัน (encapsulation) เพื่อใหเปนขอมูลชนิดออบเจ็กต โดย

ออบเจ็กตหนึ่งจะมีคุณสมบัติเหมือนส่ิงของอยางหน่ึง คือมีท้ัง "สวนประกอบ" และ "การทํางาน" ซ่ึง สามารถใชงานไดทันที

2. มีการสืบทอด (inheritance) เปนคุณสมบัติตาง ๆ ของออบเจ็กตหนึ่งซ่ึงเปน บรรพบุรุษหรือ ตนตอ เรียกวา แอนเซสเตอร (ancestor) และไดสืบทอดคุณสมบัติตาง ๆ ท่ีมีอยูใหกับ ออบเจ็กตท่ีเปน ลูกหลานหรือผูสืบทอด เรียกวา ดีเซนแด็นต (descendant) ไดหลายออบเจ็กต ทําใหเกิดความสัมพันธเกี่ยวโยงเปนลําดับช้ัน

3. มีหลายรูปแบบ (polymorphism) คือคุณสมบัติท่ีเม่ือออบเจ็กตตางๆ ไดรับคําส่ังเดยีวกัน จากโปรแกรมแลว แตละออบเจ็กตจะทํางานตามแบบของตัวเอง ซ่ึงทําใหไดผลลัพธแตกตางกัน

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 2

Page 3: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

11.2.2 ความหมายของออปเจ็กต (Object) Object หมายถึง ส่ิงของหรือวัตถุเปาหมาย การเขียนโปรแกรมแบบ OOP นั้น หมายถึง

กลุมขอมูล(data) และกลุมหนาท่ีการทํางาน (function) ท่ีถูกรวมเปนหนึ่งหนวย เราเรียกวา Object สามารถสรุปไดวา Object เปนการนําเอาขอมูลและฟงกชันรวมไวดวยกัน ลักษณะของออปเจ็กตดังกลาวดังรูป เปนลักษณะของการรวมเอาขอมูลและฟงกชันไวในหนวยเดียว กัน เรียกวา เอนทิตี (Entity) เดี่ยว ๆ ซ่ึงเปนแนวคิดสําคัญของการเขียนโปรแกรมแบบ OOP การสรางออปเจ็กตตองสรางจากขอมูลชนิดท่ีเรียกวา คลาส (class)

Object

….. …… 11.3 วิธีสรางคลาส (class)

ตนกําเนิดของออปเจ็กต ออบเจ็กต (object) ตองสรางข้ึนจากขอมูลชนิด คลาส (class) รูปแบบของขอมูลชนิดคลาส มี ลักษณะโครงสราง ดังนี้

คลาส ขอมูล

data1 data2 data3

ฟงกชัน function1() function2() function3()

Data Data Data

Function Function Function

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 3

Page 4: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

รูปแบบของการสรางขอมูลชนิด คลาส (class) เปนดังนี้ class ช่ือคลาส {

private: หรือ public: หรือ protected: ดาตาเมมเบอร; ดาตาเมมเบอร; ดาตาเมมเบอร;

private: หรือ public: หรือ protected: เมมเบอรฟงกชัน; เมมเบอรฟงกชัน; เมมเบอรฟงกชัน;

}; คลาส (class) เปนขอมูลแบบสตรักเจอร(strucuture) ซ่ึงประกอบไปดวยสมาชิกท่ีเรียกวา คลาส

เมมเบอร (class member) หรือสมาชิกของคลาส ซ่ึงมีสมาชิกอยู 2 ประเภท คือ 1. ดาตาเมมเบอร(data member) หรือ กลุมขอมูลท่ีเปนสมาชิกของคลาส ซ่ึงอาจเรียกอยางอ่ืน

ได เชน member variable, data item เปนตน 2. เมมเบอรฟงกชัน (member functions) หรือกลุมของฟงกชันท่ีเปนสมาชิกของคลาส อาจ

เรียกช่ือเปนอยางอ่ืนได เชน Method, Behavior จากรูปแบบสวนประกอบของคลาส มีความหมายดังนี้

class ช่ือคลาส คําวา class เปนคียเวิรด(keyword)ท่ีตองพิมพไวเพื่อกําหนดใหเปนขอมูล ชนิด class เพื่อ เร่ิมตนสรางแหลงกําเนิดของออปเจ็กต(object) ท่ีจะใชในโปรแกรม สวน ช่ือคลาส ตั้งตามกฎเกณฑการตั้งช่ือของ C++ เพื่อนําไวใชอางอิงใน การสรางออปเจ็กต ในโปรแกรมตอไป สวนรายละเอียดอ่ืน ๆ ของคลาสกําหนดไวในเคร่ืองหมาย { ….. }; private: หรือ public: หรือ protected: เปนคียเวิรด(keyword) ตองพิมพเพื่อกําหนด คุณสมบัติของดาตาเมมเบอรและเมมเบอรฟงกชัน มีความหมายดังนี้ private: หมายถึง ช่ือของดาตาเมมเบอรและเมมเบอรฟงกชันนั้น จะเรียกใชได เฉพาะภายในคลาสนี้เทานั้น public: หมายถึง ช่ือของดาตาเมมเบอรและเมมเบอรฟงกชันนั้น จะเรียกใชได ภายในคลาสนี้และจากภายนอกคลาสได protected: หมายถึง ช่ือของดาตาเมมเบอรและเมมเบอรฟงกชันนั้น จะเรียกใชไดเฉพาะภายในคลาส และใน คลาสท่ีสืบทอดตอ ๆ กันไป เทานั้น • ตัวอยางการสรางคลาสช่ือ Date อยางงาย ๆ มีดังตอไปนี้

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 4

Page 5: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

class Date //class name { private: // use only in this class

int Year; //ดาตาเมมเบอร int Month; //ดาตาเมมเบอร int Day; //ดาตาเมมเบอร

public: //use internal and external from another class void SetDate(int Y, int M, int D); //เมมเบอรฟงกชัน void Display(); //เมมเบอรฟงกชัน

};

จากตัวอยางการกําหนดคลาสนี้ ช่ือคลาสคือ Date ประกอบไปดวยดาตาเมมเบอรท่ีเรียกใชไดเฉพาะในคลาสนี้เทานั้น 3 ตัว คือ Year, Month, Day และในคลาสประกอบไปดวยฟงกชันเมม เบอร ท่ีสามารถเรียกใชไดท้ังภายในและภายนอกของคลาส (เพราะอีก 2กําหนดไวดวยคําวา public) ฟงกชัน คือ SetDate() และ Display() ซ่ึงรายละเอียดการเขียน code ของท้ัง 2 ฟงกชันจะไดศึกษาใน ตัวอยางตอไป รูปแบบของการสราง เมมเบอรฟงกชัน มีวิธีการเขียนเหมือนกับฟงกชันปกติของ C++ ท่ี ไมไดเปนสมาชิกของคลาส ดังท่ีไดศึกษามาแลว แตมีรูปแบบท่ีแตกตางกันบาง ดังน้ี รูปแบบท่ี 1 กําหนดรายละเอียดของเมมเบอรฟงกชันตอจากฟงกชัน main() จะตองเขียน ช่ือคลาส ใชเคร่ืองหมายแบงแยกขอบเขต :: (scope resolution operator) ตามหลังช่ือคลาส เช่ือมกับช่ือ เมมเบอรฟงกชัน เพื่อบอกวาเปนสมาชิกของคลาสนั้น เชน // สรางคลาสชื่อ Date กอนเพื่อนําไปสราง Object 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); //เมมเบอรฟงกชัน void Display(); //เมมเบอรฟงกชัน

}; void main() { สเตตเมนตในโปรแกรม; ……. }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 5

Page 6: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

void Date::SetDate(int Y, int M, int D); //เมมเบอรฟงกชันของคลาส Date // สราง member function ช่ือ SetDate เปนสมาชิกของคลาส Date ดยกําหนด ดวยเคร่ืองหมาย :: { สเตตเมนตในฟงกชัน; } void Date::Display(); // สราง member function ช่ือ Display()เปนสมาชิกของคลาส Date โดย // กําหนด ดวยเคร่ืองหมาย :: { สเตตเมนตในฟงกชัน; } รูปแบบท่ี 2 กําหนดรายละเอียดของเมมเบอรฟงกชันกอนฟงกชัน main และอยูภายนอกคลาส จะตองเขียนช่ือคลาสใชเคร่ืองหมายแบงแยกขอบเขต :: (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); //เมมเบอรฟงกชัน void Display(); //เมมเบอรฟงกชัน

}; void Date::SetDate(int Y, int M, int D); //เมมเบอรฟงกชันของคลาส Date { สเตตเมนตในฟงกชัน; } void Date:: Display() //เมมเบอรฟงกชันของคลาส Date { สเตตเมนตในฟงกชัน; } void main() { สเตตเมนตในโปรแกรม; }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 6

Page 7: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

รูปแบบท่ี 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 // ช่ือ SetDate() เปนสมาชิกของคลาส Date โดยสรางไว ในคลาส

{ สเตตเมนตในฟงกชัน; } void Display() //เมมเบอรฟงกชันของคลาส Date สราง member function //ช่ือ Display()เปนสมาชิกของคลาส Date โดยสรางไว ในคลาส { สเตตเมนตในฟงกชัน; } void main() { สเตตเมนตในโปรแกรม; } หมายเหตุ การสรางคลาสและรายละเอียดของเมมเบอรฟงกชัน อาจใชหลายรูปแบบรวมกันได

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 7

Page 8: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

11.4 การสรางและเรียกใชออบจ็กตจากคลาส กอนท่ีจะสรางออบเจ็กตใชในโปรแกรมไดจะตองสรางคลาสใหถูกตองกอนเพราะ คลาส

(class) เปนตนกําหนดของออบเจ็กต ซ่ึงภายในคลาสจะตองกําหนด ดาตาเมมเบอรและเมมเบอรฟงกชันไว และจะตองเขียน code ของเมมเบอรฟงกชันใหเสร็จสมบูรณกอน จึงจะสามารถนําช่ือคลาสไปสราง ออบเจ็กตและเรียกใชออกเจ็กตได ตัวอยางโปรแกรม clas_ex1.cpp เปนตัวอยางการสรางคลาส การสรางออบเจ็กตและการ เรียกใชออบเจ็กตเบ้ืองตน โดยสรางเมมเบอรฟงกชันไวตอจากฟงกชัน main() ดังนี้ /*Program : clas_ex1.cppProcess : 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

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 8

Page 9: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

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 ผลลัพธทางหนาจอ

Display First OOP Programming Year : 2540 Month: 12 Date : 25

{ 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) เปนโอเปอเรเตอรเพื่อเรียกใชสมาชิกของคลาส ตัวอยางโปรแกรม clas_ex2.cpp แสดงถึงการกําหนดใหดาตาเมมเบอรและเมมเบอร ฟงกชันเปนลักษณะ public ดังนั้นการเรียกใชออบเจ็กต birthday ในฟงกชัน main() จึง สามารถเรียกใชดาตาเมมเบอรของ class ไดดวย จึงสามารถกําหนดคาคงท่ีให Year, Month, Day ไดโดยตรง ดังรายละเอียดโปรแกรมตอไปนี้ /*Program : clas_ex2.cppProcess : using public member of class*/ #include <iostream.h> #include <conio.h>

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 9

Page 10: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

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;

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 10

Page 11: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP) ผลลัพธทางหนาจอ Display Second OOP Programming Year : 2540 Month: 12 Date : 25

cout<<"Month: "<<Month<<endl; cout<<"Date : "<<Day<<endl; } ตัวอยางโปรแกรม oop_exam.cpp แสดงการสรางคลาสท่ีช่ือวา VarClass ภายในคลาสประกอบไป ดวย ดาตาเมมเบอร CharVar, IntVar, FloatVar และมีเมมเบอรฟงกชันช่ือ SetValue และ DisplayData โดยเขียนไวภายในคลาส จากนั้นไดสรางออบเจ็กตช่ือ Obj_number ดังตอไปนี้ /*Program : oop_exam.cppProcess : 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

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 11

Page 12: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

void main() //begin main program { VarClass Obj_number; //create object name is Obj_number clrscr();

ผลลัพธทางหนาจอ Character : A Integer : 100 Float : 3.14159

Obj_number.SetValue(); Obj_number.DisplayData(); getch(); } // end main program ตัวอยางโปรแกรม oop_exa2.cpp มีผลการทํางานเหมือนโปรแกรม oop_exam.cpp แตเขียนเมมเบอรฟงกชันไวนอกคลาส และสรางไวหลังฟงกชัน main() มีรูปแบบดังนี้ /*Program : oop_exa2.cppProcess : 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(); }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 12

Page 13: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

void VarClass::SetValue() //function in class { CharVar = 'A'; IntVar = 100; FloatVar= 3.14159; }

ผลลัพธทางหนาจอ Character : A Integer : 100 Float : 3.14159

void VarClass::DisplayData() //function in class { cout<<"Character : "<<CharVar<<endl; cout<<"Integer : "<<IntVar<<endl; cout<<"Float : "<<FloatVar<<endl; } ตัวอยางโปรแกรม clas_ex3.cpp มีการสรางเมมเบอรฟงกชัน GetDate() เพิ่มใน โปรแกรมเพื่อทําหนาท่ีรับคาคงท่ีทางคียบอรด และสรางออบเจ็กตช่ือ Dday เพิ่มข้ึนอีก หนึ่งออบเจ็กต ดังนี้ /*Program : clas_ex3.cppProcess : 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 };

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 13

Page 14: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

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;

ผลลัพธทางหนาจอ Enter new Year<yyyy>: 2008 Enter new Month<mm>: 02 Enter new Day<dd>: 20 Display Date enter from keyboard by object :birthday Year : 2008 Month: 2 Date : 20

Display Date from argument by object : Dday Year : 2530 Month: 11 Date : 29

cout<<"Enter new Month<mm>: ";cin>>Month; cout<<"Enter new Day<dd>: ";cin>>Day; } 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; }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 14

Page 15: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

ตัวอยางโปรแกรม 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.cppProcess : 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 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 divide = "<< math.divide(number1,number2); getch(); } //end main program

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 15

Page 16: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

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;

ผลลัพธทางหนาจอ input operand1(integer) : 5 input operand2(integer) : 2 Result of sum = 7 Result of subtract = 3 Result of multiply = 10 Result of divide = 2.5

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 16

Page 17: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

ตัวอยางโปรแกรม 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; Evalue.GetScore(); //use object and function of class 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; }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 17

Page 18: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

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;

ผลลัพธทางหนาจอ Program Calculate Grade Enter midterm score: 20 Enter midterm score: 70

Total of your score: 90 You get grade : A

} ตัวอยางโปรแกรม 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 memberchar address[50];

int age; public: //member functionvoid input();

void display(); }; TEST Info; //create object ...Info... from class

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 18

Page 19: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

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();

ผลลัพธทางหนาจอ Enter your information: Name: Urachart Kokaew Address: 77 Age: 50

********************* Your information... ********************* Name: Urachart Kokaew Address: 77 Age: 50 *********************

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();

}

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 19

Page 20: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

11.5 ตัวอยางการเขียนโปรแกรมแบบ OOP จัดการ Array และ Structure เราสามารถจัดการขอมูลท่ีเปน array โดยสรางใหเปนขอมูลชนิด Object ได ดังตัวอยาง

โปรแกรมตอไปนี้ ตัวอยางโปรแกรม OOP_ARR.CPP แสดงการใช array ท่ีเปน Object ช่ือ Object คือ result [5] สรางจาก class ท่ีช่ือ TEST โดยจอง array ไวท้ังหมด 5 ชอง มี member function ของ lass คือ 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(); }; TEST result[5]; //Create Object ....is Array result[5] void main() { clrscr(); int i; for(i=0;i<=4;i++){ result[i].input(); } cout<<endl<<"Display number 1, number 2 in array"<<endl;

ผลลัพธทางหนาจอ Enter your number: Number1: 5 Number2: 21 Enter your number: Number1: 23 Number2: 22 Enter your number: Number1: 33 Number2: 14 Enter your number: Number1: 11 Number2: 2 Enter your number: Number1: 223 Number2: 33

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 20

Page 21: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

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;

ผลลัพธ(ตอ) Display number 1, number 2 in array Your number : Number1: 5 Number2: 21 Your number : Number1: 23 Number2: 22 Your number : Number1: 33 Number2: 14 Your number : Number1: 11 Number2: 2 Your number : Number1: 223 Number2: 33 Display Summation: Sum[0]26 Sum[1]45 Sum[2]47 Sum[3]13 Sum[4]256

} void TEST::summation() { sum=number1+number2; }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 21

Page 22: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

ตัวอยางโปรแกรม 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

ผลลัพธทางหนาจอ Code: 1 Name: a Position: boss Salary: 1200 Code: 2 Name: b Position: emp Salary: 1000 Code: 3 Name: c Position: prog Salary: 2000 Code: 4 Name: d Position: dean Salary: 30000 Code: 5 Name: e Position: secu Salary: 1000 จากนั้นจะมีผลท่ีคอยๆเคาะออกมาทีละ record 5 หนาจอ

{ 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(); Employee.Report(); getch(); }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 22

Page 23: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

void EMP::Input() //Detail of member function { int i; for(i=0;i<=4;i++) { cout<<"Code: "; cin >> person[i].code; cout<<"Name: "; cin >> person[i].name; cout<<"Position: "; cin >>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<<"KKU. Co.ltd"; gotoxy(1,3);cout<<"----------------------------------------------------";

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 23

Page 24: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

gotoxy(1,4);cout<<"Code Name - Surname Position Salary"; gotoxy(1,5);cout<<"----------------------------------------------------"; for(i=0;i<=4;i++){

หนาจอผลลัพธสุดทาย Report Salary Expense KKU. Co.ltd ---------------------------------------------------- Code Name - Surname Position Salary ---------------------------------------------------- 1 a boss 1200 2 b emp 1000 3 c prog 2000 4 d dean 30000 5 e secu 1000

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; r++; if(r>=22){ getch(); r=6; } }

} 11.6 การกําหนดคาดวยฟงกชันชนิดคอนสตรักเตอร

จากตัวอยางท่ีผานมาในการกําหนดคาใหแกดาตาเมมเบอรของคลาสในขณะเรียกใชออปเจ็กตนั้น เราจะกําหนดคาคงท่ีใหขณะท่ีมีการเรียกใช เชน Dday.SetDate(2530,11,29); หรือมีการรับคาทางคียบอรดเพื่อกําหนดคาดาตาเมมเบอร โดยการกําหนดคาใหแก ดาตาเมมเบอรสามารถกําหนดใหมีคาเร่ิมตนแบบอัตโนมัติได เมมเบอรฟงกชันชนิดท่ีเรียกวา คอนสตรักเตอร (constructor) ซ่ึงจะเปนเมมเบอรฟงกชันท่ีทํางานโดย อัตโนมัติทันทีท่ีสรางข้ึนเพราะฉะนั้นกําหนดคาเร่ิมตนของออบเจ็กตนจะตองกําดาตาเมมเบอรไวเปนคาเร่ิมตนดวย การสรางเมมเบอรฟงกชันชนิดคอนสตรักเตอร สรางไวใน class กําหนดช่ือใหเหมือนกับช่ือของ class และกําหนดคาคงท่ีใหแก ดาตาเมมเบอรภายในเคร่ืองหมาย { } ดังรูปแบบตัวอยางในโปรแกรม 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

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 24

Page 25: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

public: Date() { //constructor member function…function name same as class name Year = 1997; //คาคงท่ีของ data member ท่ีเปนคาเร่ิมตน Month=12; Day=31; } 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; cout<<endl<<"Display Date from constructor member function"<<endl; birthday.Display(); 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; }

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 25

Page 26: บทที่ 10 Introduction to OOP - home.kku.ac.th · PDF fileบทที่ 11 Introduction to OOP . ... ดัั้งน นการเขียนโปรแกรมแบบ

Introduction to Object Oriented Programming (OOP)

ผลลัพธหนาจอ

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

322 112 Introduction to Programming ผศ.อุรฉัตร โคแกว 26