Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at

Slides:



Advertisements
Similar presentations
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.
Advertisements

What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
1 Using Classes and Working With Class Interfaces November 20, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Working with Strings Lecture 2 Hartmut Kaiser
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
CSC 212 Object-Oriented Programming and Java Part 1.
February 14, 2005 Characters, Strings and the String Class.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Fundamental Programming: Fundamental Programming Introduction to C++
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
1 Warm-Up Problem Just like with primitive data types (int, double, etc.), we can create arrays of objects. ex: bankAccount employees[100]; Problem: It’s.
Classes: Member Functions and Implementation November 22, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
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++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CMSC 202 Lesson 2 C++ Primer. Warmup Create an array called ‘data’ Define a constant called DATA_SIZE with value 127 Write a loop to fill the array with.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
C++ String Class nalhareqi©2012. string u The string is any sequence of characters u To use strings, you need to include the header u The string is one.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
Unit 3 Lesson 5 Strings and the String Class Mr. Dave Clausen (modifications from the textbook)
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
Strings. Using strings as abstract value A string is a sequence of characters e.g. “Hello, World!” Early version of C++ followed the older C language.
Library Functions. CSCE 1062 Outline  cmath class library functions {section 3.2}  iomanip class library functions {section 8.5}  string class library.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Lecture Overview Linked List quiz Finish last class' topic:
CMSC 202 Lesson 2 C++ Primer.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Announcements 2nd homework is due this week Wednesday (October 18)
Summary of what we learned so far
Learning Objectives String Class.
Announcements 3rd homework is due this week Wednesday (March 15)
Wednesday 09/23/13.
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
From C to C++: Summary of weeks 1 - 4
Today’s Objectives 28-Jun-2006 Announcements
Programming Strings.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 6: Strings
Announcements HW1 is due TODAY.
Presentation transcript:

Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at Prepared by Doug Hogan

2 Announcements  Late drop deadline is Wednesday, 11/20  Review scores  If you do not earn a C or better, you’ll need to retake the class for the major.  4-credit D – GPA impact?  Dr. Quick has normal office hours this week.  HW5.dat was modified on Wednesday, 11/13 (leading 0 in an ID)  HW5 due next Monday

3 Overview  String class  Headers  Creating strings  Manipulating and comparing strings  Motivation for Object Oriented Programming  Strings as objects  Terminology and theory  Problems

4 The string type  Alternative to character arrays  Hides many details  Easier to manipulate  Required headers  #include  #include  using namespace std;  string is part of the C++ standard library

5 Declaring strings  Uninitialized:  Like primitive data types  e.g. string myString;  Can then use assignment operator  e.g. myString = “this is a string”;  Initialized:  Use string keyword, name, and initial value in parentheses  e.g. string myString(“a string”);

6 Input/Output  cin and cout  cin stops at whitespace  getline can be used for reading in strings with spaces included:  getline(stream, receivingString);  example:  cout << “Enter a string”; getline(cin, str1);

7 Manipulating Characters  Exactly the same as with arrays of characters!  Use an index in brackets to get or manipulate that character.  string myString(“a string”);  cout << myString[0];  prints “a”  myString[0] = “A”;  changes myString to “A string”

8 Exercises  Create a string called testString that is initially “It’s 8 a.m. and he’s expecting me to think”  string testString(“It’s 8 a.m. and he’s expecting me to think”);  Change the “8” to a “9.”  testString[5] = ‘9’;  Output the string  cout << testString;

9 Operators and strings  The string class lets you use the following operators:  Assignment: =  Comparison: >=, >, =, >, <, <=  Equality: ==, !=  Concatenation: +  Example:  if(string1 < string2) { cout << string1 << “ is before “ << string2 << endl; }

10 Problem (should be familiar…)  Suppose you have these declarations:  string str1 = “I love ”;  string str2 = “computer programming!”;  Create a string called str3 from these two strings that reads “I love computer programming!”  string str3 = str1 + str2;

11 A bit of terminology before the fun part…  We’ll call string variables objects.  We can operate on strings with functions  use dot notation  e.g. objectName.operation();  said to be sending a message to the string object

12 length() message  length( ) returns the length of the string it’s called on  ex:  string hello(“Hello”);  cout << hello.length() << endl;  prints 5  Don’t forget the parentheses!!  Must give the string object, then the dot operator!!

13 Practice  Given  string noun;  cin >> noun;  Output the length of noun.  cout << noun.length();

14 find() message  find( ) takes a string as an argument  returns the index where the argument is found in the object it’s called on  ex:  string hello(“Hello”);  cout << hello.find(“ll”); << endl;  prints 2  if the string isn’t found, find( ) returns -1

15 Substrings: substr() message  Takes two integer arguments:  first is starting character  second is length  returns a substring of the given length  string hello(“Hello World”);  cout << hello.substr(6, 5); << endl;  prints “World”  goes up to string’s length if 2 nd argument is too short

16 Problems  Given string s1(“abcdefghi”);  string s2(s1.substr(4, 3)); What is stored in s2?  Answer: efg  Write a line of code to store the location of the letter “d” from s1 in the following int:  int d;  Answer: d = s1.find(“d”);

17 Given string s(“Any string”); Give the result of each message or what is wrong with it.  length(s)  s.length  s(length)  s.length()  find(“Any”)  s.find(“ “)  s.substr(2)  s.substr(2, 5)  s.substr(“tri”)  s.find(“tri”) Modified Self-Check 4-8 from: Mercer, Rick. Computing Fundamentals with C++. Wilsonville, OR: Franklin, 1999.

18 Given string s(“Any string”); Give the result of each message or what is wrong with it.  length(s)  no dot notation  length takes no argument  s.length  no parentheses  s(length)  parentheses misplaced  s.length()  10  find(“Any”)  no object  s.find(“ “) 33  s.substr(2)  not enough arguments  s.substr(2, 5)  y str  s.substr(“tri”)  wrong arguments  s.find(“tri”) 55 Modified Self-Check 4-8 from: Mercer, Rick. Computing Fundamentals with C++. Wilsonville, OR: Franklin, 1999.

19 More string Messages  The ones we’ve discussed are the most useful  See page 593 of your book for more

20 Motivation for classes  Object-Oriented Programming (OOP)  Package together a set of related data and operations ( encapsulation )  Define a class (abstract data type), or a new data type with its operations  One instance of a class is called an object  The data and operations of a class are called its members.  string is an example of a class

21 Access rights in OOP  Classes are similar to structs  Add the notion of access rights  class member data and operations can be  public – accessible to anyone  private – accessible only to the object  usually  data are private  operations are public

22 Information Hiding  Client (user) has only the information needed to use the software.  Implementer has only the information needed to implement the software.  Communication through pre/post. Graphic from: Headington, Mark A. and David Riley. Data Abstraction and Structures using C++. Lexington, MA:Heath, 1994.

23 Information Hiding Applied  Client can access the public methods of an object  Sending a message  Methods can access private data

24 An example of a class: bankAccount  Data:  name  balance  Operations:  create an account  withdraw  deposit  check balance

25 Summary  string class  created with string keyword  operators =, >,, <, == defined  messages length(), find( str ), substr( start, length ) defined  Object Oriented Programming  classes and objects  information hiding

26 Homework for next time  Another motivation for classes is the notion of abstraction.  Find out what the word means.  Come up with an example of how we use abstraction in everyday life.  Write down an example of a bankAccount object.

27 Homework for next time  Implement the following nonmember function:  string returnedExpression(string inputString)  // PRE: inputString is a line of valid C++ code // (<=80 chars) containing the “return” // keyword and ending with a semicolon  // POST: FCTVAL == the expression that follows // the return keyword, not including the // semicolon  Hint: you’ll need to (and should) use all of the string member functions we discussed today.  Ex: returnedExpression(“ return a+foo(a-1);”);  returns “a+foo(a-1)”  returns “a+foo(a-1)”  This PowerPoint is online at