Object-Oriented Programming 1 - 32 PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition Copyright 2007 © SEI, All rights reserved. SEI 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 2 - 32 PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition Copyright 2007 © SEI, All rights reserved. SEI 2.1 Namespaces 名空间 3 - 32 PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition Copyright 2007 © SEI, All rights reserved. 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 4 - 32 SEI Namespaces – scope resolution operator The scope resolution operator :: occurs between the namespaces name and the variable. mfc :: inflag = 3; owl :: inflag = -823; 5 - 32 // mfc’s inflag // 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); //… using mfc :: inflag; inflag = 1; mfc::g(6); } 6 - 32 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; 7 - 32 SEI Namespaces - std C++’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; } 8 - 32 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 … 9 - 32 SEI 2.2 Introduction to C++ Input/Output C++ Input/Output – Manipulators(自己看) 10 - 32 PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition Copyright 2007 © SEI, All rights reserved. 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. 11 - 32 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 12 - 32 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. 13 - 32 SEI C++ Input/Output #include <iostream> using namespace std; int main() { int val, sum = 0; if a value is read into cout << "Enter next number: "; val, cin >> val is while( cin >> val ) { true; otherwise, false. sum += val; cout << "Enter next number: "; } cout << "Sum of all values: " << sum << '\n'; return 0; } 14 - 32 SEI 2.3 Files 15 - 32 PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition Copyright 2007 © SEI, All rights reserved. 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 16 - 32 SEI #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; } Files 17 - 32 SEI Files – Testing Whether Files Are Open After opening a file, it is a good idea to check whether the file was successfully opened. 18 - 32 SEI #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; } Files - Testing Whether Files Are Open 19 - 32 SEI 2.4 C++ Features Casts(类型转换,不用看) Constants(自己看) The Data Type bool Enumerated Types (自己看) Defining variables (自己看) Structures 20 - 32 PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition Copyright 2007 © SEI, All rights reserved. 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. 21 - 32 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++中,结构被看成是一种特殊的类。(以后详细介 绍) 22 - 32 SEI 2.5 The Type string 23 - 32 PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2nd Edition Copyright 2007 © SEI, All rights reserved. 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. 24 - 32 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; Assignment = string s1, s2; Concatenation + string s1 = "abcd", s2 = "efg"; string s3 = s1 + s2; Modify String erase、insert、 replace、swap Extract substring substr Searching find Comparing ==、!=、<、<=、 >、>= 25 - 32 cin >> s1; s1 = "abcd"; s2 = s1; string s1 = "abcdergrehj"; string s2 = s1.substr(4,6); SEI The Type string - erase The function erase removes a substring from a string. #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; } 26 - 32 SEI The Type string - insert The 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; } 27 - 32 SEI The Type string - replace The function replace replaces a substring with a specified string. 28 - 32 SEI 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; } 29 - 32 SEI 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; } 30 - 32 SEI The Type string - find The 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; } 31 - 32 SEI