Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Classes Classes and Function Members. Review We’ve seen that the iostream library provides the objects cin, cout and cerr: These objects were not.

Similar presentations


Presentation on theme: "Using Classes Classes and Function Members. Review We’ve seen that the iostream library provides the objects cin, cout and cerr: These objects were not."— Presentation transcript:

1 Using Classes Classes and Function Members

2 Review We’ve seen that the iostream library provides the objects cin, cout and cerr: These objects were not originally provided in C++, but were added to the language using its class mechanism.

3 Classes The C++ class mechanism allows a user to add new types to the language. Bell Labs’ Jerry Schwarz used this to create: an istream class, to define the object cin; andan istream class, to define the object cin; and an ostream class, to define cout and cerr.an ostream class, to define cout and cerr. The resulting I/O system was so elegant, it was incorporated into the language.

4 Classes (ii) Another class that was added ‘after the fact’ is the string class, which provides a convenient way to store and operate on sequences of characters. The string library provides the type string, plus an assortment of useful string-processing operations.

5 String Objects Objects of type are indexed variables, meaning that each character in the variable can be accessed via an index or subscript: Objects of type string are indexed variables, meaning that each character in the variable can be accessed via an index or subscript: string name = “John Q. Doe”; Use the subscript operator to access individual chars: oJh nQ. name 0123456 Do 789 e 10 char firstInitial = name[0]; // firstInitial == ‘J’

6 Dynamic string Objects name = “Philleas Fogg”; // name.size() == 13 Objects of type can grow and shrink as necessary to store their contents: Objects of type string can grow and shrink as necessary to store their contents: oJh nQ. name 0123456 Do 789 e 10 hPillea name 0123456 s F 789 o 10 g 11 g 12 string name = “John Q. Doe”; // name.size() == 11

7 Some string Operations Operation string function read a word from an istream istream >> str; read a line from an istream getline(istream, str); find the length of the string str.size() find if a string is empty str.empty() access the char at index i str[i] concatenate two strings str1 + str2 access a substring of a string str.substr(Pos, NumChars) insert a substring into a string str.insert(Pos, SubStr); remove a substring str.remove(Pos, NumChars); find a substring in a string str.find(Pattern, StartPos) compare two strings str1 == str2 (or !=,, =) (or !=,, =)...

8 Discussion Some string operations are “normal” functions: getline(cin, aString); Other string operations are function members: aString.size(); Function members are “messages” that class objects “understand” and to which they respond... For example, “knows” how big it is, so when it receives the message, it responds with the appropriate answer. For example, aString “knows” how big it is, so when it receives the size() message, it responds with the appropriate answer.

9 Function Members Where a “normal” function is an external agent that acts upon an object, a function member is a message that elicits an internal response from the object receiving it. Examples: getline(cin, aString); // getline() acts on aString if (aString.empty()) // ask aString, “are you empty?” if (cin.good()) // ask cin, “are you good?” In this sense, class objects are “more intelligent” than regular char, int, double,... objects.

10 Classes Most classes provide a rich set of operations that can be used to manipulate objects of the class. To use a class effectively, you must know what kinds of functions (“normal” and member) are available to operate on objects of that class. Otherwise, you risk spending much of your time “reinventing the wheel.”

11 Example Suppose a problem requires me to find the position of a particular character within a string. I can either write/call my own function to do so... int PositionOf(char ch; string str) { for (int i = 0; i < str.size(); i++) if (str[i] == ch) return i; return -1; }... int pos = PositionOf(myChar, aString);

12 Example (ii)... or I can ask aString to locate myChar for me, using the string function member named find(): int pos = aString.find(myChar, 0); Which is less work? Writing your own is more work to code, test and debug, and the result is less flexible than find().Writing your own is more work to code, test and debug, and the result is less flexible than find(). Using find() requires awareness (i) that the function member exists, and (ii) of how to use it.Using find() requires awareness (i) that the function member exists, and (ii) of how to use it.

13 Discussion Be aware of the functionality a class provides (but don’t memorize the nitty-gritty details). Know where (in a reference book) to look up the operations a class supports. Then, when a problem involves an operation on a class object, scan the list of operations looking for one that you can use -- don’t reinvent the wheel!

14 Example The text provides a RandomInt class. Objects of this class are integers with “random” values, which can be used to simulate all sorts of “random” occurrences. #include “RandomInt.h”... RandomInt die1(1,6), die2(1,6); // two dice die1.Generate(); die2.Generate() // roll the dice cout << “dice roll = “ // display results << die1 + die2 << endl;

15 RandomInt Objects The range of random values is specified when an object is declared: #include “RandomInt.h”... const int HEADS = 0, TAILS = 1; RandomInt coin(HEADS,TAILS); coin.Generate(); // flip coin cout << coin << endl; // display result

16 RandomInt Operations Operation RandomInt function Display a RandomInt ostream << randInt Declare a RandomInt RandomInt name; Declare a RandomInt within range first..last RandomInt name(first, last); Generate new random value randInt.Generate(); Generate new random value from range first..last randInt.Generate(first, last); Add two RandomInt values randInt1 + randInt2 (also -, *, /) Compare two RandomInt values randInt1 == randInt2 (also !=,, =)

17 Sample Program #include // cin, cout using namespace std; // RandomInt class #include “RandomInt.h” // RandomInt class int main() { const int HEADS = 0, TAILS = 1; // for readability const int HEADS = 0, TAILS = 1; // for readability // model a coin RandomInt coin(HEADS, TAILS); // model a coin int numHeads = 0, numTails = 0; // counters int numHeads = 0, numTails = 0; // counters for (int i = 1; i <= 10000; i++) // loop 10,000 times for (int i = 1; i <= 10000; i++) // loop 10,000 times { // flip coin coin.Generate(); // flip coin if () // count if (coin == HEADS) // count numHeads++; // heads numHeads++; // heads else // vs. tails else // vs. tails numTails++; numTails++; } cout << “\nIn 10,000 tosses of a coin,\n” cout << “\nIn 10,000 tosses of a coin,\n” << “ heads occurred “ << numHeads << “ times\n” << “ heads occurred “ << numHeads << “ times\n” << “ and tails occurred << numTails << “ times” << “ and tails occurred << numTails << “ times” << endl; << endl;}

18 Other Classes As mentioned earlier, cin and cout are objects of the istream and ostream classes, respectively. To use these classes effectively, you must be aware of the range operations available for them...

19 Some istream Operations istream function Description cin >> ch; Extract next non-whitespace character from cin and store it in ch. from cin and store it in ch. cin.get(ch); Tell cin, “Put your next character (whitespace or not) into ch.” (whitespace or not) into ch.” cin.good() Ask cin, “Are you in good shape?” cin.bad() Ask cin, “Is something wrong?" cin.fail() Ask cin, “Did the last operation fail?” cin.clear(); Tell cin, “Reset yourself to be good.” cin.ignore(n, ch); Tell cin, ignore the next n characters, or until ch occurs, whichever comes first. or until ch occurs, whichever comes first.

20 Some ostream Operations ostream function Description cout >> expr Insert expr into cout. cout.put(ch); Tell cin, “Insert ch into yourself.” cout << flush Write contents of cout to screen. cout << endl Write a newline to cout and flush it. cout << fixed Display reals in fixed-point notation. cout << scientific Display reals in scientific notation. cout << showpoint Display decimal point and trailing zeros for real whole numbers. for real whole numbers. cout << noshowpoint Hide decimal point and trailing zeros for real whole numbers. for real whole numbers.

21 More ostream Operations ostream function Description cout << showpos Display sign for positive values. cout << noshowpos Hide sign for positive values. cout << boolalpha Display true, false as “true”, “false”. cout << noboolalpha Display true, false as 1, 0. cout << setprecision(n) Display n decimal places for reals. cout << setw(w) Display next value in field width w. cout << left Left-justify subsequent values. cout << right Right-justify subsequent values. cout << setfill(ch) Fill leading/trailing blanks with ch.

22 Summary Well-designed classes provide a rich set of operations that make them useful for many problems. Operations can be external (normal functions), or internal (function members) to the class. Function members act as messages to class objects. To use a class effectively, you must know what capabilities the class provides; andwhat capabilities the class provides; and how to use those capabilities.how to use those capabilities.


Download ppt "Using Classes Classes and Function Members. Review We’ve seen that the iostream library provides the objects cin, cout and cerr: These objects were not."

Similar presentations


Ads by Google