Copyright © Jim Fawcett Spring 2017

Slides:



Advertisements
Similar presentations
Object-Oriented PHP (1)
Advertisements

Classes Separating interface from implementation
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Programming Languages and Paradigms Object-Oriented Programming.
You gotta be cool. Access Functions and Utility Functions Preprocessor Wrapper Looking Ahead to Composition and Inheritance Object Size Class Scope and.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CITA 342 Section 1 Object Oriented Programming (OOP)
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Memory Management.
Appendix 1 - Packages Jim Fawcett copyright (c)
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2005.
CSE691 Software Models and Analysis.
CMSC 202 Polymorphism.
CSE687 – Object Oriented Design
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Abstract Data Types Programmer-created data types that specify
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
7. Inheritance and Polymorphism
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Intent (Thanks to Jim Fawcett for the slides)
This pointer, Dynamic memory allocation, Constructors and Destructor
Object Oriented Programming
CS212: Object Oriented Analysis and Design
CS212: Object Oriented Analysis and Design
Chapter 9 Classes: A Deeper Look, Part 1
Lecture 22 Inheritance Richard Gesick.
Built-In (a.k.a. Native) Types in C++
Object Oriented Programming
Programming Logic and Design Fourth Edition, Comprehensive
Polymorphism CMSC 202, Version 4/02.
CISC/CMPE320 - Prof. McLeod
Objects Managing a Resource
Fundaments of Game Design
Chapter 8: Class Relationships
Overview of C++ Polymorphism
Object-Oriented PHP (1)
Software Design Lecture : 38.
Chapter 8 - Design Strategies
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Programming to Interfaces
More C++ Classes Systems Programming.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Copyright © Jim Fawcett Spring 2017 CSE687 - Object Oriented Design class notes Encapsulation in C++ Version 3 Copyright © Jim Fawcett Spring 2017

Encapsulation Encapsulation Requires that functions, modules and classes: Have clearly defined external interfaces Hide implementation details Encapsulation is all about coupling – coupling happens through interfaces. Exactly what is an interface? private data private functions public functions Encapsulation

Function Interfaces Functions have four ways to interact with the external world: Parameter list – local coupling: Value parameters and const references are input only Functions with these parameters don’t have parametric side-affects. Non-const reference paramters and pointers are both input and output We say that a function with non-const reference parameters has side-affects. Returned items – local coupling: Return by value has no after-affects inside the function. Return by reference can change the state of an object to which the function belongs. Global data – remote coupling: Functions that use global data create remote and untrace-able side effects. Static local data – temporal coupling: An invocation affects later invocations. The strongest encapsulation is with only pass-by-value or constant reference in and out. However, the indirection allowed by non-constant references is just too powerful. We can’t live without it. We can, and should, live without global data and we should minimize the use of static local data. Encapsulation

C++ References versus Pointers Prefer returning C++ references over pointers. References provide access to objects. You only get to use the object’s interface with a reference. Pointers provide access to memory. Their proper use demands that the client understand the design of the function, and perhaps the class: Does the pointer point to an object or an array of objects? Does it point to the heap or some other persistant object? Whose responsibility is destruction? This is not a manifesto to eliminate all pointer use! Pointers do a great job of capturing relationships: Arrays, Graphs, trees, lists, repositories Their big brothers, iterators, are an essential part of the STL It is appropriate to return them from creational functions that act like sophisticated new calls. Encapsulation

Classes All the same issues hold for classes, via the member functions they provide. Classes also support three accessibility levels: public, protected, and private. Public members define the client interface and should be immutable – changing the interface breaks client designs. Protected members define an interface for derived classes only. They also should be immutable once some of the derived classes are no longer under your control. Private members consist of all those helper functions that manage the class’s internal state. For proper class encapsulation: Don’t use public or global data Encapsulate member functions well, as discussed in earlier slides. Decide carefully which functions will be public, protected, and private. Never make member data public. Encapsulation

Role of Constructors and Destructor Constructors and destructors Objects are created from a class pattern only by calling a constructor. Whenever the thread of execution leaves a scope, all objects created in that scope are destroyed. Part of that destruction is the execution of the objects’ destructor and the destructors of all its parts. A constructor’s job is to allocate whatever resources the class needs on startup. The destructor returns any allocated resources. These actions mean that the class encapsulates its own resource management. That is a very big deal! Encapsulation

Class Relationships Composition is the best encapsulated relationship: Only member functions of the class have access to the interface of private data member objects. Only member functions of the class and its derived classes have access to the interface of a protected data member object. Inheritance gives derived classes access to the protected data members of the base class. Behavior of a correct base class can be made incorrect by incorrect derived class objects. Using relationships may badly break encapsulation. If you pass an object of a class to a member function of an object of the same class, the receiving object has access to all the used object’s private and protected state. Friend relationships extend this behavior to objects of other classes. Encapsulation

Encapsulation

Bad Designs What makes a design bad? Robert Martin suggests[1]: Rigidity It is hard to change because every change affects too many other parts of the system. Fragility When you make a change, unexpected parts of the system break. Immobility It is hard to reuse a part of the existing software in another application because it cannot be disentangled from the current application. Many of these “Bad Design” issues stem very directly from poor encapsulation. Encapsulation

C++ Class Encapsulation Problem The C++ object model has the source text of a user of an object responsible for carrying the object’s type information by including a header file. If class A refers to class B, then A must include B’s header. This is the way a C++ compiler does static type checking. Remember, in contrast, that C# carries its type information in the assembly metadata of the object itself. A direct consequence of this fact is that, unless we do something special to prevent it, the client is welded to specific versions of the objects it uses. If the object changes anything in its class declaration, adding a new member datum, for example, then the client has to acquire its new header and recompile. Encapsulation

Programming to Interfaces The something special we do is to: Program to an Interface not an Implementation Use an Object Factory to create the object. Here, interface means more than the public interface of a class. An interface is an abstract base class, often a struct actually, containing at least one pure virtual function. It usually has no constructors and no data. It has a destructor, only to qualify it as virtual. It often has a static factory function so the client does not have to directly instantiate an object of the class it represents. This interface shields its clients from all of the implementation details of the class it represents. Encapsulation

Encapsulation

Fini Designing to Interfaces and using factory functions completely eliminates the coupling of client to the implementation of its serving objects. Here is an example, that may be of other interest as well: Interfaces Encapsulation

End of Presentation