面向对象的程序设计.ppt_第1页
面向对象的程序设计.ppt_第2页
面向对象的程序设计.ppt_第3页
面向对象的程序设计.ppt_第4页
面向对象的程序设计.ppt_第5页
已阅读5页,还剩42页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 - 47,Object-Oriented Programming,计算机科学与技术学院 刘培强 ,2010-2011-01 电信08 计科09,2 - 47,Chapter2 From C to C+,2.1 Namespaces 2.2 Introduction to C+ Input/Output 2.3 Files 2.4 C+ Features 2.5 The Type string,3 - 47,2.1 Namespaces,名字空间,4 - 46,Namespaces - functions,A namespace is a scope. C+ provides namespaces to prevent name conflicts.,Example: namespace mfc int inflag; / / no closing semicolon required namespace owl int inflag; / / no closing semicolon required,5 - 46,Namespaces - functions,A namespace is a mechanism for expressing logical grouping. That is, if some declarations logically belong together to some criteria(准则), they can be put in a common namespace to express that fact. That is, the namespace is the mechanism for supporting module programming paradigm(范型). 同一 namespace 中的声明在概念上属于同一个逻辑实体。,6 - 46,Namespaces - functions,7 - 46,Namespaces scope resolution operator,The scope resolution operator (域解析符): occurs between the namespaces name and the variable.,mfc : inflag = 3; / mfcs inflag owl : inflag = -823; / owls inflag,8 - 46,Namespaces using declaration,由于各个 namespace 之间经常会出现互相使用对方成员的情况,如果一使用就要约束,既繁琐又容易出错。因此,C+提供了几种“有限的统一”约束的机制。 A using declaration(using声明) applies a single item in the namespace.,namespace mfc int inflag; void g(int); / ,using mfc : inflag; inflag = 1; mfc:g(6);,9 - 46,Namespaces using directive,Using directive(using 指示)is equivalent to a using declaration for each item in a namespace. A using-directive makes names from a namespace available almost as if they had been declared outside their namespace.,using namespace mfc; inflag = 21; g(-66); owl:inflag = 341;,10 - 46,Namespaces using directive,A namespace is a scope. Thus, “namespace is a very fundamental and relatively simple concept. The larger a program is, the more useful namespaces are to express logical separations of its parts. Ideally(理想地), a namespace should 1 express a logically coherent (相关的) set of features. 2 not give users access to unrelated features, and 3 not impose a significant notational burden on users.,11 - 46,Namespaces - std,C+s std namespaces includes all of the standard libraries.,#include using namespace std; int main() cout “C+: one small step for the program, n“ “one giant leap for the programmern“; return 0; ,12 - 47,2.2 Introduction to C+ Input/Output,13 - 46,C+ Input/Output basic concepts,In C+, input/output is treated as a stream of consecutive bytes(连续的字节流). Thus C+ input/output is called stream input/output.,14 - 46,C+ Input/Output basic concepts,The header iostream must be included to use C+ standard input/output. cin: the standard input object cout: the standard output object cerr: the standard error object : operator/manipulator(操作符) used for input : operator/manipulator used for output,15 - 46,C+ Input/Output basic concepts,operator and is to skip white space before reading the next input item, even if the variable is of type char.,16 - 46,C+ Input/Output basic concepts,#include using namespace std; int main() int val, sum = 0; cout val ) sum += val; cout “Enter next number: “; cout “Sum of all values: “ sum n; return 0; ,if a value is read into val, cin val is true; otherwise, false.,17 - 46,C+ Input/Output Manipulators,Input and output can formatted using manipulators. Except for setw, a manipulator permanently changes the state of the stream to which it is applied. The Manipulators are divided into two classes, which are with arguments or without arguments. To use manipulators without arguments, the header iostream must be included. Manipulators with arguments require the header iomanip. The manipulator effect lasts only for the next input or output operation, and once the status of cout is changed by using a manipulator (other than setw), the status remains in effect until changed again.,18 - 46,C+ Input/Output Manipulators,19 - 46,C+ Input/Output Manipulators,20 - 46,Mixing C and C+ Input/Output,Using the C input/output library functions and the C+ class library in the same program can cause problems because reads and writes from the two libraries are not automatically synchronized(同步). If the two libraries are mixed, the function ios:sync_with_stdio( ) should be involved before doing any input or output.,21 - 47,2.3 Files,22 - 46,Files,The header file fstream must be included to use files. ifstream: to read from a file ofstream: to write to a file : operator/manipulator used to file read : operator/manipulator used to file write,23 - 46,Files,#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; infile.open( “income.in“ ); outfile.open( “tax.out“ ); while ( infile income ) if ( income cutoff ) tax = rate1 * income; else tax = rate2 * income; outfile “Income = “ income “ greenbacksn“ “Tax = “ tax “ greenbacksn“; infile.close(); outfile.close(); return 0; ,Example 2.3.1. The program reads incomes from the file incom.in until end-of file and writes the income and tax to the file tax.out.,24 - 46,Files Testing Whether Files Are Open,After opening a file, it is a good idea to check whether the file was successfully opened.,25 - 46,Files - Testing Whether Files Are Open,#include #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; infile.open( “income.in“ ); if (!infile) cerr “ Unable to open incom.in!n “; exit(0); outfile.open(“tax.out“ ); if (!outfile) cerr “ Unable to open tax.out!n “; exit(0); / infile.close(); outfile.close(); return 0; ,26 - 47,2.4 C+ Features,Casts (类型转换) Constants (常量) The Data Type bool (布尔类型) Enumerated Types (枚举类型) Defining variables (定义变量) Structures (结构),27 - 46,C+ Features Casts,C+ adds four new casts: static_cast const_cast dynamic_cast reinterpret_cast static_cast (一般的类型转换) The cast static_cast is the most commonly used cast. It is used to convert one data type to another and handles all “reasonable” casts, for example, converting int to float. The other casts are special-purpose cast.,Example. int x=20, y=30; average=(float)x/(float)y; /the C casting average=static_cast(x)/static_cast(y); /the C+ casting,28 - 46,C+ Features Casts,const_cast (将常量转换成非常量) As the name implies, const_cast is used to cast away constness. More precisely, it is used to cast a pointer to a const object to a pointer to a nonconst object.,Example. #include using namespace std; const int* find(int val, const int* t, int n); /function declaration int main() int a=2,4,6; /define array a and initialize the array int* ptr /ptr is a pointer to int (notice: it is a nonconst pointer) ptr=const_cast(find(4,a,3); /type casting if(ptr=0) cout“”not foundendl; else cout“found; value= ”ptrendl; return 0; const int* find (int val, const int* t, int n) int I; for(i=0;in;i+) if(ti=val) return /not found ,29 - 46,C+ Features Casts,reinterpret_cast (一种指针类型向另外一种指针类型的转换或整型向指针型的相互转换) The cast reinterpret_cast is used to convert a pointer of one type to a pointer of another type, to convert from a pointer type to an integer type, and to convert an integer type to pointer type. Because the effect is implementation dependent, reinterpret_cast should be used with caution.,Example. int main() int i; float f=-6.9072f; unsigned char* p=reinterpret_cast( ,30 - 46,C+ Features Casts,dynamic_cast (运行时带有类型检测的类型转换) The cast dynamic_cast is used for casting across or within inheritance hierarchies.,Ex. class B public: virtual void f() / ; class D:public B public: void m()/ ; int main() D* p=dynamic_cast (new B); /Error! if(p) p-m(); else cerr“”Not safe for p to point to a B”endl; / return 0; ,31 - 46,C+ Features The Data Type bool,In C, true is represented by nonzero, and false by zero. C+ added the integer type bool to represent the boolean values true and false. 布尔类型的名称:bool 布尔类型的值集: true, false 布尔类型的值可以承受算术、关系和逻辑运算。运算时,布尔类型的字面值(literal)true和false分别被转换成(机器中)整数类型的值 1 和 0,程序中的非零值代表true,0为false。,32 - 46,C+ Features The Data Type bool,布尔类型的主要用途: 定义那些表达逻辑运算(logical operations)结果的变量; 作为函数(用于测试某些条件a predicate)的返回类型。,void f(int a, int b) bool b1=a= =b; / ,bool is_open(File*); bool greater(int a, int b) return ab; ,33 - 46,C+ Features Enumerated Types,Enumeration是用户以枚举值集的方式定义的类型。 Enumeration值集中的每个值都是一个已命名的整数常量(Named integer constant),这个名字就是Enumeration类型的一个字面值(枚举符, enumerator)。,Notice that every enumeration is a distinct type.,34 - 46,C+ Features Enumerated Types,Enumeration类型的主要用途,是以易于理解、易于控制的方式表示程序中需要区分的状态,that is give both the user and the compiler a hint as to the intended use.,35 - 46,C+ Features Enumerated Types,一个Enumeration类型变量的取值范围为0(或-2n-1)+2n-1。,36 - 46,C+ Features Structures,C+ has modified Cs version of structures. In addition to data members, in C+ a struct can contain functions. 在C+中,结构被看成是一种特殊的类。(以后详细介绍),37 - 47,2.5 The Type string,38 - 46,The Type string,C+ furnishes(提供) the type string as an alternative to Cs null-terminated arrays of char. By using string, the programmer does not have to be concerned about storage allocation or about handling the annoying null terminator.,39 - 46,The Type string,40 - 46,The Type string erase method,#include #include using namespace std; int main() string s = “Ray Dennis Steckler“; s.erase(4, 7); / 从第4个字符开始连续删除7个字符 cout s n; / s= “Ray Steckler“; s.erase(4); / 删除从第4个字符开始的所有字符 cout s n; / s= “Ray“; return 0; ,The function erase removes a substring from a string.,41 - 46,The Type string insert method,The function insert inserts a string at specified position.,#include #include using namespace std; int main() string s1 = “Ray Steckler“; string s2 = “Dennis “; s1.insert(4, s2); / 将字符串s2插入到字符串s1的第4个字符之后 cout s1 n; / s1=“Ray Dennis Steckler“ cout s2

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论