Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 20 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
20
Dung lượng
349,65 KB
Nội dung
{ cout << "Bad input. Please enter a number: "; cin.clear(); while (cin.get() != '\n') continue; } cout << "Value = " << x << endl; return 0; } .4:Rewrite the following so that it uses using-declarations instead of the using-directive. #include <iostream> using namespace std; int main() { double x; cout << "Enter value: "; while (! (cin >> x) ) { cout << "Bad input. Please enter a number: "; cin.clear(); while (cin.get() != '\n') continue; } cout << "Value = " << x << endl; return 0; } .5:The average(3,6) function returns an int average of the two int arguments when called in one file, and it returns a double average of the two int arguments when called in a second file in the same program. How could you set this up? This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. .6:What will the following two-file program display? // file1.cpp #include <iostream> using namespace std; void other(); void another(); int x = 10; int y; int main() { cout << x << endl; { int x = 4; cout << x << endl; cout << y << endl; } other(); another(); return 0; } void other() { int y = 1; cout << "Other: " << x << "," << y << endl; } // file 2.cpp #include <iostream> using namespace std; extern int x; namespace { int y = -4; } void another() This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. { cout << "another(): " << x << ", " << y << endl; } .7:What will the following program display? #include <iostream> using namespace std; void other(); namespace n1 { int x = 1; } namespace n2 { int x = 2; } int main() { using namespace n1; cout << x << endl; { int x = 4; cout << x << ", " << n1::x << ", " << n2::x << endl; } using n2::x; cout << x << endl; other(); return 0; } void other() { using namespace n2; cout << x << endl; { int x = 4; This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. cout << x << ", " << n1::x << ", " << n2::x << endl; } using n2::x; cout << x << endl; } Programming Exercises 1:Here is a header file: // golf.h for pe8-3.cpp const int Len = 40; struct golf { char fullname[Len]; int handicap; }; // non-interactive version: // function sets golf structure to provided name, handicap // using values passed as arguments to the function void setgolf(golf & g, const char * name, int hc); // interactive version: // function solicits name and handicap from user // and sets the members of g to the values entered // returns 1 if name is entered, 0 if name is empty string int setgolf(golf & g); // function resets handicap to new value void handicap(golf & g, int hc); // function displays contents of golf structure void showgolf(const golf & g); Note that setgolf() is overloaded. Using the first version would look like this: This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. golf ann; setgolf(ann, "Ann Birdfree", 24); The function call provides the information that's stored in the ann structure. Using the second version would look like this: golf andy; setgolf(andy); The function would prompt the user to enter the name and handicap and store them in the andy structure. This function could (but doesn't need to) use the first version internally. Put together a multifile program based on this header. One file, named golf.cpp, should provide suitable function definitions to match the prototypes in the header file. A second file should contain main() and demonstrate all the features of the prototyped functions. For example, a loop should solicit input for an array of golf structures and terminate when the array is full or the user enters an empty string for the golfer's name. The main() function should use only the prototyped functions to access the golf structures. 2:Here is a namespace: namespace SALES { const int QUARTERS = 4; struct Sales { double sales[QUARTERS]; double average; double max; double min; }; // copies the lesser of 4 or n items from the array ar // to the sales member of s and computes and stores the This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. // average, maximum, and minimum values of the entered items; // remaining elements of sales, if any, set to 0 void setSales(Sales & s, const double ar[], int n); // gathers sales for 4 quarters interactively, stores them // in the sales member of s and computes and stores the // average, maximum, and minumum values void setSales(Sales & s); // display all information in structure s void showSales(const Sales & s); } Write a three-file program based on this namespace. The first file should be a header file containing the namespace. The second file should be a source code file extending the namespace to provide definitions for the three prototyped functions. The third file should declare two Sales objects. It should use the interactive version of setSales() to provide values for one structure and the non-interactive version of setSales() to provide values for the second structure. It should display the contents of both structures by using showSales(). CONTENTS This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. CONTENTS Chapter 10. OBJECTS AND CLASSES In this chapter you learn Procedural and Object-Oriented Programming Abstraction and Classes Class Constructors and Destructors Knowing Your Objects: The this Pointer An Array of Objects Class Scope An Abstract Data Type Summary Review Questions Programming Exercises Object-oriented programming (OOP) is a particular conceptual approach to designing programs, and C++ has enhanced C with features that ease the way to applying that approach. The most important OOP features are these: Abstraction Encapsulation and data hiding Polymorphism Inheritance Reusable code The class is the single most important C++ enhancement for implementing these features and tying them together. This chapter begins our examination of classes. It explains abstraction, encapsulation, and data hiding, and shows how classes implement these features. It discusses how to define a class, provide a class with public and private sections, and create member functions that work with the class data. Also, the chapter acquaints you with constructors and destructors, which are special member functions for This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. creating and disposing of objects belonging to a class. Finally, you meet the this pointer, an important component of some class programming. The following chapters extend the discussion to operator overloading (another variety of polymorphism) and inheritance, the basis for reusing code. Procedural and Object-Oriented Programming Although we have occasionally explored the OOP perspective on programming, we've usually stuck pretty close to the standard procedural approach of languages such as C, Pascal, and BASIC. Let's look at an example that clarifies how the OOP outlook differs from that of procedural programming. As the newest member of the Genre Giants softball team, you've been asked to keep the team statistics. Naturally, you turn to your computer for help. If you were a procedural programmer, you might think along these lines: Let's see, I want to enter the name, times at bat, number of hits, batting averages (for those who don't follow baseball or softball, the batting average is the number of hits divided by the player's official number of times at bat; an at bat terminates when a player gets on base or makes an out, but certain events, such as getting a walk, don't count as official times at bat), and all those other great basic statistics for each player. Wait, the computer is supposed to make life easier for me, so let's have it figure out some of that stuff, such as the batting average. Also, I want the program to report the results. How should I organize this? I guess I should do things right and use functions. Yeah, I'll make main() call a function to get the input, call a function to make the calculations, and then call a function to report the results. Hmmm, what happens when I get data from the next game? I don't want to start from scratch again. Okay, I can add a function to update the statistics. Golly, maybe I'll need a menu in main() to select between entering, calculating, updating, and showing the data. Hmmm—how am I going to represent the data? I could use an array of strings to hold the players' names, another array to hold the at bats for each player, yet another array to hold the hits, and so on. No, that's dumb. I can design a structure to hold all the information for a single player and then use an array of those structures to represent the whole team. In short, you first concentrate on the procedures you will follow and then think about how to This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. represent the data. (Note: So that you don't have to keep the program running the whole season, you probably also want to be able to save data to a file and read data from a file. But as we haven't covered files yet, we ignore that complication for now.) Now let's see how your perspective changes when you don your OOP hat (an attractive polymorphic design). You begin by thinking about the data. Furthermore, you think about the data not only in terms of how to represent it, but in terms of how it's to be used: Let's see—what am I keeping track of? A ball player, of course. So, I want an object that represents the whole player, not just her batting average or times at bat. Yeah, that'll be my fundamental data unit, an object representing the name and statistics for a player. I'll need some methods to handle this object. Hmmm, I guess I need a method to get basic information into this unit. The computer should calculate some of the stuff, like the batting averages—I can add methods to do calculations. And the program should do those calculations automatically, without the user having to remember to ask to have them done. Also, I'll need methods for updating and displaying the information. So, the user gets three ways to interact with the data: initialization, updating, and reporting. That's the user interface. In short, you concentrate on the object as the user perceives it, thinking about the data you need to describe the object and the operations that will describe the user's interaction with the data. After you develop a description of that interface, you move on to decide how to implement the interface and data storage. Finally, you put together a program to use your new design. Abstraction and Classes Life is full of complexities, and one way we cope with complexity is to frame simplifying abstractions. You are a collection of over an octillion atoms. Some students of the mind would say your mind is a collection of semiautonomous agents. But it's much simpler to think of yourself as a single entity. In computing, abstraction is the crucial step of representing information in terms of its interface with the user. That is, you abstract the essential operational features of a problem and express a solution in those terms. In the softball statistics example, the interface describes how the user initializes, updates, and displays the data. From abstraction, it is a short step to the user-defined type, which in C++ is a class design that implements that interface. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. What's a Type? Let's think a little more about what constitutes a type. For example, what is a nerd? If you subscribe to the popular stereotype, you may think of a nerd in visual terms—thick, black-rimmed glasses, pocket protector full of pens, and so on. After a little reflection, you might conclude that a nerd is better defined operationally, for example, in how he or she responds to an awkward social situation. We have a similar situation, if you don't mind stretched analogies, with a procedural language like C. At first, you tend to think of a data type in terms of its appearance—how it is stored in memory. A char, for example, is one byte of memory, and a double often is eight bytes of memory. But a little reflection leads us to conclude that a data type also is defined in terms of the operations that can be performed upon it. For example, the int type can use all the arithmetic operations. You can add, subtract, multiply, and divide integers. You also can use the modulus operator (%) with them. On the other hand, consider pointers. A pointer might very well require the same amount of memory as an int. It might even be represented internally as an integer. But a pointer doesn't allow the same operations that an integer does. You can't, for example, multiply two pointers by each other. The concept makes no sense, so C++ doesn't implement it. Thus, when you declare a variable as an int or as a pointer-to-float, you're not just allocating memory—you also are establishing which operations can be performed with the variable. In short, specifying a basic type does two things: It determines how much memory is needed for a data object. It determines what operations, or methods, can be performed using the data object. For built-in types, this information is built in to the compiler. But when you define a user-defined type in C++, you have to provide the same kind of information yourself. In exchange for this extra work, you gain the power and flexibility to custom fit new data types to match real-world requirements. The Class The class is the C++ vehicle for translating an abstraction to a user-defined type. It combines data representation and methods for manipulating that data into one neat package. Let's look at a class that represents stocks. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it. Thanks. [...]... declarations with the addition of member functions and the public and private visibility labels In fact, C++ extends to structures the same features classes have The only difference is that the default access type for a structure is public, whereas the default type for the class is private C++ programmers This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register... by a program is called data hiding (C++ provides a third access-control keyword, protected, which we discuss when we cover class inheritance in Chapter 13, "Class Inheritance.") (See Figure 10.1.) Whereas data hiding may be an unscrupulous act in, say, a stock fund prospectus, it's a good practice in computing because it preserves the integrity of the data This document was created by an unregistered... class does with set_tot() Another example of encapsulation is the usual practice of placing class function definitions in a separate file from the class declaration OOP and C++ Object-oriented programming is a programming style that This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks you can use to some degree with any language Certainly, you... the more general features To begin, the C++ keyword class identifies this code as defining the design of a class The syntax identifies Stock as the type name for this new class This declaration enables us to declare variables, called objects, or instances, of the Stock type Each individual object represents a single holding For example, the declarations This document was created by an unregistered...This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks First, we have to think a bit about how to represent stocks We could take one share of stock... access structure members In essence, that example defines an abstract type that places the storage format and the function prototypes in a header file, hiding the actual data representation from main() C++, however, includes features specifically intended to implement the OOP approach, so it enables you to take the process a few steps further than you can with C First, placing the data representation... main() directly accesses a structure member, it violates the spirit of OOP, but it doesn't break any C language rules But trying to access directly, say, the shares member of a Stock object does break a C++ language rule, and the compiler will catch it Note that data hiding not only prevents you from accessing the data directly, but it also absolves you (in the roll as a user of the class) from needing... the details of the implementation from the design of the interface If you later find a better way to implement the data representation or the details of the member functions, you can change those This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks details without changing the program interface, and that makes programs much easier to maintain... the class Generally, a class specification has two parts: A class declaration, which describes the data component, in terms of data members, and the public interface, in terms of member functions This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks The class method definitions, which describe how certain class member functions are implemented... has class scope Other member functions of the Stock class can, if necessary, use the update() method without using the scope resolution operator That's because they belong to the same class, making This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com to register it Thanks update() in scope Using update() outside of the class declaration and method definitions, however, . abstraction, it is a short step to the user-defined type, which in C++ is a class design that implements that interface. This document was created by an unregistered ChmMagic, please go to http://www.bisenter.com. definitions in a separate file from the class declaration. OOP and C++ Object-oriented programming is a programming style that This document was created by an unregistered ChmMagic, please go to. C++ extends to structures the same features classes have. The only difference is that the default access type for a structure is public, whereas the default type for the class is private. C++