或物件所属的空间名称.ppt
《或物件所属的空间名称.ppt》由会员分享,可在线阅读,更多相关《或物件所属的空间名称.ppt(42页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Object 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 a
2、ttributes of a collection of objects 1From C to C+lNamespace-變數、函數、或物件所屬的空間名稱,在不同的namespace中可使用相同的變數名稱。-std:C+所有系統提供的函數所屬的namespace-avoid conflict of variable names in the different class libraries2namespace example/This program outputs the message/C+:one small step for the program,/one giant leap f
3、or 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 namespace3namespaceslcreate namespace-examples:namespace mfc int inflag;void g(int);namespace ow
4、l int inflag;4namespaceluse 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;6C+Input/Outputlexample#include using namespace std;int main()int id;float av;cout id av;cout “Id:”id n “Average:”av n;return 0;Enter the i
5、d and the average:900038 90.8Id:900038 Average:90.8 7C+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 8manipulatorslendl:end of line,write new line-e.g.int i=4,j=6,k=8;char c=!;cout i c endl j c n k c endl;4!6!8!9manipulatorsl
6、oct(octal),hex(hexadecimal),dec(decimal)-e.g.int 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)10manipulators llisted in chap.14-9-dec,endl,fixed,flush,hex,left,oct,right,scien
7、tific,setfill(c),setprecision(n),setw(n),showpoint,noshowpoint,showpos,noshowpos,skipws,noskipws,ws-11manipulatorslsetfill,setprecision-e.g.float a=1.05,b=10.15,c=200.87;cout setfill()setprecision(2);cout setw(10)a n;cout setw(10)b n;cout setw(10)c n;*1.05 *10.15 *200.87 12files(example)#include usi
8、ng 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;infile.open(income.txt);outfile.open(tax.txt);while(infile income)if(income cutoff)tax=rate1*income;elsetax=rate2*income;outfileIncome=income greenbacks n Tax=
9、tax greenbacks n;infile.close();outfile.close();return 0;13files(example cont.)input file“income.txt”2214 10500 31010result:output file“tax.txt”Income=2214 greenbacks Tax=664 greenbacks Income=10500 greenbacks Tax=6299 greenba cks Income=31010 greenbacks Income=18605 greenbacks14filesltesting whethe
10、r files are open-file object 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 15files-e.g.ifstream infile;infile.open(“scores.dat”);if(!infile)cerr “Unable to open sc
11、ores.datn”;exit(0);16C+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 17the type stringlstring initialization-e.g.#include string s1;string s2=“Bravo”;string s3=s2;string s4(10,x);cout s1
12、 n;cout s2 n;cout s4 n;Bravo xxxxxxxxxx 18the 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 added by the C compiler automatically a s t r i n g 0 19the type stringlstring lengthstri
13、ng 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 20getline function example(copy file)#include#include#include using namespace std;int main()string buff;ifstream infile;ofstream outfile;cout buff;infile.open(buff.c_str();cout buf
14、f;outfile.open(buff.c_str();while(getline(inflie,buff)outfile buff“nn”;infile.close();outfile.close();return 0;21the type stringlinput a line of string from cinstirng s;getline(cin,s);lconcatenation of stringstring s1=“Atlas”,s2=“King”,s3;s3=s1+s2;cout s1 n;cout s2 n;cout s3 n;Atlas King Atlas King
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 物件 所属 空间 名称
限制150内