Lecture Overview Linked List quiz Finish last class' topic:

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

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
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.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Lecture 19:String Data Type Introduction to Computer Science Spring 2006.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
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.
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++
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
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.
CSC 270 – Survey of Programming Languages
String Class Mohamed Shehata 1020: Introduction to Programming.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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.
Intro to Classes via the C++ String Class November 18, 2002 CSE103 - Penn State University Online at
More about strings in C++. String member functions The next three slides present information about functions that are members of the C++ string class.
Functions & Strings CIS Feb-06. Quiz 1.Write a function Prototype for the function findSmallest that takes three integers and returns an integer.
Chapter 8 Strings and Vectors. Slide 8- 2 Overview 8.1 An Array Type for Strings 8.2 The Standard string Class 8.3 Vectors.
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.
1 ENERGY 211 / CME 211 Lecture 7 October 6, 2008.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Slide 1 Chapter 9 Strings. Slide 2 Learning Objectives  An Array Type for Strings  C-Strings  Character Manipulation Tools  Character I/O  get, put.
Functions Input and output Lecture 2. Constants #define – is a preprocessor directive Most common use.
General Updates ● The Wiki is now back up to date. I have also added a working link to the lecture slides online. ● Whenever you notice something missing.
Strings.
Strings CSCI 112: Programming in C.
Chapter 8 Strings and Vectors 1
Chapter 8 Strings and Vectors
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Strings (part 2) Dr. Xiao Qin Auburn.
Chapter 9 Strings Copyright © 2016 Pearson, Inc. All rights reserved.
Introduction to C++ Systems Programming.
Working with Strings Lecture 2 Hartmut Kaiser
getline() function with companion ignore()
Standard Version of Starting Out with C++, 4th Edition
Dynamic Memory Allocation Reference Variables
Object Oriented Programming COP3330 / CGS5409
Engineering 1020: Introduction to Programming Fall 2018
C Stuff CS 2308.
C++ STRINGS string is part of the Standard C++ Library
Operator Overloading; String and Array Objects
Learning Objectives String Class.
More About Data Types & Functions
Working with Strings Lecture 2 Hartmut Kaiser
Operator Overloading; String and Array Objects
10.1 Character Testing.
Today’s Lecture I/O Streams Tools for File I/O
String class and its objects
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Class string and String Stream Processing
Chapter 9 Strings Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CS 144 Advanced C++ Programming February 7 Class Meeting
Standard Version of Starting Out with C++, 4th Edition
Submitted By : Veenu Saini Lecturer (IT)
Chapter 9: Pointers and String
Using string type variables
Today’s Objectives 28-Jun-2006 Announcements
Programming Strings.
Class rational part2.
getline() function with companion ignore()
Classes and Objects Systems Programming.
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Lecture Overview Linked List quiz Finish last class' topic: Static variables/functions Local/Nested classes String class

Quiz! On Linked Lists. 5-10 minutes to take it. One question, worth 10 points Immediate review afterwards by request

Static Member Variables/Functions Static variables are variables shared by all members of a class. They can be private, to gain the advantages of a global variable, but one that is private only to objects of that class. Static functions are functions that do not use any of the data specific to an object in the class it is defined in.

Using static member variables To declare them: class Server{ ... private: static int turn; ... } int Server::turn = 0; They can be public, but then you essentially just have a global variable. All class members will now share an integer called turn. Note that you must define the variable outside the class definition.

Using static member functions To declare them: class Server{ public: static void getTurn(); ... private: static int turn; ... } int Server::turn = 0; void Server::getTurn(){ turn++; return turn; }

Using Static Member Functions Note that static functions can only use static variables in their code. getTurn can now be invoked outside the class. While this is not unusual (it is public), since it is static you need not (and usually should not) invoke it with a particular object. Instead of int currTurn = object.getTurn(); call int currTurn = Server::getTurn();

Nested/Local Class definitions It is possible to declare an entirely new class within a class definition, known as a nested class. You can also declare a new class within a function, known as a local class. Local classes are confined to use within the function, and may not contain static members. Nested classes are accessible outside the class if they are public. We may see nested classes much later in the semester.

The string class Last time we briefly touched on C-strings. Now that we know about classes, we will discuss the “new and improved” version of strings in C++, the string class.

String class overview Designed to let you use strings without worrying about the details of managing char arrays. Defined in library <string> with definitions in the std namespace.

Improvements in the string class Maintains simplistic array style access if desired Allows the use of operators like = (for assignment) and + (for concatenation). Automatically allocates space as needed. Handles C-string conversion

Initializing a string There are several ways to set a string #include <string> using namespace std; string blank; blank = “not blank now”; string noun(“cat”); string concat = noun + “ “ + blank; x = y now sets the value of the left string to the value of the right string. x + y returns a new string with the value of the two strings concatenated.

I/O with strings More or less the same as previously discussed with C-strings Use >> to extract from a stream (no whitespace!) string word; cin >> word; Use the getline function slightly differently: string line; getline(cin, line); getline has two versions: getline(istream, string); getline(istream, string, char); Technical note: getline returns its first argument.

I/O with Strings Output with strings like you would any other variable: cout << line;

Processing strings There are a vast number of member functions in the string class. You can access the characters of a string like you could access an array! demoString[4]; There is also a member function that checks for bounds: demoString.at(4); Both will get the 5th character of the string, but .at() will check to make sure the character is legal to retrieve.

String processing The string class has a very large number of functionalities that make using them easier. ==,!=,<,>,<=,>= have been designed to compare strings (using overloading, a topic for next class) str.substring(position, length) returns the substring of a certain length at a particular position in str. str.empty() is a boolean check for an empty string str.length() returns information regarding the length of the string A partial listing of all the functionalities of the string class is on p. 394.