或物件所屬的空間名稱(chēng)_第1頁(yè)
或物件所屬的空間名稱(chēng)_第2頁(yè)
或物件所屬的空間名稱(chēng)_第3頁(yè)
或物件所屬的空間名稱(chēng)_第4頁(yè)
或物件所屬的空間名稱(chēng)_第5頁(yè)
已閱讀5頁(yè),還剩37頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、1Object oriented programminglWhat is Class? What is Object?-From object-oriented point of view-Class is a user-defined data type which contains relevant data and functions-Object is a variable declared under some class data type-From philosophy concept-Class is an abstract concept that describes the

2、 attributes of a collection of objects 2From C to C+lNamespace-變數(shù)、函數(shù)、或物件所屬的空間名稱(chēng),在不同的namespace中可使用相同的變數(shù)名稱(chēng)。-std: C+所有系統(tǒng)提供的函數(shù)所屬的namespace-avoid conflict of variable names in the different class libraries3namespace example/This program outputs the message/C+:one small step for the program,/one giant lea

3、p for the programmer/to the screen#include using namespace std;int main()cout C+:one small step for the program,n one giant leap for the programmer n;return 0;compare to C: #include main( ) printf(“.”); without namespace4namespaceslcreate namespace-examples:namespace mfc int inflag; void g(int); nam

4、espace owl int inflag; 5namespaceluse variables in a namespace-use scope resolution operator :-e.g.mfc:inflag = 3;owl:inflg = -823;cout x;cin len;cout x;cout x len; cout x len;7C+ Input/Outputlexample#include using namespace std;int main( ) int id;float av;cout id av;cout “Id:” id n “Average:” av n;

5、return 0;Enter the id and the average:900038 90.8Id:900038 Average:90.8 8C+ Input OutputlManipulators-for the format of I/O-set width to n: setw(n)for (i=1; i=1000; i*=10) cout setw(6) i n;1 10 100 1000 9manipulatorslendl: end of line, write new i=4, j=6, k=8;char c=!;cout i c endl j c

6、n k c endl;4! 6! 8! 10manipulatorsloct (octal), hex(hexadecimal), dec(decimal)- i = 91;cout “i=“ i “ (decimal)n”;cout “i=“ oct i “ (octal)n”;cout “i=“ hex i “ (hexadecimal)n”;cout “i=“ dec i “ (decimal)n”;i=91 (decimal) i=133 (octal) i=5b (hexadecimal) i=91 (decimal) 11manipulators llisted in

7、 chap. 14-9-dec, endl, fixed, flush, hex, left, oct, right, scientific, setfill( c ), setprecision(n), setw(n), showpoint, noshowpoint, showpos, noshowpos, skipws, noskipws, ws- 12manipulatorslsetfill, setprecision-e.g.float a = 1.05, b=10.15, c=200.87;cout setfill() setprecision(2);cout setw(10) a

8、n;cout setw(10) b n;cout setw(10) c n;*1.05 *10.15 *200.87 13files (example)#include using namespace std;const int cutoff =6000;const float rate1 =0.3;const float rate2 =0.6;int main()ifstream infile;ofstream outfile;int income,tax;in(income.txt);out(tax.txt);while (infile income ) if (income cutoff

9、 )tax =rate1 *income;elsetax =rate2 *income;outfileIncome=income greenbacks n Tax =tax greenbacks n;in();out();return 0;14files (example cont.)input file “income.txt” 2214 10500 31010result: output file “tax.txt” Income = 2214 greenbacks Tax= 664 greenbacks Income = 10500 greenbacks Tax= 6299 greenb

10、a cks Income = 31010 greenbacks Income = 18605 greenbacks15filesltesting whether files are open- converts to true if open successfully, otherwise converts to false-e.g. ifstream infile;ifstream.open(“scores.dat”);if (infile) / if open sucessfullyorif (!infile) / if fail to open the file 16files-e.g.

11、ifstream infile;in(“scores.dat”);if (!infile) cerr “Unable to open scores.datn”; exit(0);17C+ featureslbool data type-values: true (1) or false(0)-manipulators: boolalpha, noboolalpha (default)-e.g.bool flag;flag = (3 5);cout flag n;cout boolalpha flag n;1 true 18the type stringlstring initializatio

12、n-e.g.#include string s1;string s2 = “Bravo”;string s3 = s2;string s4(10,x);cout s1 n;cout s2 n;cout s4 n;Bravo xxxxxxxxxx 19the type stringlC-style string (end with a null char 0)char mystring = “a string”; orchar mystring = “a string”;printf(“%sn”, mystring); char mystring9-the null character 0 is

13、 added by the C compiler automatically a s t r i n g 0 20the type stringlstring lengthstring s = “Ed Wood”;cout “Length=“ s.length( ) n;linput a string -separate by space or new linecout s;cout s;Length=7 Ed Wood Ed 21getline function example (copy file)#include #include #include using namespace std

14、;int main()string buff;ifstream infile;ofstream outfile;cout buff;in(buff.c_str();cout buff;out(buff.c_str();while (getline(inflie, buff) outfile buff“nn”;in();out();return 0;22the type stringlinput a line of string from cinstirng s;getline(cin, s);lconcatenation of stringstring s1=“Atlas ”, s2=“Kin

15、g”, s3;s3 = s1 + s2;cout s1 n;cout s2 n;cout s3 n;Atlas King Atlas King 23the type stringlremove a substring-s.erase(position, length);-e.g.string s = “Ray Dennis Steckler”;s.erase(4, 7);cout s n;Ray Steckler 24the type stringlinsert a string-s.insert(position, str2);lreplace a substring-s.replace(s

16、tartpos, length, str2);ls-s1.s);lextract a substring-s.substr(position, length)25the type stringl operatorstring s = “Nan”cout s1; n;s0 = J;cout s n;lsearch a substring-s1.find(s2, position);lcompare strings-= , !=, , =a Jan 26functionslreference variables-provides an alternative name for storage-e.

17、g. memoryint x;int& ref=x; x refx = 3; cout ref;327functionslcall by value (default in C)-pass values to the called functionlcall by reference-pass addresses to the called function-provided in C+, but not in C-e.g. void s, int&);28call by reference#include using namespace std;void s, int&

18、;);int main( ) int i=7, j=-3; swap(i, j); cout “i=“ i n “j=“ j n; return 0;void s a, int& b) int t; t=a; a=b; b=t; pass address of i, j to a,b main( ) swap( ) ti aj bi=-3 j=7 29call by reference in C (use pointer)#include void s*, int*); / function prototypemain( ) int i=7, j=-3; s, &j); / p

19、assing addresses of i and j printf(“i=%d j=%d”, i,j);void s* a, int* b) / use pointers parameters insteadint t; / of reference variablest = *a; *a = *b; *b=t; / use pointers to reference the / values in main function30functionsloverloading functions-functions can have the same name, but-function sig

20、natures need to be distinct-function name, and -number, data type, and order of it arguments-e.g. void print(int a); void print(double a); / o.k. void print(int a, int b); / o.k. int print(int a); / wrong!, return type is not part of signatures31functionsldefault arguments-all the parameters without

21、 default values must come first in the parameter list.-better to specify in the prototype, e.g.void f(int val, float s=12.6, char t=n, string msg=“Error”);-valid invocationf(14, 48.3, t, “ok”); / s=48.3, t=t, msg=“ok”f(14, 48.3); / s=48.3, t=n, msg=“Error”f(14); / s=12.6, t=n, msg=“Error”32functions

22、loverloading functions-functions can have the same name, but-function signatures need to be distinct-function name-number, data type, and order of it arguments-e.g. void print(int a); void print(double a); / o.k. void print(int a, int b); / o.k. int print(int a); / wrong! return type is not part of

23、signatures33overloading functions#include #include void print(int a);void print(double a);int main( ) int x=8; double y=8.0; print(x); print(y); return 0;void print(int a) cout a n;void print(double a) cout showpoint a n;8 8.000 34dynamic (vs. static)allocationldynamic allocation-pointer_var = new d

24、ata-type; / single data-pointer_var = new data-typesize; / array-e.g. int* int_ptr; int_ptr = new int; ptr int* ptr; ptr = new int100; ptr0 ptr1 ptr9935dynamic allocationldelete, delete -free storage allocated by new or new typesize-e.g. delete int_ptr;delete ptr;llinked list example name next start

25、 “林旺” “馬蘭” “阿美” 0 36dynamic allocation#include using namespace std;struct Elephant string name; Elephant* next;void print_elephants(const Elephant* ptr );Elephant* get_elephants( );void free_list(const Elephant* ptr );int main( ) Elephant* start; start =get_elephants( ); print_elephants(start ); fre

26、e_list(start ); return 0; 37dynamic allocationElephant* get_elephants( )Elephant *current,*first;int response;current =first =new Elephant;cout current -name;cout response;/Add Elephants to list until user signals halt.while (response =1 ) current =current -next =new Elephant; cout current -name; co

27、ut response; current -next =0;return first;38dynamic allocationvoid print_elephants(const Elephant* ptr )int count =1;cout n n n;while (ptr !=0 )cout Elephant number“ count+ is name next; void free_list(const Elephant* ptr )const Elephant* temp_ptr;while (ptr !=0 ) temp_ptr =ptr -next; delete ptr; ptr =temp_ptr; 39dynamic

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論