Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Using const in C++ Classes In the presence of pointers we must take steps to ensure the integrity of the object Can use const method definitions The.

Similar presentations


Presentation on theme: "1 Using const in C++ Classes In the presence of pointers we must take steps to ensure the integrity of the object Can use const method definitions The."— Presentation transcript:

1 1 Using const in C++ Classes In the presence of pointers we must take steps to ensure the integrity of the object Can use const method definitions The keyword cost is appended at the end of the class definition Const methods can not alter the state of the object Const methods may only call other const methods Must be consistent with the use of const. Either always use it, or never use it. class Stack { int count() const; // … };

2 2 Class Interface Design Standards Class Interface “Rules of Thumb” Make all data ( class properties) private Protect the object integrity Only allow access to an object data through member functions: Accessor methods return data to caller, mutator methods set object data via call request. Use const where appropriate, especially when returning pointers to internal data to class users Make any helper routines that do not directly support class behavior to be private methods Use explicit constructors and destructors Use inline functions whenever possible

3 3 Class Interfaces Goal of an object oriented program is to hide the implementation details inside of classes Algorithms Data Structures Class Properties

4 4 Class Interface Design Goals of the class Interface design Protect class data from direct modification by making all data ( properties) private Ensure that the objects created from the class can only be in valid states Enables the implementation to be changed without impacting class user. ( Keep the same interface ) Hide implementation details inside of the class Prevent class users from making assumptions about the implementation Promotes reusability

5 5 Private Data Recall that one of the primary goals of an object is to protect the integrity of the object Prohibit the object users from directly modifying the state of the object One of the primary goals of a class is to construct code that protects the integrity of the class properties If you feel you need to make class properties public … think again ! The best way to protect the integrity of the objects that are created from a class is to make all class properties private. Provide accessor/mutator methods Allows for controlled modification of an objects state

6 6 Encapsulation Goal of OO Interface design is Encapsulation Encapsulation is the act of hiding all of the algorithms, data structures and class properties inside of the class. Class behavior is fully controlled through a well thought out public class interface Encapsulation is an inconvenience to the class implementer, however Encapsulation is a key attribute in reducing the complexity of building and extending complex software systems

7 7 Class Designers and Class Users The design and implementation of classes must be approached from two different points of view The actual class design How the class is going to be used relying on the public interface

8 8 The “cast” Class designer: The programmer who designs and implements classes Class user: Another programmer ( or the same ) that uses the classes that are provided by the class designer End-user: The user of the system. The end- user ( or customer ) usually does not know anything about the underlying source code As class designers we must build classes with interfaces that are suitable for the class user.

9 9 Class Designer Class designer performs a service for the class user Must take into account the needs of the class user If a class design is too restrictive then the class then the class user will not want to use the class design The class designer must implement well thought, robust classes that use efficient algorithms. Quality products.

10 10 Class User Class user desires to understand and use the class without having to comprehend the internals of the actual class implementation Design a set of services large enough to solve their programming task while being small enough to be easy to comprehend Class users typically only need to understand the public class interface

11 11 Designing a Class Interface General criteria for deterring the quality of a class interface Cohesion Primitive Operations Completeness Convenience Consistency Tradeoffs are sometimes necessary because some of the criteria mentioned above are in conflict with each other Completeness vs. Convenience Primitive Operations vs. Convenience

12 12 Abstraction Understanding abstraction is essential to understanding good class design practices. Definition(Booch): An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crispy defined conceptual boundaries, relative to the perspective of the viewer. Abstraction focus on the outside view of the object Used to separate an object’s essential behavior from its internal implementation

13 13 Abstraction Abstraction should capture the entire behavior of an object No more and no less Should not produce any sided effects that go beyond the scope of the abstraction An abstraction focuses upon the essential characteristics of some object, relative to the perspective of the viewer. Inside vs. outside the box. The correct level of abstraction is governed by the nature of the system.

14 14 Cohesion A class should describe a single abstraction The class operations must be at an appropriate and consistent level of abstraction for the application The class interface should provide an interface that supports a collection of operations that fit together to support a single coherent purpose

15 15 CohesionExample What operations do not belong to the class? Sometimes it is a close call. class cat { public: void eat(); void walk(); void makeSound(); void fly(); };

16 16 Primitive Operations Class operations in the public class interface should be primitive Should not be decomposable into smaller operations Primitive operations allow the user to mix and match operations to solve a particular problem Primitive operations provide a large amount of flexibility to the class user Class user solves a logical problem by calling a sequence of primitive operations

17 17 Primitive Operations Example Consider the Mailbox class example. Suppose that the Mailbox class provides a method to print a message based on a message ID void MailBox :: printMsg( int I ) { // print message } The user could call the printMsg() method in the Mailbox class. However, the Mailbox class also provides a GetMsg() method. A possible better approach using primitive operations: Get a message from the Mailbox Ask the message object to print itself This solution is flexible because it enables the Message object to worry about formatting and rendering the message.

18 18 Completeness The set of class operations must be complete The set of operations supported by the class must supply all operations that make sense at the desired level of abstraction Example: The class interface of the Mailbox class does not support a behavior for enumerating a list of the message headers that are being managed by the Mailbox object

19 19 Completeness example This is a very desirable feature that makes sense for the Mailbox class to support. Thus the following operation should be added to the Mailbox class for the sake of completeness: vector & getMessageHeaders() const getMessageHeaders() would return a vector of the message headers to the user of Mailbox class

20 20 Convenience A class that consists entirely of primitive operations may be complete but not convenient to use. Must take into account how the class is to be used and add additional operations that would be convenient for the class user. Example: getMessageHeaders() Do not overuse adding convenient methods to the class. Too many additional operations might make the class interface overly complex The convenience aspect of class interface design often contradicts one or more other aspects of good class interface design. Use convenience only where it makes sense !

21 21 Consistency Users of a class typically do not code the class Using unfamiliar ( another persons) code is often difficult Not familiar with the persons coding style In order to help your class users, all operations supported by your classes should consistent with respect to: Method names ( use a naming convention) Arguments and return values How are arguments passed, using reference or using pointers. Consistent use of the return values: Are they used to return data, or status (success/failure) codes

22 22 Consistency Your class interface should also maintain a consistent level of abstraction Be careful when mixing detailed and general behavior in the same class

23 23 Classifying the Behavior supported by the Class Interface Constructor Recall that the purpose of a constructor is to initialize data fields in an object Every class should have at least one constructor Most classes should have a default constructor More than one constructor may be provided Destructor A destructor releases dynamic resources acquired by an object during its lifetime A destructor is not necessary if the class does not allocate or acquire any dynamic resources Java does not have destructors due to garbage collection mechanism

24 24 Classifying the Behavior Supported by the Class interface Accessors Accessors compute or return a value from an object without modifying the object Accessors may simply return a property or may make a calculation based on the state of the object Accessor methods usually declared const

25 25 Classifying the Behavior Supported by the Class interface Mutators A mutator is any operation that modifies the object in some way Mutators may simply update a class property Mutators may result in calculations being performed that result in the update of one or more properties of the object Mutator allow safe updates to object’s properties preventing malicious updates which may put object into invalid state

26 26 Classifying the Behavior Supported by the Class interface Comparison Operators A comparison operator compares an object with another object of the same type to determine if they are identical, or whether one is less than, or greater than the other

27 27 Classifying the Behavior Supported by the Class interface Iterators Used in classes that manage a collection of items Access to items via a key or an iteration protocol Iteration protocol enables the object to Point to the first item Get the item currently being pointed at Move to the next item Identify when the object points to the last item

28 28 Copying and Cloning An object typically needs to support copy and cloning operations May return a copy of itself (Cloning) May set itself to be a copy of another object (Copying) Example: operator=

29 29 Input and Output Output operations render the desired output onto some output medium Screen Printer Window File Network Interface Card Output operations do not modify the object Usually input operations result in a modification of the objects state Input operations accept input from some medium Screen, Printer, Window, File, Net work Card


Download ppt "1 Using const in C++ Classes In the presence of pointers we must take steps to ensure the integrity of the object Can use const method definitions The."

Similar presentations


Ads by Google