sei powerpoint presentation for liuhui, object-oriented programming for c++, 2 nd edition copyright...
DESCRIPTION
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved Namespaces 名空间TRANSCRIPT
PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition
Copyright 2007 © SEI, All rights reserved.1 - 32 SEI
Object-Oriented Object-Oriented ProgrammingProgramming
PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition
Copyright 2007 © SEI, All rights reserved.2 - 32 SEI
Chapter2 From C to C++Chapter2 From C to C++
2.1 Namespaces2.2 Introduction to C++ Input/Output2.3 Files2.4 C++ Features2.5 The Type string
PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition
Copyright 2007 © SEI, All rights reserved.3 - 32 SEI
2.1 Namespaces2.1 Namespaces
名空间
4 - 32 SEI
NamespacesC++ provides namespaces to prevent name conflicts.
namespace mfc { int inflag; // …} // no closing semicolon requirednamespace owl { int inflag; // …} // no closing semicolon required
5 - 32 SEI
Namespaces – scope resolution operator
The scope resolution operator :: occurs between the namespaces name and the variable.
mfc :: inflag = 3; // mfc’s inflagowl :: inflag = -823; // owl’s inflag
6 - 32 SEI
Namespaces – using declaration
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);
7 - 32 SEI
Namespaces – using directiveUsing directive (using 指示) is equivalent to a using declaration for each item in a namespace.using namespace mfc;
inflag = 21;g(-66);owl::inflag = 341;
8 - 32 SEI
Namespaces - stdC++’s std namespaces includes all of the standard libraries.
#include <iostream>using namespace std;
int main() { cout << "C++: one small step for the program, \n" << "one giant leap for the programmer\n"; return 0;}
9 - 32 SEI
Conversion of C HeaderThe definitions, declarations, and so on in nonstandard C++ “.h” headers typically are not placed in namespaces.
The standard C header files have been renamed: .h is dropped and a c is prefixed.
stdlib.h cstdlibstdio.h cstdioctype.h cctype…
PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition
Copyright 2007 © SEI, All rights reserved.10 - 32 SEI
2.2 Introduction to C++ 2.2 Introduction to C++ Input/OutputInput/Output
C++ Input/Output – Manipulators (自己看)
11 - 32 SE
I
C++ Input/OutputIn C++, input/output is treated as a stream of consecutive(连续的 ) bytes.Thus C++ input/output is called stream input/output.
12 - 32 SE
I
C++ Input/OutputThe header iostream must be included to use C++ standard input/output.
cin: the standard inputcout: the standard output (buffered)cerr: the standard error (not buffered)
Two Operators, both operators recognize the data type supplied, so no format string (like that required for printf/scanf) is necessary.
>>: used for input<<: used for output
13 - 32 SE
I
C++ Input/Outputoperator >> and << recognize the data type supplied, so no format string is necessary.The default action of the input operator >> is to skip white space before reading the next input item, even if the variable is of type char.
14 - 32 SE
I
C++ Input/Output#include <iostream>using namespace std;int main() { int val, sum = 0; cout << "Enter next number: "; while( cin >> 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.
PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition
Copyright 2007 © SEI, All rights reserved.15 - 32 SEI
2.3 Files2.3 Files
16 - 32 SE
I
FilesThe header file fstream must be included to use files.
ifstream: to read from a fileofstream: to write to a file>>: used to file read<<: used to file write
17 - 32 SE
I
Files#include <fstream>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 << " greenbacks\n" << "Tax = " << tax << " greenbacks\n"; } infile.close(); outfile.close(); return 0;}
18 - 32 SE
I
Files – Testing Whether Files Are Open
After opening a file, it is a good idea to check whether the file was successfully opened.
19 - 32 SE
I
Files - Testing Whether Files Are Open
#include <fstream>#include <iostream>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;}
PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition
Copyright 2007 © SEI, All rights reserved.20 - 32 SEI
2.4 C++ Features2.4 C++ Features
Casts (类型转换,不用看)Constants (自己看)The Data Type boolEnumerated Types (自己看)Defining variables (自己看)Structures
21 - 32 SE
I
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.
22 - 32 SE
I
C++ Features – Structures C++ has modified C’s version of structures.In addition to data members, in C++ a struct can contain functions.在 C++ 中,结构被看成是一种特殊的类。(以后详细介绍)
PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition
Copyright 2007 © SEI, All rights reserved.23 - 32 SEI
2.5 The Type string2.5 The Type string
24 - 32 SE
I
The Type stringC++ furnishes the type string as an alternative to C’s 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.
25 - 32 SE
I
The Type string功能 String 中的方法 举例
String Length
length() string s1; int size = s1.length();
Output Strings
cout<< string s1 = "abcd"; cout << s1 ;
Input Strings
cin>> string s1; cin >> s1;
Assignment = string s1, s2; s1 = "abcd"; s2 = s1;
Concatenation
+ string s1 = "abcd", s2 = "efg";string s3 = s1 + s2;
Modify String
erase 、 insert 、replace 、 swap
Extract substring
substr string s1 = "abcdergrehj";string s2 = s1.substr(4,6);
Searching findComparing == 、 != 、 < 、 <= 、> 、 >=
26 - 32 SE
I
The Type string - erase
#include <iostream>#include <string>using namespace std;
int main() { string s = "Ray_Dennis+Steckler"; s.erase(4, 7); // 从第 4 个字符开始连续删除 7 个字符 (Ray_Steckler) cout << s << '\n'; s.erase(4); // 删除从第 4 个字符开始的所有字符 (Ray_) cout << s << '\n'; return 0;}
The function erase removes a substring from a string.
27 - 32 SE
I
The Type string - insertThe function insert inserts a string at specified position.#include <iostream>#include <string>using namespace std;
int main() { string s1 = "Ray Steckler"; string s2 = "Dennis "; s1.insert(4, s2); // 将字符串 s2 插入到字符串 s1 的第 4 个字符之后 cout << s1 << '\n'; cout << s2 << '\n'; return 0;}
28 - 32 SE
I
The Type string - replaceThe function replace replaces a substring with a specified string.
29 - 32 SE
I
The Type string - replace#include <iostream>#include <string>using namespace std;
int main() { string s1 = "Ray Dennis Steckler"; string s2 = "Fran"; s1.replace(4, 6, s2); // 用字符串 s2 替换字符串 s1 中从第 4 个字符
// 开始的连续 6 个字符 cout << s1 << '\n'; cout << s2 << '\n'; return 0;}
30 - 32 SE
I
The Type string - swap#include <iostream>#include <string>using namespace std;
int main() { string s1 = "Ray Dennis Steckler"; string s2 = "Fran"; s1.swap(s2); // 将字符串 s1 和字符串 s2 的值互换 cout << s1 << '\n'; cout << s2 << '\n'; return 0;}
31 - 32 SE
I
The Type string - findThe function find is used to search a string for a substring.#include <iostream>
#include <string>using namespace std;int main() { string s1 = "Ray Dennis Steckler"; string s2 = "Dennis"; int f = s1.find(s2); // 字符串 s1 中是否出现了字符串 s2 if ( f < s1.length() ) cout << "Found at index: " << f << '\n'; else cout << "Not found\n"; return 0;}