SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved Object-Oriented Programming
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved Chapter2 From C to C Namespaces 2.2 Introduction to C++ Input/Output 2.3 Files 2.4 C++ Features 2.5 The Type string
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved Namespaces 名空间
SEI 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
SEI 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
SEI 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);
SEI 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;
SEI 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; }
SEI 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 …
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved Introduction to C++ Input/Output C++ Input/Output – Manipulators (自己看)
SEI C++ Input/Output In C++, input/output is treated as a stream of consecutive( 连续的 ) bytes. Thus C++ input/output is called stream input/output.
SEI 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
SEI 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.
SEI 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.
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved Files
SEI 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
SEI 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; }
SEI Files – Testing Whether Files Are Open After opening a file, it is a good idea to check whether the file was successfully opened.
SEI 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; }
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved C++ Features Casts (类型转换,不用看) Constants (自己看) The Data Type bool Enumerated Types (自己看) Defining variables (自己看) Structures
SEI 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.
SEI C++ Features – Structures C++ has modified C’s version of structures. In addition to data members, in C++ a struct can contain functions. 在 C++ 中,结构被看成是一种特殊的类。(以后详细介 绍)
SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved The Type string
SEI 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.
SEI 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 == 、 != 、 、 >=
SEI 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.
SEI 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; }
SEI The Type string - replace The function replace replaces a substring with a specified string.
SEI 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; }
SEI 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; }
SEI 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; }