TAILIEUCHUNG - Absolute C++ (4th Edition) part 39

Absolute C++ (4th Edition) part 39. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | The Standard Class string 385 Display A string Object Can Behave Like an Array 1 Demonstrates using a string object as if it were an array. 2 include iostream 3 include string 4 using namespace std 5 int main 6 string firstName lastName 8 cout Enter your first and last name n 9 cin firstName lastName 10 cout Your last name is spelled n 11 int i 12 for i 0 i i 13 14 cout lastName i 15 lastName i - 16 17 cout endl 18 for i 0 i i 19 cout lastName i Places a - under each letter. 20 cout endl 21 cout Good day firstName endl return 0 23 Sample Dialogue Enter your first and last names John Crichton Your last name is spelled C r i c h t o n Good day John an array but the length member function makes it behave like a partially filled array that automatically keeps track of how many positions are occupied. The array square brackets when used with an object of the class string do not check for illegal indexes. If you use an illegal index that is an index that is greater than or equal to the length of the string in the object the results are unpredictable but are bound to be bad. You may just get strange behavior without any error message that tells 386 Strings you that the problem is an illegal index value. There is a member function named at that does check for an illegal index value. The member function named at behaves basically the same as the square brackets except for two points You use function notation with at so instead of a i you use i and the at member function checks to see if i evaluates to an illegal index. If the value of i in i is an illegal index you should get a runtime error message telling you what is wrong. In the following code fragment the attempted access is out of range yet it probably will not produce an error message although it will be accessing a nonexistent indexed variable string str Mary cout str 6 endl The next example however will cause the program to terminate abnormally so that you at least know

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.