1 / 54

นาย ธีรวัฒน์ ประกอบผล รองศาสตราจารย์ระดับ 9 คณะวิทยาศาสตร์ สถาบันเทคโนโลยีพระจอมเกล้า

นาย ธีรวัฒน์ ประกอบผล รองศาสตราจารย์ระดับ 9 คณะวิทยาศาสตร์ สถาบันเทคโนโลยีพระจอมเกล้า เจ้าคุณทหารลาดกระบัง kpteeraw@kmitl.ac.th www.kmitl.ac.th/~kpteeraw. โปรแกรมภาษาซี , เครื่องมือในการพัฒนา คลาส และอ๊อบเจ็กต์ AppWizard การออกแบบหน้าจอติดต่อกับผู้ใช้

yagil
Télécharger la présentation

นาย ธีรวัฒน์ ประกอบผล รองศาสตราจารย์ระดับ 9 คณะวิทยาศาสตร์ สถาบันเทคโนโลยีพระจอมเกล้า

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

Presentation Transcript


  1. นายธีรวัฒน์ ประกอบผล รองศาสตราจารย์ระดับ 9 คณะวิทยาศาสตร์ สถาบันเทคโนโลยีพระจอมเกล้า เจ้าคุณทหารลาดกระบัง kpteeraw@kmitl.ac.th www.kmitl.ac.th/~kpteeraw

  2. โปรแกรมภาษาซี , เครื่องมือในการพัฒนา • คลาส และอ๊อบเจ็กต์ • AppWizard • การออกแบบหน้าจอติดต่อกับผู้ใช้ • การแมปเมสเสจ , ขบวนการ DDX , การจัดการสตริง • การใช้ Checkbox,Radiobox,List box , การจัดการไฟล์ • การสร้างเมนู • การสร้าง DLL • แนวทางการพัฒนาโปรแกรมประยุกต์ลักษณะต่าง ๆ เนื้อหาการเขียนโปรแกรม Visual C++

  3. C++ C C Win32API MFC,OWL C++ แนวทางการศึกษาภาษาซี เขียนโปรแกรมบนวินโดวส์ เขียนเกมบนวินโดว์ด้วย DirectX C SDL, DirectX OpenGL C++ & Windows programming

  4. ภาษาC++ 1.ต่างกันที่นามสกุล 2. ประกาศตัวแปรที่ใดก็ได้ 3. ใช้ // เป็นคอมเมนท์ 4. ใช้สตรีม I/O เป็นวิธีในการแสดงผล

  5. ตัวแปร (variables) ตัวแปรเก็บข้อมูลชนิดจำนวนเต็ม bool เป็นข้อมูลบูลีน (boolean) char short int หรือ long ตัวแปรเก็บข้อมูลชนิดจำนวนจริง float double หรือ long double

  6. ค่าDefault ของพารามิเตอร์ int total(int a = 10,int b = 20) { return a+b; } void main() { printf(“%d\n”,total( ) ); printf(“%d\n”,total(534,45) ); }

  7. การโอเวอร์โหลดฟังก์ชั่น(Overloading Function) void main() { test(); test(40); test(20,30) } void test() { // } void test(int a) { // } int test(int a, int b) { return a+b; }

  8. บอกว่าจะใช้คำสั่งพื้นฐานบอกว่าจะใช้คำสั่งพื้นฐาน โปรแกรมแรก #include <iostream> using namespace std; void main() { char name[256]; cout<<“What is your name ?”<<endl; cin>>name; cout<<“Hello “<<name<<endl; } endl End Line ends End String

  9. การเขียนโค้ดแบบเดิม ๆ void B( ) { } void A( ) { } void Z( ) { } void C( ) { }

  10. Class Together { } การเขียนโค้ดแบบเดิม ๆ void B( ) { } void A( ) { } void Z( ) { } void C( ) { }

  11. ประกาศฟังก์ชันในคลาสได้ประกาศฟังก์ชันในคลาสได้ เปลี่ยนจาก struct เป็น class class Person { int age; void setage(int a) { age = a; } }; class Person { int age; }; void main() { Person p1,p2,p3; p1.age = 35; } เรียก p1, p2, p3 ว่า อ๊อบเจ็กต์ (Object)

  12. การนิยามคลาส class Person { int age; void setage(int a) { age = a; } void showage() { printf(“age = %d\n”,age); } }; void main() { Person p; } class ชื่อคลาส { ประกาศตัวแปร ประกาศฟังก์ชั่น } void main() { Person p; p.setage(25); p.showage(); }

  13. class Person { int age; public; void setage(int a) { age = a; } void showage() { printf(“age = %d\n”,age); } }; void main() { Person p; } กฎการเข้าถึงสมาชิกในคลาส ส่วนตัว หรือ Private สาธารณะ หรือ Public ปกป้อง หรือ Protected

  14. เหตุใดจึงต้องมี private หรือ public class Circle { double pi; double r; public: void setr(double temp) { pi = 3.14; r = temp; } double getarea() { return pi*r*r; } }; main() { Circle c1; c1.setr(20.0); printf(“area = %f “,c1.getarea() ) }

  15. พอยเตอร์ this class Circle { double pi; double r; public: void setr(double temp) { pi = 3.14; r = temp; } double getarea() { return pi*r*r; } }; กรณีที่พารามิเตอร์มีชื่อเดียวกับสมาชิกของคลาส void setr(double r) { pi = 3.14; this->r = r; }

  16. ตัวอย่าง class Text { char buffer[256]; public: void SetText(char buffer[256]) { strcpy(this->buffer,buffer); } void PrintfText() { printf(“%s\n”,buffer); } };

  17. ตัวอย่าง class Vector3D { float x,y,z; public: void SetValue(float a,float b,float c) { x=a; y=b; z = c; } void ShowValue() { printf(“The vector is (%0.2f,%0.2f,%0.2f)\n”,x,y,z); } double VectorLength() { double l; l = sqrt(x*x + y*y +z*z); return l; } }; void main() { Vector3D v1; v1.SetValue(3,2,3); v1.ShowValue(); printf(‘Length of the vector is %0.2f\n”, v1.VectorLength()); }

  18. เมธอดแสดงผล เมธอดที่เป็นฟังก์ชันของคลาส iostream put(char); write(text,size); ในการใช้งานสามารถเขียนเป็นลูกโซ่ได้ cout.put(‘S’); cout.put(‘A’).put(‘=‘).put(65); cout.write(“C++ program”,11); cout.write(“C++ program”,3)<<“….”<<endl;

  19. void set(int a, char name[256]) { this -> age=age; strcpy(this->name,name); } void set(int age,char name[256],double grade) { this->age=age; this->grade=grade; strcpy(this->name,name); } }; การโอเวอร์โหลดฟังก์ชั่นในคลาส class Person { int age; double grade; char name[256]; public: void set(int age) { this->age=age; }

  20. การประกาศค่าคงที่ในคลาสการประกาศค่าคงที่ในคลาส จะใช้ const และ enum ตัวอย่างเช่น const double PI = 3.14; enum {OPEN,CLOSE,ALIVE}; enum { JACK=11,QUEEN,KING,ACE }

  21. void myfriend() { Test t; t.num1 = 20; t.num2 = 30; t.dosomething(); } void main() { myfriend(); } ฟังก์ชั่นแบบ friend class Test { private: int num1,num2; void dosomething() { printf(“%d,%d\n”,num1,num2); } }; public: friend void myfriend();

  22. แนวคิดแบบ OOP void main() { circlearea(2.5); circlelong(2.5); circlearea(4.8); circlelong(4.8); } #include <stdio.h> #define PI 3.14 void circlearea(double r) { printf(“area = %f\n”,PI*r*r); } void circlelong(double r) { printf(“Circle’s border long = %f\n”,2*PI*r); }

  23. void main() { Circle c1, c2; c1.setr(2.5); c2.setr(4.8); c1.getarea(); c1.getlong(); c2.getarea(); c2.getlong(); } class Circle { #define PI 3.14 double r; public: void setr(double r) { this ->r = r; } void getarea() { printf(“area = %f\n”,PI*r*r); } void getlong() { printf(“Circle’s border long = %f\n”,2*PI*r); }

  24. ในการสร้างคลาสไม่สามารถกำหนดค่าเริ่มต้นได้ในการสร้างคลาสไม่สามารถกำหนดค่าเริ่มต้นได้ class MyClass { int time = 20; int level = 5; }

  25. void main() { Sum s1; s1.addnum(5); s1.addnum(10); s1.addnum(15); s1.addnum(16); s1.addnum(20); s1.addnum(23); s1.showdata(); } class Sum { int total; int counter; public: void addnum(int temp) { total += temp; counter++; } void showdata() { printf(“total = %d, counter = %d\n”total,counter); } };

  26. Constructor และ Destructor ใช้กำหนดค่าตัวแปรให้กับคลาส ต้องมีชื่อเดียวกับคลาส

  27. Sum() { total = counter = 0; } void main() { Sum s1; s1.addnum(5); s1.addnum(10); s1.addnum(15); s1.addnum(16); s1.addnum(20); s1.addnum(23); s1.showdata(); } class Sum { int total; int counter; public: void addnum(int temp) { total += temp; counter++; } void showdata() { printf(“total = %d, counter = %d\n”total,counter); } };

  28. การส่งค่าให้คอนสตรัดเตอร์การส่งค่าให้คอนสตรัดเตอร์ class circle { double r; public: circle(double temp) { r = temp; } void getarea() { printf(“Area = %f”,3.14*r*r); } }; circle c(10); c.getarea();

  29. ดีสตรัดเตอร์(Destructor) class Test { public: Test() { } ~ Test() { } };

  30. void main() { easyfile myfile(“test.txt”); myfile.writestring(“Hello”); myfile.writestring(“COMPUTER”); } class easyfile { FILE *fp; public: easyfile(char fname[30]) { fp=fopen(fname,”w+t”); } ~ easyfile() { fclose(fp); } void writestring(char s[256]) { fputs(s,fp); } };

  31. การโอเวอร์โหลดคอนสตรัคเตอร์การโอเวอร์โหลดคอนสตรัคเตอร์

  32. void set(double w,double h) { wide = w; height = h; } double getarea() { return (0.5)*wide*height; } }; class Triangle { double wide,height; public: Triange() { wide = height = 0; } Triange(double w, double h) { wide = w; height = h; } Triange t1; t1.set(3.5,4.6); Printf(“Area of Triange is %f\n”,t1.getarea()); Triange t1(3.5,4.6); Printf(“Area of Triange is %f\n”,t1.getarea());

  33. รูปแบบ การแยกฟังก์ชันไว้นอกคลาส return-type class-name :: function-name(input-parameter-list) { /* BODY */ }

  34. Class Vector3D { float x,y,z; public: void SetValue(float a,float b,float c); void ShowValue(); double VectorLength(); }; void Vector3D::SetValue(float a, float b, float c) { x = a; y = b; z = c; } void Vector3D::ShowValue() { printf(“The vector is (%0.2f,%0.2f,%0.2f)\n”,x,y,z); } ตัวอย่าง double Vector3D::VectorLength() { double l; l = sqrt(x*x+y*y+z*z); return l; }

  35. void easyfile::writestring(char s[256]) { fputs(s,fp); } void main() { easyfile myfile(“test.txt”); myfile.writestring(“Hello”); myfile.writestring(“COMPUTER”); } class easyfile { FILE *fp; public: easyfile(char fname[30]) { fp=fopen(fname,”w+t”); } ~ easyfile() { fclose(fp); } void writestring(char s[256]) };

  36. Class Vector3D { float x,y,z; public: Vector3D(); void SetValue(float a,float b,float c); void ShowValue(); double VectorLength(); }; Vector3D::Vector3D() { x = 0; y = 0; z = 0; } Constructor

  37. Constructor Class Vector3D { float x,y,z; public: Vector3D(float a, float b, float c); Vector3D(); void SetValue(float a,float b,float c); void ShowValue(); double VectorLength(); }; Vector3D::Vector3D() { x = 0; y = 0; z = 0; } Vector3D::Vector3D(float a, float b, float c) { x = a; y = b; z = c; }

  38. Destructors ทำลาย Object Class Vector3D { float x,y,z; public: Vector3D(float a, float b, float c); ~Vector3D(); void SetValue(float a,float b,float c); void ShowValue(); double VectorLength(); }; Vector3D::~Vector3D() { delete x; delete y; delete z; }

  39. การสืบทอดคลาส (Inheritance) • คลาสแม่ (Base class หรือ Parent Class) • คลาสลูก (Child class)มีคุณสมบัติและสมาชิกทุกตัวเหมือนคลาสลูก

  40. รูปแบบการสืบทอดคลาส class คลาสใหม่ : (ขอบเขต) คลาสแม่ที่ต้องการ { // ประกาศตัวแปรและฟังก์ชันเพิ่มเติม };

  41. class CSum { double sum; int n; public: CSum() { sum = 0; n = 0; } void add(int num) { sum += num; n++; } double getsum( ) { return sum; } }; ตัวอย่าง public class CStat : CSum { }; void main( ) { CStat s1; s1.add(10); s1.add(20); printf(“sum = %1.2f\n”,s1.getsum()); }

  42. class CSum { double sum; int n; public: CSum() { sum = 0; } void add(int num) { sum += num; n++; } double getsum( ) { return sum; } }; class CStat : public CSum { public: CStat() { } double getaverage() { return sum/n; } }; void main( ) { CStat s1; s1.add(10); s1.add(20); printf(“sum = %1.2f\n”,s1.getsum()); printf(“average = %1.2f\n”,s1.getaverage()); } ตัวอย่าง protected:

  43. class Circle { protected; double r; public: Circle(double number) { r = number; } double getarea( ) { return 3.14*r*r; } }; class Circle2 : public Circle { public: Circle2(double number) { r = number; } double getlong() { return 3.14*2*r; } }; void main( ) { Circle c(5.6); printf(“Area = %1.2f\n”,c.getarea()); } ตัวอย่าง

  44. class Circle { protected; double r; public: Circle(double number) { r = number; } double getarea( ) { return 3.14*r*r; } }; class Circle2 : public Circle { public: Circle2(double number) : Circle(number) { // r = number; } double getlong() { return 3.14*2*r; } }; void main( ) { Circle c(5.6); printf(“Area = %1.2f\n”,c.getarea()); } ตัวอย่าง

  45. class Circle { protected; double r; public: Circle() { r = 0; } Circle(double number) { r = number; } double getarea( ) { return 3.14*r*r; } }; class Circle2 : public Circle { public: Circle2(double number) { r = number; } double getlong() { return 3.14*2*r; } }; void main( ) { Circle2 c(5.6); printf(“Area = %1.2f\n”,c.getarea()); printf(“Circle long = %1.2f\n”,c.getlong()); } ตัวอย่าง

  46. ตัวแปร static class Person { int km; public: Person( ) { km = 0; } void run( ) { km++; printf(“%d “,km); } }; class Person { public: void run( ) { int km = 0; km++; printf(“%d “,km); } }; void main() { Person p1; p1.run(); p1.run(); p1.run(); }

  47. ตัวแปร static class Person { public: void run( ) { static int km = 0; km++; printf(“%d “,km); } }; class Person { public: void run( ) { int km = 0; km++; printf(“%d “,km); } }; void main() { Person p1; p1.run(); p1.run(); p1.run(); }

  48. class Person { public: void run( ) { static int km = 0; km++; printf(“%d “,km); } }; void main() { Person p1,p2,p3; p1.run(); p1.run(); p1.run(); } 1 2 3

  49. class Person { public: void run( ) { static int km = 0; km++; printf(“%d “,km); } }; void main() { Person p1,p2,p3; p1.run( ); p1.run( ); p1.run( ); p2.run( ); p2.run( ); p3.run( ); }; 1 2 3 4 5 6

More Related