Chapter 7 Continued Arrays & Strings. Strings as Class Members Strings frequently appear as members of classes. The next example, a variation of the objpart.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Classes and Data Abstraction Lecture 9. Objects Models of things in the real world Defined in classes  Class name is the object name Example: Library.
Chapter 7: User-Defined Simple Data Types, Namespaces, and the string Type.
Objectives In this chapter, you will:
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
Chapter 14: Overloading and Templates
VBA Modules, Functions, Variables, and Constants
Operator Overloading in C++ Systems Programming. Systems Programming: Operator Overloading 22   Fundamentals of Operator Overloading   Restrictions.
Chapter 13: Overloading.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 15: Operator Overloading
Operator Overloading in C++
Review of C++ Programming Part II Sheng-Fang Huang.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
UNFORMATTED INPUT OUTPUT. Topics to be discussed……………….. overloaded operators >> and and
1 Chapter 10 Characters, Strings, and the string class.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Strings Representation and Manipulation. Objects Objects : Code entities uniting data and behavior – Built from primitive data types.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Homework #4: Operator Overloading and Strings By J. H. Wang May 8, 2012.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
FILE HANDLING IN C++.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
COP 3530 Data Structures & Algorithms Discussion Session 3.
9-1 Learning Objectives  An Array Type for Strings  C-Strings.
 2008 Pearson Education, Inc. All rights reserved Operator Overloading.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Characters, Strings, And The string Class Chapter 10.
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++
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
1 String Processing CHP # 3. 2 Introduction Computer are frequently used for data processing, here we discuss primary application of computer today is.
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Chapter 16 C++ Strings By C. Shing ITEC Dept Radford University.
Chapter 9 Strings. Learning Objectives An Array Type for Strings – C-Strings Character Manipulation Tools – Character I/O – get, put member functions.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
Programming Fundamentals. Today’s Lecture Multidimensional Arrays Arrays as Class Member Data Arrays of Objects C-Strings.
Chapter 9: Completing the Basics. In this chapter, you will learn about: – Exception handling – Exceptions and file checking – The string class – Character.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Chapter Characters, Strings, and the string class 10.
 Constants A constant is a fixed value of a data type that cannot be changed Integer Constants Whole numbers → Do not have decimal points Examples: 83.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Programming Fundamentals. Today’s Lecture Array Fundamentals Arrays as Class Member Data Arrays of Objects C-Strings The Standard C++ string Class.
Chapter 7 Continued Arrays & Strings. Arrays of Structures Arrays can contain structures as well as simple data types. Let’s look at an example of this,
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Object-Oriented Concepts
SAS in Data Cleaning.
Topics Introduction to File Input and Output
C++ STRINGS string is part of the Standard C++ Library
Operator Overloading; String and Array Objects
Operator Overloading; String and Array Objects
Programming with ANSI C ++
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Topics Introduction to File Input and Output
DATA TYPES AND OPERATIONS
character manipulation
Array Fundamentals A simple example program will serve to introduce arrays. This program, REPLAY, creates an array of four integers representing the ages.
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Chapter 7 Continued Arrays & Strings

Strings as Class Members Strings frequently appear as members of classes. The next example, a variation of the objpart program from chapter 6, uses a C-string to hold the name of the widget part. Let’s look at “strpart.cpp”.

A User-Defined String Type There are some problems with C-Strings as they are normally used in C++. One problem is that you cannot use the following expression: strDest = strScr; Let’s try to create our own string data type.

A User-Defined String Type If we define our own string type, using a C++ class, we can use this assignment statement. In our next example, “strobj.cpp”, we will create a class called String (not to be confused with the “real” string class built into C++).

The Standard C++ String Class Standard C++ includes a new class called string. This class improves on the traditional C-string in many ways. One way it improves is that you no longer need to worry about creating an array of the right size to hold string variables. The string class assumes all the responsibility for memory management.

The Standard C++ string Class Also, the string class allows the use of overloaded operators, so you can concatenate string objects with the + operator: s3 = s1 + s2; This new class is more efficient and safer to use than C-strings were. In most cases this is the preferred way to use strings in C++.

Defining and Assigning string Objects You can define a string object in several ways. You can use a constructor with no arguments, creating an empty string, you can also use a one argument constructor, where the argument is a C-string constant. Our next example shows this, “sstrass.cpp”.

Input/Output with string Objects Input and output are handled in a similar way to that of C-strings. The > operators are overloaded to handle string objects; and a function getline() handles input that contains embedded blanks or multiple lines. The next example, “sstrio.cpp” shows this.

Finding string Objects The string class includes a variety of member functions for finding strings and substrings in string objects. The next example, “sstrfind.cpp” shows this.

Modifying string Objects There are various ways to modify string objects. The next example shows the member functions erase(), replace(), and insert() at work. Let’s look at “sstrching.cpp”

Comparing string Objects You can overload operators or the compare() function to compare string objects. These will show whether strings are the same, or whether they precede or follow one another alphabetically. The program “sstrcom” show this.

Accessing Characters in string Objects You can access individual characters within a string object in several ways. In our next example we’ll show access using the at() member function. You can also use the overloaded [ ] operator, which makes the string object look like an array.

Accessing Characters in string Objects But the [ ] operator doesn’t warn you if you attempt to access a character that’s out of bounds. It is safer to use the at() function which causes the program to stop if you use an out-of-bounds index. Let’s look at “sstrchar.cpp”.