1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 1 Chapter 9 Classes & Objects type string is.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 10 Class Design.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L11 (Chapter 20) Lists, Stacks,
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Standard Template Library Ming.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
計算機概論實習 How to Use string Template class basic_string String manipulation (copying, searching, etc.) typedef basic_string string; Also typedef.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 26 Implementing Lists, Stacks,
Chapter 9 Objects and Classes
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
February 14, 2005 Characters, Strings and the String Class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Objects and Classes Mostafa Abdallah
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++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
String Class Mohamed Shehata 1020: Introduction to Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
C++ Programming Basics
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
An Array Type For Strings. Two ways to represent strings – i.e. “Hello” cstring An array with base type char Older way of processing strings Null character.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter 9 Objects and Classes §9.1 Introduction §9.2 Defining Classes and Objects §9.3 Access Objects §9.4 Separating Declaration from Implementation §9.5.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
13. string Operations Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Accessing String Elements.
 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Enumerated Types and Strings Types.
Operator Overloading; Class string
Chapter 4 Loops Case Studies
10.1 Character Testing.
String class and its objects
Chapter 9 Objects and Classes
Introduction to Programming
C++ Programming Lecture 20 Strings
Today’s Objectives 28-Jun-2006 Announcements
Presentation transcript:

1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Classes & Objects type string is a natural lead-in to OOP, since strings act like character arrays and are, in fact, objects (a class type)‏

2 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X Objects in Brief Object ::= a domain entity (p. 274)‏ student button circle Coke machine string defined by name, state, & behavior 2

3 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X Objects in Brief name ::= the usual rules for identifiers state ::= data members (state variables)‏ behavior ::= function members (methods)‏ alternately-- object ::= named set of properties/members data members member functions 3

4 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X Objects in Brief name ::= the usual rules for identifiers state ::= data members (state variables)‏ behavior ::= function members (methods)‏ alternately-- object ::= named set of properties/members data members member functions 4

5 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X Objects in Brief Example: Circle Object Example: Circle Object Example: Circle Object State Variable: radius Methods: setRadius(), getArea(), getDiameter()‏ int main(){ Circle c; c.setRadius(5); cout << c.getArea()‏ << c.getDiameter(); 5

6 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X Implementation in C++ Objects are defined using a class The class is a blueprint for creating objects objects are created by making an instance of the class Circle is a class type c is an instance of the class metaphor: classes are to objects as apple pie recipes are to apple pies 6

7 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 7 Case Study: The Standard Class string (  9.8)‏ 9.8)‏ 9.8)‏ > 100 member functions (p. 284)‏

8 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X The Standard Class string cstring ::= NUL-terminated char array →  low-level;  error-prone; not a type type string avoids problems associated with cstrings provides additional operations (!)‏ 8

9 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 9 String Ops copy (=)‏ append (+)‏ compare (<, <=, ==,...)‏ #include int main(){ string s1, s2(“hot”), s3(“dog”), s4; s1 = s2 + ‘ ‘ + s3 + “!”; s4 = s1; cout << s1 << s4 << endl; }

10 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 10 Appending a String You can use several overloaded functions to add new contents to a string. For example, see the following code: string s1("Welcome"); s1.append(" to C++"); // appends " to C++" to s1 cout << s1 << endl; // s1 now becomes Welcome to C++ string s2("Welcome"); s2.append(" to C and C++", 0, 5); // appends " to C" to s2 cout << s2 << endl; // s2 now becomes Welcome to C string s3("Welcome"); s3.append(" to C and C++", 5); // appends " to C" to s3 cout << s3 << endl; // s3 now becomes Welcome to C string s4("Welcome"); s4.append(4, 'G'); // appends "GGGG" to s4 cout << s4 << endl; // s4 now becomes WelcomeGGGG

11 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 11 Assigning a String You can use several overloaded functions to assign new contents to a string. For example, see the following code: string s1("Welcome"); s1.assign("Dallas"); // assigns "Dallas" to s1 cout << s1 << endl; // s1 now becomes Dallas string s2("Welcome"); s2.assign("Dallas, Texas", 0, 5); // assigns "Dalla" to s2 cout << s2 << endl; // s2 now becomes Dalla string s3("Welcome"); s3.assign("Dallas, Texas", 5); // assigns "Dalla" to s3 cout << s3 << endl; // s3 now becomes Dalla string s4("Welcome"); s4.assign(4, 'G'); // assigns "GGGG" to s4 cout << s4 << endl; // s4 now becomes GGGG

12 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 12 Functions at, clear, erase, and empty You can use the at(index) function to retrieve a character at a specified index, clear() to clear the string, erase(index, n) to delete part of the string, and empty() to test if a string is empty. For example, see the following code: string s1("Welcome"); cout << s1.at(3) << endl; // s1.at(3) returns c cout << s1.erase(2, 3) << endl; // s1 is now Weme s1.clear(); // s1 is now empty cout << s1.empty() << endl; // s1.empty returns 1 (means true)‏

13 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 13 Comparing Strings Often, in a program, you need to compare the contents of two strings. You can use the compare function. This function works in the same way as the C-string strcmp function and returns a value greater than 0, 0, or less than 0. For example, see the following code: string s1("Welcome"); string s2("Welcomg"); cout << s1.compare(s2) << endl; // returns -2 cout << s2.compare(s1) << endl; // returns 2 cout << s1.compare("Welcome") << endl; // returns 0

14 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 14 String Operators