SEI PowerPoint Presentation for LiuHui, Object-Oriented Programming for C++, 2 nd Edition Copyright 2007 © SEI, All rights reserved. 1 - 32 Object-Oriented.

Slides:



Advertisements
Similar presentations
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
Advertisements

CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
計算機概論實習 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.
Chapter 5: Loops and Files.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
SECTION 2 C++ Language Basics. Strategies for learning C++ Focus on concepts and programming techniques. (Don’t get lost in language features) Learn C++
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Introduction to C++ Systems Programming.
1 Chapter 9 Scope, Lifetime, and More on Functions.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
1 Object oriented programming What is Class? What is Object? - From object-oriented point of view - Class is a user-defined data type which contains relevant.
1 Chapter objectives To provide a standardized library for character string handling Overloaded operators and member functions as a facility 補充 : C++ string.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
COP 3530 Data Structures & Algorithms Discussion Session 3.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 21 - C++ Stream Input/Output Basics Outline 21.1Introduction 21.2Streams Iostream Library.
String Class. C-style and C++ string Classes C-style strings, called C-strings, consist of characters stored in an array ( we’ll look at them later) C++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
1 Simple File I/O Chapter 11 Switch Statement Chapter 12.
Loops and Files. 5.1 The Increment and Decrement Operators.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 Chapter 9 Scope, Lifetime, and More on Functions.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
第二章 线性表 第二节 线性链表 5. 线性表的链式表示 顺序表的优点是可以随机选取表中元素 缺点是插入删除操作复杂。 用指针将互不相连的内存结点串成的 线性表叫线性链表。 结点 node 由一个数据元素域,一个或几个 指针域组成。单链表的结点只有一个指针域。
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
C++ Programming Michael Griffiths Corporate Information and Computing Services The University of Sheffield
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Chapter 1.2 Introduction to C++ Programming
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CS Computer Science IA: Procedural Programming
Introduction to C++ Systems Programming.
Problems With Character Arrays
Basic Elements of C++.
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? how is.
Basic Elements of C++ Chapter 2.
Chapter 9 Scope, Lifetime, and More on Functions
String class and its objects
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Review of Strings which include file needs to be used for string manipulation? what using statement needs to be included for string manipulation? how is.
Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Standard Input/Output Stream
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Programming with Data Files
C++ Programming Lecture 8 File Processing
Today’s Objectives 28-Jun-2006 Announcements
Lecture 9 Files Handling
Presentation transcript:

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; }