1 / 18

# 高等程式語言實習課 - PowerPoint PPT Presentation

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about '高等程式語言實習課' - more

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 高等程式語言實習課

int main()

{

const double pi = 3.1415926;

const char *p = “Hello”;

char * const p = “Hello”;

const char * const p = “hello”

}

const 物件 的 值 只能在宣告時設值

void print(int &a,int &b){ cout << a << ““ << b << endl; a=0; b=0;}

int main()

{ int x=3,y=5; print(x,y); cout << x << ““ << y << endl;}

void print(const int &a,const int &b){ cout << a << ““ << b << endl; a=0; b=0; //<=能跑嗎?}

int main()

{ int x=3,y=5; print(x,y); cout << x << ““ << y << endl;}

class test{ const int a;

int b;public: test(int _a,int _b)

{

a=_a;

b=_a;

}

};

error C2758: 'a' : must be initialized in constructor base/member initializer list

class test{ const int a;

int b;public: test(int _a,int _b):a(_a),b(_b){}

};

class clock{ int hr; int min; int sec;public:…

int gethr() const {return hr;} //為什麼要加const? int getmin() const {return min;} int getsec() const {return sec;}…

};

const 常數函數 不可修改成員變數

class test{

int a;

int b;

public:

int geta() { return a;}

int getb() { return b;}…

};

test operator+(const test &t1,const test &t2)

{

test t;

t.set(t1.geta(),t2.getb());

return t;

}

error C2662: 'geta' : cannot convert 'this' pointer from 'const class test' to 'class test Conversion loses qualifiers

class test{

int a;

int b;

public:

int geta() const { return a;}

int getb() const { return b;}…

};

test operator+(const test &t1,const test &t2)

{

test t;

t.set(t1.geta(),t2.getb());

return t;

}

Non-const

Non-static

object

const

static

member

function

• 想要計數程式執行了幾次這個物件

class test{

int xxoo;public: test(int &t,int ooxx) { t=t++; xxoo=ooxx;

}};

class test{ static int t;

int xxoo;public: test(int ooxx) { t++; xxoo = ooxx;

}};

error LNK2001: unresolved external symbol "private: static int test::t" (?t@test@@0HA)

class test{ static int t=0;

int xxoo;public: test(int ooxx) { t++; xxoo = ooxx;

}};

error C2252: 't' : pure specifier can only be specified for functions

class test{ static int t;

int xxoo;public: test(int ooxx) { t++; xxoo = ooxx;

}};

int test::t = 0;

class math{public: int abs(int n) { return –n;

} int pow(int n,int m) {…

} int sin(int c) {

}

};

voin main()

{

math m;

cout<<m.abs(1-5);

cout<<m.pow(2,10);

cout<<m.sin(60);

}

voin main()

{

cout<<math::abs(1-5);

cout<<math::pow(2,10);

cout<<math::sin(60);

}

class math{public: static int abs(int n) { return –n;

} static int pow(int n,int m) {…

} staticint sin(int c) {

}

};