Object Oriented Programming COP3330 / CGS5409

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Chapter 8 Arrays and Strings
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
Object Oriented Programming COP3330 / CGS5409.  Dynamic Allocation in Classes  Review of CStrings.
1 Chapter 10 Characters, Strings, and the string class.
Chapter 8 Arrays and Strings
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Characters, Strings, And The string Class Chapter 10.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter Characters, Strings, and the string class 10.
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
Strings: C-strings vs. Strings as Objects
Characters, Strings, and the cstring Library
Strings CSCI 112: Programming in C.
C-Strings We have already seen that a C-string is a null-terminated array of characters.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Andy Wang Object Oriented Programming in C++ COP 3330
Computer Programming BCT 1113
Characters, Strings, and the cstring Library
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
Object Oriented Programming COP3330 / CGS5409
C Stuff CS 2308.
Remark: Data Type of Array Name
Strings: C-strings vs. Strings as Objects
More About Data Types & Functions
Pointers and Pointer-Based Strings
Week 9 – Lesson 1 Arrays – Character Strings
String in C++.
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Strings What is a string? It is an array of characters terminated with
CPS120: Introduction to Computer Science
Engineering Problem Solving with C++, Etter
Standard Version of Starting Out with C++, 4th Edition
Lecture 2 Arrays & Pointers May 17, 2004
C++ Programming Lecture 20 Strings
COP 3330 Object-oriented Programming in C++
Lecture 2 Arrays & Pointers September 7, 2004
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Object Oriented Programming COP3330 / CGS5409 Recitation Week 10 Object Oriented Programming COP3330 / CGS5409

Today’s Recitation c-strings vs. concept of string class Dynamic review Assignment 5

C-style strings Recall that a C-string is implemented as a null-terminated array of type char No built-in string type in C. Must use character arrays NOT every character array is a C-string. Only when terminated with the null-character String literals in code ("Hello World", "John Smith") are implemented as constant C- strings

The <cstring> library Contains functions for common string operations, such as copy, compare, concatenate, length, search, tokenization, and more strlen() strcpy(), strncpy() strcat(), strncat() strcmp(), strncmp() strstr() strtok() etc.

Special features in <iostream> Special built-in functions for I/O handling of C-style strings, like the insertion and extraction operators, get(), getline(), etc char str1[40]; cout << str1; // insertion operator for strings cin >> str1; // extraction, reads up to white space cin.get(str1, 40, ','); // reads to delimeter (comma) cin.getline(sr1, 40); // reads to delimiter (default delimiter // is newline), discards delimiter

The DOWN side of C-strings Fixed length, when declared as a normal C-style array This is fine for things like database fields, where a fixed size on each field is necessary Not so flexible for storing just any old string. string name really just acts as a pointer, since it's the name of an array As such, always must be passed in and out of functions by address -- i.e. with pointer parameter type or return type Must be careful of array boundaries. Overflow of string boundary not automatically detected Less intuitive function calls for common operations, like comparison or copying:

The DOWN side of C-strings // Consider: I want to assign "Hello World" to a string called greeting: char greeting[40]; // can I now say the following? (Which would be pretty intuitive) greeting = "Hello World"; // Legal? NO! // Instead, I have to do it this way, with the strcpy function: strcpy(greeting, "Hello World"); // Comparison: This call doesn't compare contents, only pointers: if (str1 == str2) // For c-strings, use strcmp: if (strcmp(str1, str2) == 0) // then strings are the same

Building a String class Use dynamic allocation and resizing to get flexible capacity Do all dynamic memory management inside the class, so the user of string objects doesn't have to! Use operator overloads if more intuitive notations are desired insertion, extraction operators for easy I/O comparison operators for easy comparisons, sorting, etc. operator+ for concatenation, if desired Build copy constructor and assignment operator to for correct assignment and pass-by-value abilities Build a conversion constructor to convert c-style strings to string objects, for automatic type conversions! Could include conversion constructors for converting other types to strings, too

example String class BString class

Assignment 5 intro/issues Don't wait until the last minute to start this one!!! Use good principles like we did in the BString example (and other DMA examples) separate out the memory management functions from the algorithmic features. This means, avoid doing TONS of "new" and "delete" calls everywhere -- restrict those to a select set of functions. This will make your life easier as you code this Decide on storage first, and then make sure all of your functions adhere to whatever rules you set up in advance for your storage

Questions?