Lecture 8 Array and Struct ( ตัวแปรชุด และ ข้อมูลแบบโครงสร้าง)
An Image/Link below is provided (as is) to download presentation
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.Content is provided to you AS IS for your information and personal use only.Download presentation by click this link.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.During download, if you can't get a presentation, the file might be deleted by the publisher.
E N D
More Related Content
Presentation Transcript
Lecture 8 Array and Struct (ตัวแปรชุด และ ข้อมูลแบบโครงสร้าง)
การกำหนดค่าเริ่มต้นให้อาร์เรย์การกำหนดค่าเริ่มต้นให้อาร์เรย์ เราสามารถกำหนดค่าเริ่มต้นให้อาร์เรย์ได้โดยมีตัวอย่างดังนี้ #include <iostream.h> void main() { char A[10]={‘S’,’o’,’m’,’p’,’o’,’n’,’g’}; } จากด้านบนค่า A[0] = S , A[1] = o , A[2] = m , A[3] = p A[4] = o , A[5] = n , A[6] = g , A[7] = ‘’ A[8] = ‘’, A[9] = ‘’
A S o m p o n g \o A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] การกำหนดค่าเริ่มต้นให้อาร์เรย์ เราสามารถกำหนดค่าเริ่มต้นให้อาร์เรย์ได้โดยมีตัวอย่างดังนี้ #include <iostream.h> void main() { char A[]=“Sompong”; } คอมไพเลอร์จะจองหน่วยความจำให้อัตโนมัติและจะมี \o ต่อท้ายเสมอ แสดงการสิ้นสุดข้อความ \o
A S o m p o n g \o A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] การกำหนดแบบนี้ไม่ได้น่ะครับ เพราะว่า การใช้เครื่องหมาย “ “ จะมี \o ต่อท้ายของ array เสมอ #include <iostream.h> void main() { char A[7]=“Sompong”; } คอมไพเลอร์จะจองหน่วยความจำให้อัตโนมัติและจะมี \o ต่อท้ายเสมอ แสดงการสิ้นสุดข้อความ \o
A S o m p o n g \o A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] แบบนี้กำหนดได้ครับ จองหน่วยความจำไว้ 8 บล็อก ไม่ได้ก็ให้มันรู้ไป!!! #include <iostream.h> void main() { char A[8]=“Sompong”; } แสดงการสิ้นสุดข้อความ \o
A S o m p o n g \o A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] แบบนี้กำหนดก็เหมือนกันน่ะครับ กำหนดได้เยอะแยะ ไปหมด เลือกตามที่เราถนัดแล้วกัน #include <iostream.h> void main() { char A[8]={“Sompong”}; } แสดงการสิ้นสุดข้อความ \o
A S o m p o n g A[0] A[1] A[2] A[3] A[4] A[5] A[6] แบบนี้ก็ได้ครับ ถ้าอยากจองหน่วยความจำไว้ 7 บล็อก เท่าเดิม แต่ต้องเป็นตัวอักษร น่ะ #include <iostream.h> void main() { char A[7]={‘S’,’o’,’m’,’p’,’o’,’n’,’g’}; }
8.4 การส่งค่า Array ไปยังฟังก์ชัน ตัวอย่าง #include <iostream.h> #include <math.h> int cube(int x[]); //declare function int main(void) { int a[]={1,2,3}; int ans; ans=cube(a); cout<<"Result is "<<ans<<endl; return 0; } int cube(int x[]) { int result; //local variable result = x[0]+x[1]+x[2]; return result; }
สงสัยไหมน่ะ ????? การส่งค่าเข้าในฟังก์ชันของตัวแปรแบบ array เป็นแบบ Pass by value(ส่งค่าไปให้)หรือ Pass by reference(ส่งค่าอ้างอิงไปให้) 2แบบนี้...ถ้าลืมแล้วก็กลับไปดู Lecture 7 ครับ
ตัวอย่าง การส่งผ่านตัวแปร array ไปให้ฟังก์ชัน #include <iostream.h> #include <math.h> void cube(int x[]); //declare function int main(void) { int a[]={1,2,3}; int ans; cout<<"Result is "<<a[0]<<a[1]<<a[2]<<endl; ans=cube(a); cout<<"Result is "<<a[0]<<a[1]<<a[2]<<endl; return 0; } void cube(int x[]) { x[0]=4; } ทำตัวอย่างนี้แล้วตอบด้วยน่ะครับว่าเป็นการส่งแบบไหน Pass by value(ส่งค่าไปให้)หรือ Pass by reference(ส่งค่าอ้างอิงไปให้) 2แบบนี้...ถ้าลืมแล้วก็กลับไปดู Lecture 7 ครับ
รูปแบบ Memory Map ของ Object struct children{ int age; char sex; char tel[4]; int hei; }Sompong; Sompong เป็น Object ของ struct ชื่อ Children เมื่อเราต้องการเข้าถึงข้อมูล ภายใน struct childern จะต้องเข้าถึงผ่าน Object ของ struct children Somong hei tel hei age age sex tel tel tel
ตัวอย่างการใช้งาน struct struct children{ int age; char sex; char tel[9]; int height; }; void main(void) { children Sompong; cin >> Sompong.age; cout << Sompong.age; } ประกาศ Object Sompong ภายในฟังก์ชันเมน
วิธีการกำหนด class classชื่อ { ตัวแปร ตัวแปร }; เช่น class children{ int age; char sex; char tel[9]; int height; };
ตัวอย่างการใช้งาน class class children{ int age; char sex; char tel[9]; int height; }; void main(void) { children Sompong; cin >> Sompong.age; cout << Sompong.age; } ลองเขียนดูแล้ว Run ดูน่ะครับ ว่าผลเป็นยังงัย !!
ตัวอย่างการใช้งาน class ต้องแก้เป็นแบบนี้ เราว่า struct จะ default ไว้เป็นแบบ Private class children{ public: int age; char sex; char tel[9]; int height; }; void main(void) { children Sompong; cin >> Sompong.age; cout << Sompong.age; }