620 likes | 1.79k Vues
นาย ธีรวัฒน์ ประกอบผล รองศาสตราจารย์ระดับ 9 คณะวิทยาศาสตร์ สถาบันเทคโนโลยีพระจอมเกล้า เจ้าคุณทหารลาดกระบัง kpteeraw@kmitl.ac.th www.kmitl.ac.th/~kpteeraw. โปรแกรมภาษาซี , เครื่องมือในการพัฒนา คลาส และอ๊อบเจ็กต์ AppWizard การออกแบบหน้าจอติดต่อกับผู้ใช้
E N D
นายธีรวัฒน์ ประกอบผล รองศาสตราจารย์ระดับ 9 คณะวิทยาศาสตร์ สถาบันเทคโนโลยีพระจอมเกล้า เจ้าคุณทหารลาดกระบัง kpteeraw@kmitl.ac.th www.kmitl.ac.th/~kpteeraw
โปรแกรมภาษาซี , เครื่องมือในการพัฒนา • คลาส และอ๊อบเจ็กต์ • AppWizard • การออกแบบหน้าจอติดต่อกับผู้ใช้ • การแมปเมสเสจ , ขบวนการ DDX , การจัดการสตริง • การใช้ Checkbox,Radiobox,List box , การจัดการไฟล์ • การสร้างเมนู • การสร้าง DLL • แนวทางการพัฒนาโปรแกรมประยุกต์ลักษณะต่าง ๆ เนื้อหาการเขียนโปรแกรม Visual C++
C++ C C Win32API MFC,OWL C++ แนวทางการศึกษาภาษาซี เขียนโปรแกรมบนวินโดวส์ เขียนเกมบนวินโดว์ด้วย DirectX C SDL, DirectX OpenGL C++ & Windows programming
ภาษาC++ 1.ต่างกันที่นามสกุล 2. ประกาศตัวแปรที่ใดก็ได้ 3. ใช้ // เป็นคอมเมนท์ 4. ใช้สตรีม I/O เป็นวิธีในการแสดงผล
ตัวแปร (variables) ตัวแปรเก็บข้อมูลชนิดจำนวนเต็ม bool เป็นข้อมูลบูลีน (boolean) char short int หรือ long ตัวแปรเก็บข้อมูลชนิดจำนวนจริง float double หรือ long double
ค่าDefault ของพารามิเตอร์ int total(int a = 10,int b = 20) { return a+b; } void main() { printf(“%d\n”,total( ) ); printf(“%d\n”,total(534,45) ); }
การโอเวอร์โหลดฟังก์ชั่น(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; }
บอกว่าจะใช้คำสั่งพื้นฐานบอกว่าจะใช้คำสั่งพื้นฐาน โปรแกรมแรก #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
การเขียนโค้ดแบบเดิม ๆ void B( ) { } void A( ) { } void Z( ) { } void C( ) { }
Class Together { } การเขียนโค้ดแบบเดิม ๆ void B( ) { } void A( ) { } void Z( ) { } void C( ) { }
ประกาศฟังก์ชันในคลาสได้ประกาศฟังก์ชันในคลาสได้ เปลี่ยนจาก 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)
การนิยามคลาส 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(); }
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
เหตุใดจึงต้องมี 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() ) }
พอยเตอร์ 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; }
ตัวอย่าง class Text { char buffer[256]; public: void SetText(char buffer[256]) { strcpy(this->buffer,buffer); } void PrintfText() { printf(“%s\n”,buffer); } };
ตัวอย่าง 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()); }
เมธอดแสดงผล เมธอดที่เป็นฟังก์ชันของคลาส 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;
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; }
การประกาศค่าคงที่ในคลาสการประกาศค่าคงที่ในคลาส จะใช้ const และ enum ตัวอย่างเช่น const double PI = 3.14; enum {OPEN,CLOSE,ALIVE}; enum { JACK=11,QUEEN,KING,ACE }
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();
แนวคิดแบบ 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); }
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); }
ในการสร้างคลาสไม่สามารถกำหนดค่าเริ่มต้นได้ในการสร้างคลาสไม่สามารถกำหนดค่าเริ่มต้นได้ class MyClass { int time = 20; int level = 5; }
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); } };
Constructor และ Destructor ใช้กำหนดค่าตัวแปรให้กับคลาส ต้องมีชื่อเดียวกับคลาส
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); } };
การส่งค่าให้คอนสตรัดเตอร์การส่งค่าให้คอนสตรัดเตอร์ class circle { double r; public: circle(double temp) { r = temp; } void getarea() { printf(“Area = %f”,3.14*r*r); } }; circle c(10); c.getarea();
ดีสตรัดเตอร์(Destructor) class Test { public: Test() { } ~ Test() { } };
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); } };
การโอเวอร์โหลดคอนสตรัคเตอร์การโอเวอร์โหลดคอนสตรัคเตอร์
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());
รูปแบบ การแยกฟังก์ชันไว้นอกคลาส return-type class-name :: function-name(input-parameter-list) { /* BODY */ }
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; }
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]) };
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
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; }
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; }
การสืบทอดคลาส (Inheritance) • คลาสแม่ (Base class หรือ Parent Class) • คลาสลูก (Child class)มีคุณสมบัติและสมาชิกทุกตัวเหมือนคลาสลูก
รูปแบบการสืบทอดคลาส class คลาสใหม่ : (ขอบเขต) คลาสแม่ที่ต้องการ { // ประกาศตัวแปรและฟังก์ชันเพิ่มเติม };
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()); }
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:
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()); } ตัวอย่าง
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()); } ตัวอย่าง
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()); } ตัวอย่าง
ตัวแปร 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(); }
ตัวแปร 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(); }
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
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