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.

Slides:



Advertisements
Similar presentations
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Advertisements

Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Object-Oriented PHP (1)
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Rossella Lau Lecture 5, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 5: Class construction  Encapsulation 
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 11: Classes and Data Abstraction
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
C++ fundamentals.
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Access Control Problem A primary consideration in object- oriented design is to “separate the things that change from the things that stay.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Defining New Types Lecture 21 Hartmut Kaiser
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Learners Support Publications Classes and Objects.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Structures and Classes
Classes (Part 1) Lecture 3
Programming Logic and Design Seventh Edition
Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
Corresponds with Chapter 7
The Object-Oriented Thought Process Chapter 05
Classes and Objects.
Defining Classes and Methods
COP 3330 Object-oriented Programming in C++
Object-Oriented PHP (1)
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

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 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 Class Interfaces Goal of an object oriented program is to hide the implementation details inside of classes Algorithms Data Structures Class Properties

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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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