Strings,Classes,and the String Class

Strings,Classes,and the String Class - Lecture 4: Strings,...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 4: Lecture 4: Strings, Classes, and the String Class Strings, Classes, and the String Class PIC 10A Todd Wittman What is a class? What is a class? • A class is a suped-up data type that may have special member functions defined in a library. • An object is a suped-up variable that is a particular instance of a class. int v; class_name o; data type variable class object •Typically, we access the member function of a class with a period after our object object_name . function_name (parameters)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Classes in This Class Classes in This Class •We’ve already seen a class in action: cin and cout are objects defined in the iostream library. •Today we’re going to learn about the string class. •On Wednesday we’ll learn how to use a graphics class to draw some pictures. •In November we’ll learn how to write our own classes. Sec 2.6 Sec 2.6 : The String Class : The String Class • A string is a class that can hold text, much like the primitive data types int/double hold numbers. • To use it, we have to include the string library # include <string> • The declaration is the same as for variables. string name; name = “My precious!\n”; • Note the string text can hold punctuation, spaces, and even escape characters.
Background image of page 2
3 String I/O String I/O string name = “My\nprecious!\n”; cout << name; • Output: My precious! • Just remember to include both the string and iostream libraries. # include <iostream> #include <string> String I/O String I/O • To read multiple strings, they should be separated by a space or line break. int x, y;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

Strings,Classes,and the String Class - Lecture 4: Strings,...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online