Download presentation
Published by可 陆 Modified over 7 years ago
1
Lecture Overview Linked List quiz Finish last class' topic:
Static variables/functions Local/Nested classes String class
2
Quiz! On Linked Lists. 5-10 minutes to take it.
One question, worth 10 points Immediate review afterwards by request
3
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.
4
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.
5
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; }
6
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();
7
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.
8
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.
9
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.
10
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
11
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.
12
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.
13
I/O with Strings Output with strings like you would any other variable: cout << line;
14
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.
15
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.