©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Access Control.

Slides:



Advertisements
Similar presentations
Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Advertisements

Stereotypes Stereotypes provide the capability to create a new kind of modeling element. –They can be used to classify or mark modeling elements. –A type.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
C++ Inheritance Gordon College CPS212. Basics OO-programming can be defined as a combination of Abstract Data Types (ADTs) with Inheritance and Dynamic.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Type Inheritance and Polymorphism.
CS-2135 Object Oriented Programming
Object-Oriented PHP (1)
1 © Wolfgang Pelz UML3 UML 3 Notations describe how to use reusable software. Package Component Deployment Node.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance and Polymorphism CS351 – Programming Paradigms.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Namespaces.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CS212: Object Oriented Analysis and Design Lecture 5: Classes and Objects - II.
Copyright © Curt Hill Generic Classes Template Classes or Container 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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Programming in Java CSCI-2220 Object Oriented Programming.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 Class Diagrams. 2 Overview Class diagrams are the most commonly used diagrams in UML. Class diagrams are for visualizing, specifying and documenting.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Overview of C++ Templates
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Inspired by the Oulipu. The 3 Tenets of OO Encapsulation Polymorphism Inheritance.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes (Part 1) Lecture 3
Object-Oriented Analysis and Design
7. Inheritance and Polymorphism
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
11.1 The Concept of Abstraction
Object Oriented Analysis and Design
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Object-Oriented Programming
Packages and Interfaces
More Object-Oriented Programming
Object-Oriented Programming
Java Programming, Second Edition
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Computer Science II for Majors
Lecture 9 Concepts of Programming Languages
Presentation transcript:

©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Access Control

©Fraser Hutchinson & Cliff Green What is Access Control? Three tenets of O-O programming: –Encapsulation –Inheritance –Polymorphism (typically runtime / dynamic binding) Access control concerned with encapsulation –What do we want visible? –What do we want to hide? –From whom?

©Fraser Hutchinson & Cliff Green C++ Access Control C++ supports three levels of access control –Public : visible to all –Protected: visible to derived classes –Private: visible only to the class

©Fraser Hutchinson & Cliff Green Access Control Uses Intended to support design policy –Aids in the communication of a design by delineating what is intended to be visible –Can be used to promote loose-coupling (hard to couple to what you cannot see) C++ access control is not a runtime security mechanism, but a compile-time design enforcement mechanism

©Fraser Hutchinson & Cliff Green Sibling Access class Car { protected: double getMilesPerGallon () const { /* … */ } // … }; namespace Honda { class Civic : public Car { public: double getResaleValue () const { double mpg = getMilesPerGallon (); // ok // … } bool shouldTradeFor (Car const& car) { double newMPG = car.getMilesPerGallon (); // ILLEGAL! // … } }; }

©Fraser Hutchinson & Cliff Green Sibling Access Why is this illegal? –Protected and private methods can be accessed between different objects of the same class –Base class protected method can be called from within derived object method –However, siblings should not have access to the other’s private (or protected) methods; for example, Civic should not be able to call getMilesPerGallon on a Saab object

©Fraser Hutchinson & Cliff Green Explanation from ARM The C++ Annotated Reference Manual (ARM) explains this further: –When a friend or a member function of a derived class references a protected nonstatic member of a base class, an access check applies in addition to those described earlier... Except when forming a pointer to member, the access must be through a pointer to, reference to, or object of the derived class itself (or any class derived from that class)...

©Fraser Hutchinson & Cliff Green Friend Declarations A class, global function, or method may be declared to be a friend of a class Friend has unrestricted access to private and protected members of the class Friend declarations are not inherited or transitive - a friend of a friend does not automatically become a friend, a friend of a parent does not automatically become a friend

©Fraser Hutchinson & Cliff Green Example Friend Declarations class Employee { // friend functions and classes are // given unrestricted access to all // public, private and protected // attributes and methods friend class Secretary; friend void ::IRS_Audit(); friend void Boss::Spy(); };

©Fraser Hutchinson & Cliff Green More on Friends You can specify who your friends are You cannot limit what friends have access to –Friends can access everything an instance of your class can Often, but not always, use of friends is indicative of a weak design

©Fraser Hutchinson & Cliff Green Template Friends Friends can be declared in template classes, to generate many friends: template class Bar { }; template class Foo { //... friend class Bar ; };

©Fraser Hutchinson & Cliff Green Iterators Iterator classes provide ideal example where friend relationships can actually produce better design Iterators require intimate knowledge of the container class it is iterating against A container typically needs one outside class (usually under control of the container designer) with intimate knowledge of the container implementation details; outside users need only know container interface details

©Fraser Hutchinson & Cliff Green Std Lib List Container In the example of std::list, every user of the class accesses the list through its non data-revealing / non implementation- revealing public interface Iterator is needed to access any elements

©Fraser Hutchinson & Cliff Green Iterator Design Drivers Why are the iterator classes separate, nested classes rather than just exposing data within the list's interface? Is one iterator enough? Will there be multiple clients to the container? –Multiple iterators are needed Will standard algorithms be used on the container? –Iterators allow generic algorithms that work with any container –Algorithm code is insulated from binding to members of any specific container

©Fraser Hutchinson & Cliff Green Iterator Design Drivers Will there ever be a need to change the underlying container? –Iterators reduce reliance on a specific class interface –Underlying container can be changed and the application code is unaffected

©Fraser Hutchinson & Cliff Green Iterators Are Your Friends All of the above pushes for the use of iterators –“a window into a container” –Client code still has control over the container, regardless of what really goes on behind the scenes Iterators are an excellent example of selective exposure –Clients get what they need and only what they need –Many functions only need to access container iterators, not the container itself –Decouples client code from the implementation of the containers

©Fraser Hutchinson & Cliff Green Summary C++ access control and friends are all aspects of the public interface Public interface defines what a type can do, to the users of that type Separating the interface from the implementation is crucial to good software design The responsibilities / functionality / interface of a type (class) lies at the heart of O-O programming