Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
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) 後, 中心坐標其體積為?
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()
Array - syntax • Dynamic memory allocation • 一維陣列 : int a[5] = {1,2,3,4,5}; • 二維陣列 : int b[3][2]={{1,2},{3,4},{5,6}};
Exercise • Dynamic memory allocation • 以二維陣列與一維陣列計算下題結果: • Hint : • int a[4][3]; • int b[3]; • int c[4];
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?
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*
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
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); }
Constructor”s” Class(2) • Normal constructor • Default constructor • Copy constructor Pan’s house Mar’s house
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); }
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 可以執行, 但會有問題, 尤其是指標成員
template 2010 summer • What template can do • Function template • Class template • Non-Type Parameter
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 ????
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
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; }
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; }
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();
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();
Template specialization template template<class T>class List; //當其定義無法適用於所有的型態時, 無法適用的型態要另 //外定義 template<>class List<char*>; //定義當宣告為char* 時, class List 的成員內容
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.