1 / 5

// 用建構函數範例 Page:7-18,7-25 #include <iostream.h> #include <conio.h> class counter { private:

一個類別可以有一個以上的 constructor 我們稱為 overloaded constructor, 只要 constructor 之引數個數或資料型態不一 樣 , 則 compiler 便可視為不同之 constructor. // 用建構函數範例 Page:7-18,7-25 #include <iostream.h> #include <conio.h> class counter { private: unsigned int count; public: counter() // constructor

Download Presentation

// 用建構函數範例 Page:7-18,7-25 #include <iostream.h> #include <conio.h> class counter { private:

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. 一個類別可以有一個以上的 constructor • 我們稱為overloaded constructor, 只要 • constructor 之引數個數或資料型態不一樣, • 則 compiler 便可視為不同之constructor // 用建構函數範例 Page:7-18,7-25 #include <iostream.h> #include <conio.h> class counter { private: unsigned int count; public: counter() // constructor { count=0; } void countchar(); int getcount() { return count; } }; void counter::countchar() { char ch; cout << "\nPlease enter a string: \n"; while ((ch=getche())!='\r') { count++; } } void main() { counter c1; c1.countchar(); cout << "\n Consists " << c1.getcount(); cout << "characters" << endl; } #include <iostream.h> class String { char *str; public: String(); String(char *); void print() { cout << str << endl;} }; String::String() { str="abcde"; } String::String(char *ptr) { str=ptr; } void main() {String a; // call String() String b("xyz"); // call String(char *) a.print(); b.print(); }

  2. 通常利用 constructor 來配置記憶體,並利用 destructor 來釋回 #include <iostream.h> #include <string.h> class Strings { private: char *str; public: Strings() { strcpy(str,""); } Strings(char *st) { str=new char[strlen(st)+1]; strcpy(str,st); } ~Strings() { delete str; } void set(char *ptr) { str=new char[strlen(ptr)+1]; strcpy(str,ptr); } void printstr() { cout << str << endl; } }; void main() { char *title="London bridge is falling down !"; Strings ps1; // call String() ps1.set("London bridge"); / /use m.f. for initial ps1.printstr(); Strings ps2(title); // call Strings(char *st) for initial ps2.printstr(); Strings ps3("falling down"); //call String(char *st) ps3.printstr(); for initial Strings ps4="is falling down"; //call String(char *st) ps4.printstr(); for initial }

  3. 另一種Constructor 初始值的設定方法:將初值設定在constructor 之引數中, • 呼叫時有設初值的引數可省略不寫。 #include <iostream.h> class Time { private: int hour,minute,second; public: Time(int hr=0, int min=0,int sec=0) { hour=hr; minute=min; second=sec; } void print() { cout << hour << ":" << minute << ":" << second << endl;} }; void main() {Time t1,t2(2),t3(21,34),t4(12,25,42); t1.print(); t2.print(); t3.print(); t4.print(); } #include <iostream.h> class Time { private: int hour,minute,second; public: Time(int hr, int min,int sec) { hour=hr; minute=min; second=sec; } void set(int hr, int min, int sec) {hour=hr; minute=min; second=sec; } void print() { cout << hour << ":" << minute << ":" << second << endl;} }; void main() {Time t1,t2(2),t3(21,34) // error! Time t4(12,25,42); t4.print(); t4.set(13,24,55); t4.print(); }

  4. C++之動態記憶體配置 • C 之動態記憶體配置函數為 指標變數=malloc(容量) 及free(指標變數), • malloc 通常搭配sizeof(型態變數) 以配置程式師所指定的記憶體容量。 • 例如: int *ptr; • ptr=(int) malloc(10*sizeof(int)); • 以 sizeof 將int 之bytes 數算出,以 malloc 將使電腦配置20 bytes 記憶體並將 • 起始位址存入指標變數 ptr 中。 • C++之動態記憶體配置指令為為 New 與Delete • 格式一: 指標變數= new 基本型態變數 [個數] • 格式二: 指標變數= new 自定型態變數 (初始化之值) • 將new 所配置之記憶體釋回: delete 指標變數 • 例: int *ptr; • ptr=new int [100]; • … • delete ptr; 2 bytes 20 bytes 可眚略 例: float *ptr; ptr=new float (3.14); ... delete ptr

  5. 成員的初始化串列 • 當我們用類別來定義物件時,系統會先為類別內的資料成員配置好記憶體 空間,然後再呼叫適當的建構函數來設定初值。然而,有時我們會希望系統 在配置空間時能同時作初始化的工作,這時就可以用“成員初始化串列” 。 • 成員初始化串列必須出現在 constructor 的定義(而非宣告)之中: constructor 名稱(參數…): 資料成員名稱(初值運算式)... #include <iostream.h> #include <conio.h> class Test { private: int i; int &ri; const int ci; public: Test(int a, int &b, int c); void Put() { cout << "i=" << i << endl; cout << "ri=" << ri << endl; cout << "ci=" << ci << endl; } }; Test::Test(int a, int &b, int c) : ri(b),ci(c) { i=a; } void main() { clrscr(); int k=4; Test t1(2,k,6); t1.Put(); }  建立 i 之空間  建立 ri, 並設定ri 為 b之 reference(綽號)  建立 ci, ci  c  執行 constructor, i  a 配置順序 t1 i ri k 初始化串列 ci Output: i=2 ri=4 ci=6 每個資料成員在串列中最多只能出現一次,初值 的運算可以是常數、變數或複雜運算式,其排列 次序不重要,系統為資料配置時依他們在類別定 義(宣告) 中出現的順序來執行

More Related