A student was asked to point out interface elements in this code: Answer: cout. What is wrong?
Clarification of the concept of INTERFACE The interface we ve been talking about in OOP is not the man-machine interface between a program and its user. Rather, it is the interface between a class and other classes or user code that interact with that class. example
A student was asked to point out interface elements in this code: This is the interface!
Clarification of the concept of INTERFACE cout is not part of the interface; it is an object (of the ostream class). Its interface contains these and other tools : the << operator the write and width member functions the precision member function the rdbuf pointer
Ch. 5: Hiding the Implementation
Remember from ch.4 A typical C library contains structs and associated functions to act on those structs. What we have changed from C to C++:
In C++, we take the functions that are conceptually associated with the struct and make them literally associated by: Putting the function declarations inside the scope of the struct These are now called member functions or methods.
Changing the way functions are called: s.add(); Eliminating the passing of the structure address as the first argument Adding a new type name to the program (so we don t have to create a typedef for the struct tag).
Further problems with C libraries: control and safety text When you create a library, you establish a relationship with the client programmer who uses that library to build an application or another library. In a C struct, as with most things in C, there are no rules. Client programmers can do anything they want with that struct, and there s no way to force any particular behaviors.
text For example, even though you saw in the last chapter the importance of the functions named initialize( ) and cleanup( ), the client programmer has the option not to call those functions. And even though you would really prefer that the client programmer not directly manipulate some of the members of your struct, in C there s no way to prevent it.
There are two reasons for controlling access to members: text 1 - To keep the client programmer s hands off tools they shouldn t touch, tools that are necessary for the internal machinations of the data type, but not part of the interface the client programmer needs to solve their particular problems. This is actually a service to client programmers because they can easily see what s important to them and what they can ignore.
There are two reasons for controlling access to members: text 2 - To allow the library designer to change the internal workings of the structure without worrying about how it will affect the client programmer. In the Stack example in the last chapter, you might want to allocate the storage in big chunks, for speed, rather than creating new storage each time an element is added. If the interface and implementation are clearly separated and protected, you can accomplish this and require only a relink by the client programmer. And this is why we have
C++ access control C++ introduces three new keywords to set the boundaries in a structure: public, private, and protected. These access specifiers are used only in a struct (or class) declaration, and they change the boundary for all the declarations that follow them. Whenever you use an access specifier, it must be followed by a colon. example
By default, the members of a struct are public
C++ access control For backward compatibility, C++ accepts the keyword struct, although in new programs we shall always use class instead!:
In a class, however, the members are private by default
In a struct, we can also control the access with private and public
QUIZ: Is either of the new statements legal? a.i = 42; b.j = 43; }
QUIZ What s wrong in this code?
Solution While it s OK for a member function to access the private member width a global function isn t allowed to access it.