C++ For Dummies 5th Edition phần 5 pot

44 427 0
C++ For Dummies 5th Edition phần 5 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

16 568523 Ch11.qxd 4/5/04 1:58 PM Page 160 160 Part III: Introduction to Classes I don’t have to clutter my limited storage with all the things that an SUV has in common with other cars. All I have to remember is “an SUV is a car that . . .” and tack on those few things that are unique to an SUV (like the price tag). I can go further. Cars are a subclass of wheeled vehicles along with other mem- bers, such as trucks and pickups. Maybe wheeled vehicles are a subclass of vehicles, which includes boats and planes. And on and on and on. Why Classify? Why do we classify? It sounds like a lot of trouble. Besides, people have been using the functional approach for so long, why change now? It may seem easier to design and build a microwave oven specifically for this one problem, rather than build a separate, more generic oven object. Suppose, for example, that I want to build a microwave to cook nachos and nachos only. I wouldn’t need to put a front panel on it, other than a START button. I always cook nachos the same amount of time, so I could dispense with all that DEFROST and TEMP COOK nonsense. My nachos-only microwave needs to hold only one flat little plate. Three cubic feet of space would be wasted on nachos. For that matter, I can dispense with the concept of “microwave oven” alto- gether. All I really need is the guts of the oven. Then, in the recipe, I put the instructions to make it work: “Put nachos in the box. Connect the red wire to the black wire. Bring the radar tube up to about 3,000 volts. Notice a slight hum. Try not to stand too close if you intend to have children.” Stuff like that. But the functional approach has some problems: ߜ Too complex. I don’t want the details of oven building mixed into the details of nacho building. If I can’t define the objects and pull them out of the morass of details to deal with separately, I must deal with all the complexities of the problem at the same time. ߜ Not flexible. Someday I may need to replace the microwave oven with some other type of oven. I should be able to do so as long as its inter- face is the same. Without being clearly delineated and developed sepa- rately, it becomes impossible to cleanly remove an object type and replace it with another. ߜ Not reusable. Ovens are used to make lots of different dishes. I don’t want to create a new oven every time I encounter a new recipe. Having solved a problem once, it would be nice to be able to reuse the solution in future programs. The remaining chapters in this Part demonstrate how object-oriented lan- guage features address these problems. 17 568523 Ch12.qxd 4/5/04 1:58 PM Page 161 Chapter 12 Adding Class to C++ In This Chapter ᮣ Grouping data into classes ᮣ Declaring and defining class members ᮣ Accessing class members P rograms often deal with groups of data: a person’s name, rank, and serial number, stuff like that. Any one of these values is not sufficient to describe a person — only in the aggregate do the values make any sense. A simple structure such as an array is great for holding stand-alone values; however, it doesn’t work very well for data groups. This makes good ol’ arrays inadequate for storing complex data (such as personal credit records that the Web compa- nies maintain so they can lose them to hackers). For reasons that will become clear shortly, I’ll call such a grouping of data an object. A microwave oven is an object (see Chapter 11 if that doesn’t make sense). You are an object. Your name, rank, and credit card number in a data- base are objects. Introducing the Class What you need is a structure that can hold all the different types of data nec- essary to describe a single object. In my simple example, a single object can hold both a first name and a last name along with a credit card number. C++ calls the structure that combines multiples pieces of data into a single object a class. 17 568523 Ch12.qxd 4/5/04 1:58 PM Page 162 162 Part III: Introduction to Classes The Format of a Class A class used to describe a name and credit card grouping might appear as follows: // the dataset class class NameDataSet { public: char firstName[128]; char lastName [128]; int creditCard; }; // a single instance of a dataset NameDataSet nds; A class definition starts with the keyword class followed by the name of the class and an open-close brace pair. The statement after the open brace is the keyword public. (Hold off asking about the meaning of the public keyword. I’ll make its meaning public a little later. Later chapters describe options to public, such as private. Thus, the public must stay private until I can make the private public.) The alternative keyword struct can be used. The keywords struct and class are identical except that the public declaration is assumed in the struct and can be omitted. You should stick with class for most programs for rea- sons that will become clear later in this chapter. Following the public keyword are the entries it takes to describe the object. The NameDataSet class contains the first and last name entries along with the credit card number. As you would expect, the first and last names are both character arrays — the credit card number is shown here as a simple integer (“the better to steal you with, my dear”). A class declaration includes the data necessary to describe a single object. The last line of the snippet declares the variable nds to be a single entry of class NameDataSet. Thus, nds might be an entry that describes a single person. We say that nds is an instance of the class NameDataSet. You instantiate the class NameDataSet to create nds. Finally, we say that firstName and the others are members or properties of the class. We say a whole lot of silly things. 17 568523 Ch12.qxd 4/5/04 1:58 PM Page 163 Chapter 12: Adding Class to C++ 163 Accessing the Members of a Class The following syntax is used to access the property of a particular object: NameDataSet nds; nds.creditCard = 10; cin >> nds.firstName; cin >> nds.lastName; Here, nds is an instance of the class NameDataSet (for example, a particular NameDataSet object). The integer nds.creditCard is a property of the nds object. The type of nds.creditCard is int, whereas that of nds.firstName is char[]. Okay, that’s computerspeak. What has actually happened here? The program snippet declares an object nds, which it will use to describe a customer. For some reason, the program assigns the person the credit card number 10 (obviously bogus, but it’s not like I’m going to include one of my credit card numbers). Next, the program reads the person’s first and last names from the default input. I am using an array of characters rather than the class string to handle the name. From now on, the program can refer to the single object nds without dealing with the separate parts (the first name, last name, and credit card number) until it needs to. The following program demonstrates the NameDataSet class: // DataSet - store associated data in // an array of objects #include <cstdio> #include <cstdlib> #include <iostream> #include <string.h> using namespace std; // NameDataSet - store name and credit card // information class NameDataSet { public: char firstName[128]; char lastName [128]; int creditCard; }; 17 568523 Ch12.qxd 4/5/04 1:58 PM Page 164 164 Part III: Introduction to Classes // function prototypes: bool getData(NameDataSet& nds); void displayData(NameDataSet& nds); int main(int nNumberofArgs, char* pszArgs[]) { // allocate space for 25 name data sets const int MAX = 25; NameDataSet nds[MAX]; // load first names, last names and social // security numbers cout << “Read name/credit card information\n” << “Enter ‘exit’ to quit” << endl; int index = 0; while (getData(nds[index]) && index < MAX) { index++; } // display the names and numbers entered cout << “\nEntries:” << endl; for (int i = 0; i < index; i++) { displayData(nds[i]); } // wait until user is ready before terminating program // to allow the user to see the program results system(“PAUSE”); return 0; } // getData - populate a NameDataSet object bool getData(NameDataSet& nds) { cout << “\nEnter first name:”; cin >> nds.firstName; // compare the name input irrespective of case if (stricmp(nds.firstName, “exit”) == 0) { return false; } cout << “Enter last name:”; cin >> nds.lastName; cout << “Enter credit card number:”; cin >> nds.creditCard; return true; 17 568523 Ch12.qxd 4/5/04 1:58 PM Page 165 Chapter 12: Adding Class to C++ 165 } // displayData - display a data set void displayData(NameDataSet& nds) { cout << nds.firstName << “ “ << nds.lastName << “/” << nds.creditCard << endl; } The main() function allocates 25 objects of class NameDataSet. main(), prompts the user as to what is expected of her, and then enters a loop in which entries are read from the keyboard using the function getData(). The loop terminates when either getData() returns a false or the maximum number of objects (25) have been created. The same objects read are next passed to displayData(NameDataSet) for display. The getData() function accepts a NameDataSet object as its input argu- ment, which it assigns the name nds. Ignore the ampersand for now — I explain it in Chapter 14. getData() then reads a string from standard input into the entry firstName. If the stricmp() function can find no difference between the name entered and “exit,” the function returns a false to main() indicating that it’s time to quit. (The function stricmp() compares two strings without regard to their case. This function considers “exit” and “EXIT” plus any other combination of upper- case and lowercase letters to be identical.) Otherwise, the function pushes on, reading the last name and the credit card number into the object nds. The displayData() function outputs each of the members of the NameDataSet object nds separated by delimiters. A simple run of this program appears as follows: Read name/credit card information Enter ‘exit’ for first name to exit Enter first name:Stephen Enter last name:Davis Enter credit card number:123456 Enter first name:Marshall Enter last name:Smith Enter credit card number:567890 Enter first name:exit 17 568523 Ch12.qxd 4/5/04 1:58 PM Page 166 166 Part III: Introduction to Classes Entries: Stephen Davis/123456 Marshall Smith/567890 Press any key to continue The program begins with an explanatory banner. I enter my own glorious name at the first prompt (I’m modest that way). Because the name entered does not rhyme with “exit,” the program continues, and I add a last name and a pretend credit card number. On the next pass, I tack on the name Marshall Smith and his real credit card number (have fun, Marshall). On the final path, I enter “exit”, which terminated the input loop. The program does nothing more than spit back at me the names I just entered. 18 568523 Ch13.qxd 4/5/04 1:58 PM Page 167 Chapter 13 Making Classes Work In This Chapter ᮣ Adding active properties to the class ᮣ Declaring and defining a member function ᮣ Accessing class member functions ᮣ Overloading member functions P rogrammers use classes to group related data elements into a single object. The following Savings class associates an account balance with a unique account number: class Savings { public: unsigned accountNumber; float balance; }; Every instance of Savings contains the same two data elements: void fn(void) { Savings a; Savings b; a.accountNumber = 1; // this is not the same as b.accountNumber = 2; // this one } The variable a.accountNumber is different from the variable b.accountNumber, just as the balance in my bank account is different from the balance in yours, even though they’re both called balance (or, in the case of my account, lack of balance). 18 568523 Ch13.qxd 4/5/04 1:58 PM Page 168 168 Part III: Introduction to Classes Activating Our Objects You use classes to simulate real-world objects. The Savings class tries to represent a savings account. This allows you to think in terms of objects rather than simply lines of code. The closer C++ objects are to the real world, the easier it is to deal with them in programs. This sounds simple enough. However, the Savings class doesn’t do a very good job of simulating a sav- ings account. Simulating real-world objects Real-world objects have data-type properties such as account numbers and balances, the same as the Savings class. This makes Savings a good start- ing point for describing a real object. But real-world objects do things. Ovens cook. Savings accounts accumulate interest, CDs charge a substantial penalty for early withdrawal — stuff like that. Functional programs “do things” via functions. A C++ program might call strcmp() to compare two strings or max() to return the maximum of two values. In fact, Chapter 24 explains that even stream I/O ( cin >> and cout << ) is a special form of function call. The Savings class needs active properties of its own if its to do a good job of representing a real concept: class Savings { public: float deposit(float amount) { balance += amount; return balance; } unsigned int accountNumber; float balance; }; In addition to the account number and balance, this version of Savings includes the function deposit(). This gives Savings the ability to control its own future. A class MicrowaveOven has the function cook(), the class Savings has the function accumulateInterest(), and the class CD has the function penalizeForEarlyWithdrawal(). Functions defined in a class are called member functions. 18 568523 Ch13.qxd 4/5/04 1:58 PM Page 169 Chapter 13: Making Classes Work 169 Why bother with member functions? Why should you bother with member functions? What’s wrong with the good ol’ days: class Savings { public: unsigned accountNumber; float balance; }; float deposit(Savings& s, unsigned amount) { s.balance += amount; return s.balance; } Here, deposit() implements the “deposit into savings account” function. This functional solution relies on an outside function, deposit(), to imple- ment an activity that savings accounts perform but that Savings lacks. This gets the job done, but it does so by breaking the object-oriented (OO) rules. The microwave oven has internal components that it “knows” how to use to cook, defrost, and burn to a crisp. Class data members are similar to the parts of a microwave — the member functions of a class perform cook-like functions. When I make nachos, I don’t have to start hooking up the internal compo- nents of the oven in a certain way to make it work. Nor do I rely on some external device to reach into a mess of wiring for me. I want my classes to work the same way my microwave does (and, no, I don’t mean “not very well”). I want my classes to know how to manipulate their internals without outside intervention. Member functions of Savings such as deposit() can be written as external functions. I can put all of the functions necessary to make a savings account work in one place. Microwave ovens can be made to work by soldering and cutting wires. I don’t want my classes or my microwave ovens to work that way. I want a Savings class that I can use in my banking program without considering how it might work on the inside. Adding a Member Function There are two aspects to adding a member function to a class: creating the member function and naming it (sounds silly, doesn’t it?). [...]... s[10]; // assign the 5th student a gpa of 5. 0 (lucky guy) s[4].gpa = 5. 0; // add another course to the 5th student; // this time he failed - serves him right s[4].addCourse(3, 0.0); } Here s is an array of Student objects s[4] refers to the fifth Student object in the array By extension, s[4].gpa refers to the GPA of the 5th student Further, s[4].addCourse() adds a course to the 5th Student object Declaring... *pS.gpa = 3 .5; return 0; } As the comments indicate, this doesn’t work The problem is that the dot operator (.) is evaluated before the pointer (*) Note: The * operator is often referred to as the splat operator — not a popu­ lar term with insects C++ programmers use parentheses to override the order in which operations are performed For example, the parentheses force addition to be performed before multiplication... 6; t.gpa = 1.0; // not doing so good t.addCourse(3, 1 .5) ; // things aren’t getting any better // wait until user is ready before terminating program // to allow the user to see the program results system(“PAUSE”); return 0; } The syntax for calling a member function looks like a cross between the syntax for accessing a data member and that used for calling a function The right side of the dot looks... standard non-Visual Studio.NET–specific C++ Why Not Use References Rather Than Pointers? The syntax for manipulating a reference is similar to that used with normal objects So why not just stick with references and never look back at pointers? Objects and their addresses aren’t the same thing Many times, the syntax for a reference actually becomes more complicated than that for pointers Consider the following... 3.0; // the values before the call cout . number:”; cin >> nds.creditCard; return true; 17 56 852 3 Ch12.qxd 4 /5/ 04 1 :58 PM Page 1 65 Chapter 12: Adding Class to C++ 1 65 } // displayData - display a data set void displayData(NameDataSet&. credit card number :56 7890 Enter first name:exit 17 56 852 3 Ch12.qxd 4 /5/ 04 1 :58 PM Page 166 166 Part III: Introduction to Classes Entries: Stephen Davis/123 456 Marshall Smith /56 7890 Press any. number. C++ calls the structure that combines multiples pieces of data into a single object a class. 17 56 852 3 Ch12.qxd 4 /5/ 04 1 :58 PM Page 162 162 Part III: Introduction to Classes The Format

Ngày đăng: 12/08/2014, 12:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan