1 / 54

2010 summer vacation

2010 summer vacation. Marshall, CEITL. INDEX. 2010 summer. Struct Dynamic memory allocation Class Constructor, destructor template. STRUCT. 2010 summer. Basic data types Syntax More… Constructor Destructor Homework. Basic data types – types and sizes. STRUCT.

dalia
Télécharger la présentation

2010 summer vacation

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. 2010 summer vacation Marshall, CEITL

  2. INDEX 2010 summer • Struct • Dynamic memory allocation • Class • Constructor, destructor • template

  3. STRUCT 2010 summer • Basic data types • Syntax • More… • Constructor • Destructor • Homework

  4. Basic data types – types and sizes STRUCT Ref : http://msdn.microsoft.com/en-us/library/cc953fe1(v=VS.80).aspx

  5. Basic data types – range of values STRUCT Ref : http://msdn.microsoft.com/en-us/library/s3f49ktz(VS.80).aspx

  6. Exercise STRUCT • 有一長方體a, 其長、寬、高為(3,4,5);x,y,z坐標分別為(1,1,2), 沿x, y, z 軸分別移動(13,2,6) 後, 中心坐標與其體積為? • 有一長方體b, 其長、寬、高為(1,4,2);x,y,z坐標分別為(3,3,2), 沿x, y, z 軸分別移動(6,9,12) 後, 中心坐標其體積為?

  7. syntax STRUCT struct PERSON{ bool gender; int age; double height, weight; }; void main(){ PERSON tim; tim.gender = 1; tim.age = 18; tim.height= 180.0; tim.weight = 80.5; //PERSON tim = {1,18,180.0,80.5}; } 結構體的名字 定義結構體 成員 分號 宣告變數 程示主體 存取成員 也可以…

  8. Size of struct STRUCT • Size of ? • 對於結構體,編譯器會自動進行成員變數的對齊,以提高運算效率。預設情況下,編譯器為結構體的每個成員按其自然對界(natural alignment)條件分配空間。自然對界是指按結構體的成員中size最大的成員對齊。 第一個成員的位址和整個結構的位址相同。 struct PERSON{ bool gender; int age; double height, weight; }; Ref : http://stenlyho.blogspot.com/2007/04/ccstruct.html

  9. Constructor & destructor STRUCT #include <iostream> #include <cstdlib> using namespace std; struct numbers{ int number1; double number2; numbers(){ number1 = 5; number2= 5.5; cout << "numbers is created! " << endl; } ~numbers(){ cout <<"一個number的結構體被終結了" << endl; } }; void main(){ cout << a.number1 << endl; cout << a.number2 << endl; } 建構函式 解構函式 Ref :http://www.study-area.org/coobila/tutorial_422.html

  10. Constructor & destructor (2) STRUCT • void main(){ • cout << "main starts" << endl; • numbers a; • a.number1=100; • a.number2=999; • sub(a); • sub(a); • sub(a); • sub(a); • sub(a); • cout << "main ended " << endl; • } #include <iostream> #include <cstdlib> using namespace std; struct numbers{ int number1; double number2; numbers(){ number1 = 5; number2= 5.5; cout << "numbers is created! " << endl; } ~numbers(){ cout <<“A numbers be terminated!" << endl; } }; void sub(numbers a){ cout << a.number1 << endl; cout << a.number2 << endl; } • What will be output ? Ref :http://www.study-area.org/coobila/tutorial_422.html

  11. exercise STRUCT • 產生10個長方體, 並以亂數產生其座標與長、寬、高。座標為小於 10 的整數,長寬高為小於5的浮點數。 • 在結構體刪除時輸出”刪除了(x,y,z)=(??,??,??), (L,W,H)=(??,??,??)的結構體” Hint : rand(): 在 <cstdlib> 定義, 隨機從0~RAND_MAX 挑出一個整數。 型別轉換:(double)rand()/RAND_MAX

  12. Summary STRUCT • Size and range of data types • Syntax of struct • Constructor & destructor • What is …. • When will be called ….. • Lifecycle of variables • Static variables • Alignment • rand()

  13. Dynamic memory allocation 2010 summer • Data & memory address • Pointer & address • Array • Dynamic memory allocation • Exercise

  14. Data & memory address • Dynamic memory allocation void main(){ inti = 999; int* p = &i; }

  15. Pointer & address • Dynamic memory allocation i void main(){ inti = 999; int* p = &i; //int *p 或 int* p 均可 // int* p1,p2,p3; // int *p1,*p2,*p3; cout << i <<","<<&i << endl; cout << p << "," << *p << "," << & p << endl; } int 999 0031FDAC p Int* 0031FDAC 0031FDA0

  16. Array - syntax • Dynamic memory allocation • 一維陣列 : int a[5] = {1,2,4,5,6}; • 取得可存放5個int的記憶體位置,並把起始位址存入a • a[0] : 從a的位址走 0 步 , 取出一個 int • a[1] : 從a的位址走 1 步 , 取出一個 int • 產生10個長方體, 並以亂數產生其座標與長、寬、高。 • 產生200個長方體, 並以亂數產生其座標與長、寬、高。

  17. Array - syntax • Dynamic memory allocation • 一維陣列 : int a[5] = {1,2,3,4,5}; • 二維陣列 : int b[3][2]={{1,2},{3,4},{5,6}};

  18. Exercise • Dynamic memory allocation • 以二維陣列與一維陣列計算下題結果: • Hint : • int a[4][3]; • int b[3]; • int c[4];

  19. Dynamic memory allocation • Dynamic memory allocation • 3 types of variables • Static • Normal • Dynamic Void main(){ static int a[5]; int b[10]; int* c = new int[10]; delete [] c; c= new int[200]; } 1 add 2 c Int* add 3 Without “delete”, What will happen?

  20. 2D dynamic matrix ( 3 x 2 ) • Dynamic memory allocation • void main(){ • int** a; • a = new int*[3]; • for(int i = 0 ; I < 3 ; i++) • a[i] = new int[2]; • //a[0] = new int[2]; • //a[1] = new int[2]; • //a[2] = new int[2]; • for(inti=0; i<3 ;i++){ • delete [] a[i]; • } • delete [] a; • } Int** Int* Int* Int*

  21. Char array • Dynamic memory allocation char c1 = ‘b’ ; char c[20] = “Hello!!” ; char s[3][100] = {“Hello”, “world” , “!!”}

  22. Exercise • Dynamic memory allocation • 寫出一程式, 讓使用者輸入整數, 程式會將此整數因數分解, 並存入兩整數陣列。分別存入 factor[] 與 power[]. Ex : 輸入 40 . 將存入 factor[] = {2,5} power [] = {3,1} 即40 = 23*51

  23. Summary • Dynamic memory allocation

  24. Class 2010 summer • Object oriented programming (OOP) • Member & method • Private & public • Constructor

  25. Exercise 2010 summer • 有一矩形, 其長為5, 寬為3,長邊方向與Y軸同向,另其與原點相距最遠的點為corner1, 質量中心座標為(3 , 7), 矩型對質量中心做3。/sec 順時針轉動;且其中心對原點作3。/sec 逆時針轉動, 試求其第0~180sec, corner1的座標。 corner1 Y X

  26. Object oriented programming (OOP) 2010 summer • 以物件為主的思考方式,能更直觀的將現實中的目標在程式中模擬出來。 一般的想法 物件導向的想法 公轉角速度 *時間*半徑 定義矩形: 中心位置 公轉路徑 自轉方式 長 寬 自轉角速度 *時間*半徑 找出矩形 中心點的位置 Corner1相對 於中心點的位置 輸入時間 Corner1 的位置 Corner1的坐標

  27. Member & method 2010 summer void printcorner(){ printf("corner1 : ( x , y ) = ( %f , %f)\n" , xpos + width/2 , ypos + length/2 ); printf("corner2 : ( x , y ) = ( %f , %f)\n" , xpos + width/2 , ypos - length/2 ); printf("corner3 : ( x , y ) = ( %f , %f)\n" , xpos - width/2 , ypos - length/2 ); printf("corner3 : ( x , y ) = ( %f , %f)\n" , xpos - width/2 , ypos + length/2 ); } }; class rectangular{ private: double xpos, ypos; double length, width; public: rectangular(){ xpos = 0 ; ypos = 0 ; } void setsize(double l, double w){ length = l; width = w; } void move(double x , double y ){ xpos += x ; ypos += y; } 方法 (method)

  28. Private & public 2010 summer • Encapsulation(封裝性) : 透過存取權限設定而可達到資料保護的效果, • 權限的分類: • public : 不論物件內外均可存取。 • Protect : …skip… • Private : 物件內的成員才能存取。(C++預設) • 通常,物件內的變數均會設成private,而方法設成public,要更改變數的內容均用呼叫mehtod來達成。

  29. Constructor definition & calling 2010 summer void main(){ rectangular rectA; rectA.printcorner(); rectA.setlocation(5.55,2.22); rectA.setsize(3.00, 5.00); rectA.printcorner(); rectangular rectB(123.33 ,22.1); rectB.printcorner(); rectB.setsize(5.66 , 3.22); rectB.printcorner(); rectangular rectC(1 ,2, 3, 4); rectC.printcorner(); } class rectangular{ private: double xpos,ypos; double length, width; public: rectangular(){ a = 0; } rectangular(double x, double y){ xpos =x ; ypos = y; } rectangular(double x, double y, double l ,double w){ xpos = x; ypos= y; length = l; width = w; } void setlocation(double x, double y){……} void setsize(double l, double w){……} void printcorner(){…….} }; Constructors?

  30. Exercise 2010 summer • 一球由座標(2000, 5000) 沿x軸方向發射,重力加速度a(x,y)為(0, -9.8),請以10秒為單位cout出球的所在位置,至球落地為止。 • 有一矩形, 其長為5, 寬為3,長邊方向與Y軸同向,質量中心位置(x , y) = ( t , sin(x/360) ) , 矩型對質量中心做3。/sec 順時針轉動;試求其第0~180sec, 4個角的座標。

  31. Class (2) 2010 summer • Pointer of class • Syntax “->” • Special word “this” • Array of class • More about constructor • Default constructor • Copy constructor • Normal constructor • Operator overloading • Copy assignment operator • Operator overloading

  32. Pointer of class class(2) //保留字 “this” Class rect{ Private: double l,w,x,y; Public: rect(); void setsize(double l , double w){ this-> l = l; this->w = w; } void setpos(double x, double y){ this-> x = x; this ->y = y; } }; //類別的陣列 Class rect{ Private: double l,w,x,y; Public: rect(); setsize(double l , double w); setpos(double x, double y); }; Void main(){ rect r[3]; r[1].setsize(100,200); rect *a = new rect[3]; a[0] .setsize(100,200); ……… } //宣告類別的指標 Class rect{ Private: double l,w,x,y; Public: rect(); void setsize(double l , double w); void setpos(double x, double y); }; Void main(){ rect *a = new rect; a->setsize(4,3); a->setpos(1,2); }

  33. Exercise Class(2) • 請將因式分解改寫為物件導向的版本。 • 宣告一類別number,其中成員包含 • 待因式分解的值value, • 結構體factor,內有value及power,均為int[40] • 所以 value = factor.value[0]^factor.power[0] *….. • 建構函式 number(int input),使得 input 為待分解值 value, 並在建構函式中完成分解。

  34. Constructor”s” Class(2) • Normal constructor • Default constructor • Copy constructor Pan’s house Mar’s house

  35. Copy constructor Class(2) house(const house& other){ for(int i =0 ; i< 3 ; i++){ this->door[i] = other.door[i] ; this->wall[i] = other.wall[i] ; this->roof[i] = other.roof[i] ; this->chimney[i] = other.chimney[i] ; } } //copy constructor }; class house{ private: unsigned short door[3], wall[3]; unsigned short roof[3], chimney[3]; public: house(){} // default constructor house(unsigned short *a,unsigned short *b, unsigned short *c,unsigned short *d ){ for(int i =0 ; i< 3 ; i++){ this->door[i] = a[i] ; this->wall[i] = b[i] ; this->roof[i] = c[i] ; this->chimney[i] = d[i] ; } } //normal constructor void main(){ unsigned short color1[3] ={255,255,255} ; unsigned short color2[3] ={255,255,0}; unsigned short color3[3] ={0,255,0}; unsigned short color4[3] ={255, 0 ,0}; house pan(color1 , color2, color3 ,color4); house mar(pan); }

  36. Operator overloading Class(2) //copy values between variables int a = 5; int b; b=a; //copy values between classes house pan(color1,color2,color3,color4); house mar; mar = pan //????????? 初始化/Initialize 指定/assign 可以執行, 但會有問題, 尤其是指標成員

  37. Operator overloading Class(2) //Copy assignment operator class house(){ … house& operator=(const house& other){ this->~house(); for(int i =0 ; i< 3 ; i++){ this->door[i] = other.door[i] ; this->wall[i] = other.wall[i] ; this->roof[i] = other.roof[i] ; this->chimney[i] = other.chimney[i] ; } return *this; } … }; 常數變數(const variable): 只能在初始化改變其值, 之後即無法更改的變數 a=(b=c);

  38. Operator overloading Class(2) //other operators classX operator+(const classX& rightObj){ classXans ; ans.data1 = data1+rightObj.data1; .............. return ans; }

  39. Exercise Class(2) 完成下面類別: Class intarray{ Private: int* value; //值的陣列 int size; //值陣列的大小 Public: intarray(); intarray(int* input, int size); // intarray(const intarray& other); ~intarray(); void setvalue(int * input, int size); //設值的function intarray& operator=(const intarray& other); //copy assignment operator intarray operator+(const intarray& other); //將兩個陣列接起來 };

  40. template 2010 summer • What template can do • Function template • Class template • Non-Type Parameter

  41. What template can do template Template <class T> T myAdd(T a,T b){ return a+b; } intmyAdd(int a, int b){ return a+b; } double myAdd(double a, double b){ return a+b; } unsigned short float ????

  42. Syntax template template<class T> void functionX(…){ … T data; … } template<typename T> class Y{ … T data; … }; template<class T1, class T2> class Z{ … T1 data; T2data1; … • }; • Function of compiler • Key word “class”/”typename” • Can apply to either class or function • Multi type paraments are acceptable

  43. Function template template #include <iostream> using namespace std; intmyAdd(int a ,int b){ cout << "processing intmyAdd" << endl; return a+b; } template<class T> T myAdd(T a, T b){ cout << "processing template myAdd" << endl; return a+b; } void main(){ double a=1, b=2 ; cout << myAdd(a,b) << endl; }

  44. class template template 原型宣告 template<class T1, class T2> class myClass{ private: T1* a; T2* b; public: myClass(){ a= new T1[0]; b= new T2[0]; cout << "template class in created ... " << endl; } }; template<class T1, class T2>class myClass; 實現 呼叫 void main(){ myClass<int,int> c1,c2; }

  45. Non-Type Parameter template #include <iostream> using namespace std; template<class T, inti> class Buffer{ T v[i]; int size; public: //Buffer():size(i) {} Buffer(){ size = i; } void print(){ cout << "size=" << size <<"," << v << endl; } }; void main(){ Buffer<char , 127> cBuf ; cBuf.print();

  46. Default values Parameter template #include <iostream> using namespace std; template<class T = string , inti=127> class Buffer{ T v[i]; int size; public: //Buffer():size(i) {} Buffer(){ size = i; } void print(){ cout << "size=" << size <<"," << v << endl; } }; void main(){ Buffer<char > cBuf ; cBuf.print();

  47. Template specialization template template<class T>class List; //當其定義無法適用於所有的型態時, 無法適用的型態要另 //外定義 template<>class List<char*>; //定義當宣告為char* 時, class List 的成員內容

More Related