發表文章

c 動態記憶體

動態記憶體 動態記憶體可以讓使用者在定義變數時不需事先之到變數大小,使用動態記憶體需使用指標的方式儲存記憶體位置,並且在變數使用完閉後釋放記憶體空間。 Example 以 struct 型式使用動態記憶體 typedef struct { char *name; int age; }person; int main(){ person * myperson = malloc (sizeof(perosn)); myperson->name=”John”; myperson->age = 27; free(myperson); }

c 語言 function

structure  define example struct point { int x; int y; } ; int main(){  struct point p; p.x=10; p.y=5; draw(p); }   進階定義的方法使用typeodef typedef:能夠自己定義資料型態的名稱,用在struct上能夠再定義struct型態的變數時不用再打一次struct。  define example typedef struct { int x; int y; } point; int main(){  point p; p.x=10; p.y=5; draw(p); }    輸入struct變數進入function內做運算 void move(point *p){  (*p).x++;  (*p).y++; } 或是   void move(point *p){  p->x++;  p->y++; }  

C程式語言學習

http://www.learn-c.org/

在ubuntu中編寫c語言

1.安裝必要的編譯器 開啟Terminal輸入 sudo apt - get install build - essential   2.編寫.c的檔案 隨意使用一個文字編寫軟體編寫c語言後另存新檔為.c副檔名的檔案 ex:   #include<stdio.h> int main(){ printf("Hello"); return 0; } 另存新檔為.c檔 3.回到Terminal中進行編譯     這裡假設檔名為prog.c   開啟Terminal輸入  gcc prog.c -o canberun 4.最後執行程式      開啟Terminal輸入   ./ canberun 其中 canberun 為第3步我們將編譯後可執行檔的檔名此名稱能夠隨意命名  

Pointer

圖片
Pointer use ex result

Class

圖片
class 將程式的變數轉換為物件的型式 大多數class的邊寫方法都會與main當分開 分為一個.h當用來定義class中會用到的變數型態或function型態 和另一cpp執行檔用來實做.h檔中定義的各變數或function   Example1 main.c header file app file result define variables in donstructors Example2 main.c header file app file result use class constant function 當以class之變數型態定義變數時,若此定義之變數為constant則只能使用class內之constant function Example3 main.c header file app file result define class constant variable 我們知道constant variable的數值需要在定義值時順便給與,但是相同的class可以代表不同的物體,可能會有同的constant數值,這時候可以使用 member initialization list 再設定物件時定義數值。 Example4 main.c header file app file result Friend Functions 正常來說定義在class 內 private的變數不能被外部改變,但我們可以藉由定意外不function 為class 的friend function,讓這個friend function可以編輯private 內的變數。 注意當要將物件(class)傳入function 時需以指標的形式 Example5 main.c header file app file result Example

Overloading

Function Overloading Function Overloading 只要函式的輸入參數數量或是型態不同,就能夠讓多個不同功能的函式擁有一樣的名子 Example