1 / 41

Programming II

Programming II. You gotta be cool . C++ Stream Input/ Output. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error States Tying an Output Stream to an Input Stream. You my friend .

oma
Télécharger la présentation

Programming II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming II You gotta be cool 

  2. C++ Stream Input/ Output • Stream • Stream Output • Stream Input • Unformatted I/O with read, gcountand write • Stream Manipulators • Stream Format States • Stream Error States • Tying an Output Stream to an Input Stream

  3. You my friend A friend function of a class is defined outside that class’s scope, yet has the right to access the non-public (and public) members of the class. Standalone functions, entire classes or member functions of other classes may be declared to be friends of another class. Using friend functions can enhance performance. This section presents a mechanical example of how a friend function works. Later in this course, friend functions are used to overload operators for use with class objects (Chapter 6) and to create iterator classes. Objects of an iterator class can successively select items or perform an operation on items in a container class object. Objects of container classes can store items. Using friends is often appropriate when a member function cannot be used for certain operations, as we’ll see in Chapter 6.

  4. You my friend To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend. To declare all member functions of class ClassTwo as friends of class ClassOne, place a declaration of the form friend class ClassTwo; in the definition of class ClassOne.

  5. You my friend

  6. You my friend

  7. You my friend Friendship is granted, not taken—i.e., for class B to be a friend of class A, class A must explicitly declare that class B is its friend. Also, the friendship relation is neither symmetric nor transitive; i.e., if class A is a friend of class B, and class B is a friend of class C, you cannot infer that class B is a friend of class A (again, friendship is not symmetric), that class C is a friend of class B (also because friendship is not symmetric), or that class A is a friend of class C (friendship is not transitive).

  8. The this pointer We’ve seen that an object’s member functions can manipulate the object’s data. How do member functions know which object’s data members to manipulate? Every object has access to its own address through a pointer called this (a C++ keyword). The this pointer is not part of the object itself—i.e., the memory occupied by the this pointer is not reflected in the result of a sizeof operation on the object. Rather, the this pointer is passed (by the compiler) as an implicit argument to each of the object’s non-static member functions. Later section introduces static class members and explains why the this pointer is not implicitly passed to static member functions.

  9. The this pointer Objects use the this pointer implicitly (as we’ve done to this point) or explicitly to reference their data members and member functions. The type of the this pointer depends on the type of the object and whether the member function in which this is used is declared const. For example, in a nonconstant member function of class Employee, the this pointer has type Employee * const (a constant pointer to a nonconstant Employee object). In a constant member function of the class Employee, the this pointer has the data type const Employee * const (a constant pointer to a constant Employee object). The next example shows implicit and explicit use of the this pointer; later in this Chapter, we will see some substantial and subtle examples of using this.

  10. The this pointer

  11. The this pointer

  12. Objects as Members of Classes Previously, we saw how to pass arguments to the constructor of an object we created in main. Now we will see how an object’s constructor can pass arguments to member-object constructors via member initializers.

  13. Objects as Members of Classes The next program uses classes Date and Employee to demonstrate composition. Class Employee’s definition contains private data members firstName, lastName, birthDate and hireDate. Members birth-Date and hireDate are const objects of class Date, which contains private data members month, day and year. The Employee constructor’s header specifies that the constructor has four parameters (first, last, dateOfBirth and dateOfHire). The first two parameters are passed via member initializers to the string class constructor. The last two are passed via member initializers to the Date class constructor.

  14. Objects as Members of Classes Date.h

  15. Objects as Members of Classes Date.cpp

  16. Objects as Members of Classes Date.cpp

  17. Objects as Members of Classes Employee.h

  18. Objects as Members of Classes Employee.cpp

  19. Objects as Members of Classes Employee.cpp

  20. Objects as Members of Classes Employee Constructor’s Member Initializer List The colon (:) following the constructor’s header (Employee.cpp, line 9) begins the member initializer list. The member initializers specify the Employee constructor parameters being passed to the constructors of the string and Date data members. Parameters first, last, dateOf-Birth and dateOfHire are passed to the constructors for objects firstName’s, lastName , birthDate and hireDate, respectively. Again, member initializers are separated by commas.

  21. Objects as Members of Classes Date Class’s Default Copy Constructor As you study class Date (Date.h), notice that the class does not provide a constructor that receives a parameter of type Date. So, why can the Employee constructor’s member initializer list initialize the birthDate and hireDate objects by passing Date object’s to their Date constructors? This is because the compiler provides each class with a default copy constructor that copies each data member of the constructor’s argument object into the corresponding member of the object being initialized.

  22. Objects as Members of Classes Testing Classes Date and Employee Themain.cpp creates two Date objects (lines 6–7) and passes them as arguments to the constructor of the Employee object created in line 8. Line 10 outputs the Employee object’s data. When each Date object is created in lines 6–7, the Date constructor defined in lines 7–19 of Date.cpp displays a line of output to show that the constructor was called (see the first two lines of the sample output). [Note: Line 8 of themain.cpp causes two additional Date constructor calls that do not appear in the program’s output. When each of the Employee’s Date member object’s is initialized in the Employee constructor’s member initializer list, the default copy constructor for class Date is called. Since this constructor is defined implicitly by the compiler, it does not contain any output statements to demonstrate when it’s called.]

  23. Objects as Members of Classes themain.cpp

  24. Static Class Members There is an important exception to the rule that each object of a class has its own copy of all the data members of the class. In certain cases, only one copy of a variable should be shared by all objects of a class. A static data member is used for these and other reasons. Such a variable represents “class-wide” information (i.e., a property that is shared by all instances and is not specific to any one object of the class).

  25. Static Class Members (Employee.h)

  26. Static Class Members (Employee.cpp)

  27. Static Class Members (Employee.cpp)

  28. Static Class Members

  29. Static Class Members

  30. Proxy Classes Two of the fundamental principles of good software engineering are separating interface from implementation and hiding implementation details. We strive to achieve these goals by defining a class in a header and implementing its member functions in a separate implementation file. As we see earlier, however, headers do contain a portion of a class’s implementation and hints about others. For example, a class’s private members are listed in the class definition in a header, so these members are visible to clients, even though the clients may not access the private members. Revealing a class’s private in this manner potentially exposes proprietary information to clients of the class.

  31. Proxy Classes We now introduce the notion of a proxy class that allows you to hide even the private data of a class from clients of the class. Providing clients of your class with a proxy class that knows only the public interface to your class enables the clients to use your class’s services without giving the clients access to your class’s implementation details. Implementing a proxy class requires several steps, which we demonstrate next. First, we create the class definition for the class that contains the proprietary implementation we would like to hide. Our example class, called Implementation. The proxy class here is Interface.

  32. Proxy Classes Class Implementation provides a single private data member called value (the data we would like to hide from the client), a constructor to initialize value and functions setValue and getValue.

  33. Proxy Classes (Implementation.h)

  34. Proxy Classes We define a proxy class called Interface with an identical public interface (except for the constructor and destructor names) to that of class Implementation. The proxy class’s only private member is a pointer to an Implementation object. Using a pointer in this manner allows us to hide class Implementation’s implementation details from the client. Notice that the only mentions in class Interface of the proprietary Implementation class are in the pointer declaration in line 11 and in line 1, a forward class declaration. (Interface.h)

  35. Proxy Classes When a class definition uses only a pointer or reference to an object of another class (as in this case), the class header for that other class (which would ordinarily reveal the private data of that class) is not required to be included with #include. This is because the compiler doesn’t need to reserve space for an object of the class. The compiler does need to reserve space for the pointer or reference. The sizes of pointers and references are characteristics of the hardware platform on which the compiler runs, so the compiler already knows those sizes. You can simply declare that other class as a data type with a forward class declaration (line 1) before the type is used in the file. (Interface.h)

  36. Proxy Classes (Interface.h)

  37. Proxy Classes The member-function implementation file for proxy class Interface (Interface.cpp) is the only file that includes the header Implementation.h containing class Implementation. The file Interface.cpp is provided to the client as a precompiled object code file along with the header Interface.h that includes the function prototypes of the services provided by the proxy class. Because file Interface.cpp is made available to the client only as object code, the client is not able to see the interactions between the proxy class and the proprietary class (lines 5, 12, 17 and 22). The proxy class imposes an extra “layer” of function calls as the “price to pay” for hiding the private data of class Implementation. Given the speed of today’s computers and the fact that many compilers can inline simple function calls automatically, the effect of these extra function calls on performance is often negligible.

  38. Proxy Classes (Interface.cpp)

  39. Proxy Classes Themain.cpp tests class Interface. Notice that only the header for Interface is included in the client code (line 2)—there is no mention of the existence of a separate class called Implementation. Thus, the client never sees the private data of class Implementation, nor can the client code become dependent on the Implementation code.

  40. Proxy Classes (themain.cpp)

More Related