Unit 3 Lesson 5 Strings and the String Class Mr. Dave Clausen (modifications from the textbook)

Slides:



Advertisements
Similar presentations
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
28-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
Chapter 2: Introduction to C++.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
1 Character Strings and Variables Character Strings Variables, Initialization, and Assignment Reading for this class: L&L,
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ fundamentals.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Input & Output: Console
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Unit 3: Java Data Types Math class and String class.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Unit 3 Lesson 4 How Data Types Affect Calculations Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Functions.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 2 part #1 C++ Program Structure
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
Unit 3 Lesson 6 Input and Output Streams with modifications by Mr. Dave Clausen.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 Introduction to Object Oriented Programming Chapter 10.
CSCI 1100/1202 January 14, Abstraction An abstraction hides (or ignores) the right details at the right time An object is abstract in that we don't.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Programming what is C++
Topic Pre-processor cout To output a message.
Chapter 2: Introduction to C++
Introduction to Objects
Chapter 2 – Getting Started
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Wednesday 09/23/13.
Chapter 2: Introduction to C++.
A First Program.
Chapter 2 part #1 C++ Program Structure
String Objects & its Methods
Introduction to Objects
Presentation transcript:

Unit 3 Lesson 5 Strings and the String Class Mr. Dave Clausen (modifications from the textbook)

Mr. Dave Clausen2 Introduction to Strings and Literals A group of characters put together to create text is called a string. Strings are one of the most useful kinds of data in a program. C++ doesn’t have a “built-in” data type for strings. Values or strings that are hard-coded into the source code are called literals. A hard-coded numeric value is called a numeric literal. A string of text that is hard-coded is called a string literal. A single character can also be hard-coded. A character literal appears in single quotation marks. A string literal appears in double quotation marks.

Mr. Dave Clausen3 Code List 5-1 x = 6.3; // 6.3 is a numeric literal cout << “Hello”; // “Hello” is a string literal MyChar = ‘A’; // ‘A’ is a character literal Literals cannot change their values while the program runs.

Mr. Dave Clausen4 Introduction to Classes and Objects C++ is an Object Oriented Programming (OOP) language. The purpose of a high level programming language is to hide the details and make programming a more rapid and dependable process. OOP allows programmers to create their own operations, and data types while hiding the details. You don’t need to know the details of how strings work, just how to use them.

Mr. Dave Clausen5 String Class An object-oriented string data type is referred to as a string class. A string class is a definition used to create a string object. It is important to know the distinction between a class and an object. –Think of a class as a generic definition from which an object is created. –A dog would be an example of a class, while “Rover” would be an example of an object of that class (the dog class). –An object is said to be an instance of a class. –Therefore, “Rover” is an instance of the dog class.

Mr. Dave Clausen6 The “oostring” class Our textbook authors have provided us with a string class to use, they have named it the “oostring” class. The class consists of two parts, a header file and a definition file, or oostring.hoostring.cpp oostringh.txtoostringcpp.txt

Mr. Dave Clausen7 Using the String Class Using the string class is a little more complicated than using the built-in data types. To use the string class, you must include a header file in your source code. #include “oostring.cpp” for Borland C //”oostring.h” for other compilers. Use “ ” instead of since oostring is not precompiled. The two files (oostring.h and oostring.cpp) have to be in the same directory as your source code.

Mr. Dave Clausen8 Code List 5-2 When you declare a string object, you can create an empty string object or you can initialize the object with a string. #include “oostring.cpp” int main ( ) { oostring MyString1; // declare an empty string object oostring MyString2 (“ABCDEF”); // initializing while declaring }

Mr. Dave Clausen9 Assigning Strings to String Objects You can assign strings to string objects in one of three ways: –You can assign the contents of one string object to another string object. –You can assign a string literal to a string object. –You can assign a character literal to a string object.

Mr. Dave Clausen10 Code List 5-3 #include “oostring.cpp” int main ( ) { MyString1 = MyString2; //object to object MyString1 = “string literal”; //literal to object MyString1 = ‘A’; //character literal to string object }

Mr. Dave Clausen11 Printing the Contents of a String Object to the Screen You can use cout to display the contents of a string object: cout << MyString1 << endl;

Mr. Dave Clausen12 Code List 5-4 / / stringex.cppstringex.txtstringex.cppstringex.txt # include # include “oostring.cpp” int main () { oostring MyString1; oostring MyString2 (“ABCDEFGHIJKLMNOPQRSTUVWXYZ”) ; MyString1 = “Hello world!”; cout << MyString1 << ‘\n’; cout << MyString2 << ‘\n’; return 0; }

Mr. Dave Clausen13 String Operations Programs often need to perform a variety of operations on the strings it stores. For example, to properly center a string, you may need to know the number of characters in the string. You may also need to add the strings together.

Mr. Dave Clausen14 Messages One of the important concepts behind the use of objects is the idea of containment. This term refers to the way an object hides the details of how data is stored, and how operations work. The data, and the code required to work with the data, is contained or encapsulated within the object itself. To make the object do what we want it to do, we send the object a message.

Mr. Dave Clausen15 Obtaining the Length of a String The message used to obtain the length of a string is simply length. length = MyString2.length(); The period that follows the name of the object is called the dot operator. The dot operator separates the name of the object from the message, in this case length. The code inside the object that performs the length operation is called a method. Therefore, when you are sending the length message you could say that you are using the length method of the string class.

Mr. Dave Clausen16 Code List 5-5 // stringex2.cppstringex2.txtstringex2.cppstringex2.txt #include #include "oostring.cpp" int main() { int length1; int length2; oostring MyString1; oostring MyString2("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); MyString1 = "Hello World!"; length1 = MyString1.length(); length2 = MyString2.length(); cout << MyString1 << endl; cout << "Length = " << length1 <<endl; cout << MyString2 << endl; cout << "Length = " << length2 <<endl; getch(); return 0; }

Mr. Dave Clausen17 Shorthand Notation I prefer that we do not use the following shorthand notation in our programs, since this is an introductory class.

Mr. Dave Clausen18 String Concatenation Concatenation is a big word that describes the operation of adding or appending one string onto the end of another string. Suppose, for example, that you have one string object holding a first name and another string object holding a last name. To get both strings together in one string object, you need to concatenate the last name onto the first name. Actually, you would first concatenate a space onto the end of the first name to insert a space between the first and last names.

Mr. Dave Clausen19 Concatenation Shorthand I prefer that we do not use the following shorthand notation in our programs, since this is an introductory class.

Mr. Dave Clausen20 Code List 5-6 MyString1 = “Tracy”; MyString2 = “Stewart”; MyString1 = MyString1 + ‘ ‘;/ / add a space after the first name MyString1 = MyString1 + MyString2; / / add the last name to MyString1 MyString1 = MyString1 + “ was here.”; / / add a string literal to MyString1 cout << MyString1 << endl;