Download presentation
Presentation is loading. Please wait.
Published byBeryl Clark Modified over 9 years ago
1
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved. 1 - 32 Object-Oriented Programming
2
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved. 2 - 32 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
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved. 3 - 32 2.1 Namespaces 名空间
4
SEI 4 - 32 Namespaces C++ provides namespaces to prevent name conflicts. namespace mfc { int inflag; // … } // no closing semicolon required namespace owl { int inflag; // … } // no closing semicolon required namespace mfc { int inflag; // … } // no closing semicolon required namespace owl { int inflag; // … } // no closing semicolon required
5
SEI 5 - 32 Namespaces – scope resolution operator The scope resolution operator :: occurs between the namespaces name and the variable. mfc :: inflag = 3; // mfc ’ s inflag owl :: inflag = -823; // owl ’ s inflag mfc :: inflag = 3; // mfc ’ s inflag owl :: inflag = -823; // owl ’ s inflag
6
SEI 6 - 32 Namespaces – using declaration A using declaration ( using 声明) applies a single item in the namespace. namespace mfc { int inflag; void g(int); // … } namespace mfc { int inflag; void g(int); // … } using mfc :: inflag; inflag = 1; mfc::g(6); using mfc :: inflag; inflag = 1; mfc::g(6);
7
SEI 7 - 32 Namespaces – using directive Using directive ( using 指示) is equivalent to a using declaration for each item in a namespace. using namespace mfc; inflag = 21; g(-66); owl::inflag = 341; using namespace mfc; inflag = 21; g(-66); owl::inflag = 341;
8
SEI 8 - 32 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 programmer\n"; return 0; } #include using namespace std; int main() { cout << "C++: one small step for the program, \n" << "one giant leap for the programmer\n"; return 0; }
9
SEI 9 - 32 Conversion of C Header The 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 cstdlib stdio.h cstdio ctype.h cctype …
10
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved. 10 - 32 2.2 Introduction to C++ Input/Output C++ Input/Output – Manipulators (自己看)
11
SEI 11 - 32 C++ Input/Output In C++, input/output is treated as a stream of consecutive( 连续的 ) bytes. Thus C++ input/output is called stream input/output.
12
SEI 12 - 32 C++ Input/Output The header iostream must be included to use C++ standard input/output. cin: the standard input cout: 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
SEI 13 - 32 C++ Input/Output operator >> 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
SEI 14 - 32 C++ Input/Output #include 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; } #include 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.
15
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved. 15 - 32 2.3 Files
16
SEI 16 - 32 Files The header file fstream must be included to use files. ifstream: to read from a file ofstream: to write to a file >>: used to file read <<: used to file write
17
SEI 17 - 32 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 << " greenbacks\n" << "Tax = " << tax << " greenbacks\n"; } infile.close(); outfile.close(); return 0; } #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 << " greenbacks\n" << "Tax = " << tax << " greenbacks\n"; } infile.close(); outfile.close(); return 0; }
18
SEI 18 - 32 Files – Testing Whether Files Are Open After opening a file, it is a good idea to check whether the file was successfully opened.
19
SEI 19 - 32 Files - Testing Whether Files Are Open #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; } #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; }
20
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved. 20 - 32 2.4 C++ Features Casts (类型转换,不用看) Constants (自己看) The Data Type bool Enumerated Types (自己看) Defining variables (自己看) Structures
21
SEI 21 - 32 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
SEI 22 - 32 C++ Features – Structures C++ has modified C’s version of structures. In addition to data members, in C++ a struct can contain functions. 在 C++ 中,结构被看成是一种特殊的类。(以后详细介 绍)
23
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved. 23 - 32 2.5 The Type string
24
SEI 24 - 32 The Type string C++ 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
SEI 25 - 32 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 substrstring s1 = "abcdergrehj"; string s2 = s1.substr(4,6); Searching find Comparing == 、 != 、 、 >=
26
SEI 26 - 32 The Type string - erase #include 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; } #include 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
SEI 27 - 32 The Type string - insert The function insert inserts a string at specified position. #include 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; } #include 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
SEI 28 - 32 The Type string - replace The function replace replaces a substring with a specified string.
29
SEI 29 - 32 The Type string - replace #include 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; } #include 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
SEI 30 - 32 The Type string - swap #include 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; } #include 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
SEI 31 - 32 The Type string - find The function find is used to search a string for a substring. #include 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; } #include 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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.