版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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-變數、函數、或物件所屬的空間名稱,在不同的namespace中可使用相同的變數名稱。-std: C+所有系統提供的函數所屬的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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五版法律服务企业法务专员职位劳动合同3篇
- 二零二五版房屋买卖合同范本下载涉及装修及家具家电条款3篇
- 二零二五年时尚服饰品牌区域独家代理销售合同2篇
- 二零二五年度航空货运大客户承运合同范本3篇
- 二零二五年建筑材料出口销售与绿色认证合同3篇
- 二零二五版grc构件生产、安装与装配式建筑推广实施合同3篇
- 二零二五版技术开发与成果转化合同3篇
- 二零二五年建筑材料运输及安装服务合同6篇
- 二零二五年度家具安装与室内空气净化合同2篇
- 二零二五版展览馆场地租赁合同范本(含展览策划服务)3篇
- 公路工程施工现场安全检查手册
- 公司组织架构图(可编辑模版)
- 1汽轮机跳闸事故演练
- 陕西省铜川市各县区乡镇行政村村庄村名居民村民委员会明细
- 礼品(礼金)上交登记台账
- 北师大版七年级数学上册教案(全册完整版)教学设计含教学反思
- 2023高中物理步步高大一轮 第五章 第1讲 万有引力定律及应用
- 青少年软件编程(Scratch)练习题及答案
- 浙江省公务员考试面试真题答案及解析精选
- 系统性红斑狼疮-第九版内科学
- 全统定额工程量计算规则1994
评论
0/150
提交评论