CPSC 427: Object-Oriented Programming

Similar documents
CPSC 427a: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

ECE 122. Engineering Problem Solving with Java

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

CPSC 427a: Object-Oriented Programming

CPSC 427a: Object-Oriented Programming

Mat 2170 Week 9. Spring Mat 2170 Week 9. Objects and Classes. Week 9. Review. Random. Overloading. Craps. Clients. Packages. Randomness.

Student Responsibilities. Mat 2170 Week 9. Notes About Using Methods. Recall: Writing Methods. Chapter Six: Objects and Classes

CPSC 427: Object-Oriented Programming

Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

C++ STREAMS; INHERITANCE AS

COMP 110 Programming Exercise: Simulation of the Game of Craps

Overriding Variables: Shadowing

Chapter 4: Writing Classes

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Study Guide to Exam 2

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

Faculty of Engineering Computer Engineering Department Islamic University of Gaza Network Lab # 7 Permissions

Binghamton University. CS-140 Fall Dynamic Types

CSE 431S Type Checking. Washington University Spring 2013

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

CSE 12, Week Six, Lecture Two Discussion: Getting started on hw7 & hw8

CPSC 427: Object-Oriented Programming

Function Call Stack and Activation Records

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

Object-Oriented Programming. Lecture 2 Dr Piotr Cybula

1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types

C++ Inheritance and Encapsulation

Inheritance. COMP Week 12

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

C++ Important Questions with Answers

ProgrammingAssignment #5: Let s Play Craps!

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

CPSC 427: Object-Oriented Programming

Inheritance, and Polymorphism.

Inheritance and Polymorphism

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

What is Inheritance?

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Lecture 19. Topics: Chapter 9. Simulation and Design Moving to graphics library Unit Testing 9.5 Other Design Techniques

Introduction to OOP. Procedural Programming sequence of statements to solve a problem.

Program #7: Let s Play Craps!

void insert( Type const & ) void push_front( Type const & )

EMBEDDED SYSTEMS PROGRAMMING OO Basics

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Inheritance. Transitivity

OBJECTS AND CLASSES. Examples. You and I are instances of the class of objects known as people.

Instantiation of Template class

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CPSC 427: Object-Oriented Programming

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Functions and Recursion

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

More C++ : Vectors, Classes, Inheritance, Templates

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

CHAPTER 9 INHERITANCE. 9.1 Introduction

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

CompSci 125 Lecture 20. Inheritance: Introduction, Overrides UML: Introduction to Class Diagrams

Lecture 16: Static Semantics Overview 1

Chapter 10 Classes Continued. Fundamentals of Java

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Programming 2. Object Oriented Programming. Daniel POP

Project Compiler. CS031 TA Help Session November 28, 2011

3. Basic Concepts. 3.1 Application Startup

JavaScript: Sort of a Big Deal,

Concept as a Generalization of Class and Principles of the Concept-Oriented Programming

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

ECE 122. Engineering Problem Solving with Java

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Administrivia. CMSC433, Fall 2001 Programming Language Technology and Paradigms. Administrivia (cont.) Project 1. Copy constructor.

Object-Oriented Principles and Practice / C++

CE221 Programming in C++ Part 1 Introduction

Inheritance STL. Entity Component Systems. Scene Graphs. Event Systems

G52CPP C++ Programming Lecture 13

Overview. Elements of Programming Languages. Objects. Self-Reference

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

Advanced UML Class Models

Java: advanced object-oriented features

ITI Introduction to Computing II

General Computer Science II Course: B International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

CPSC 427: Object-Oriented Programming

Increases Program Structure which results in greater reliability. Polymorphism

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Transcription:

CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 17 March 31, 2016 CPSC 427, Lecture 17 1/29

Name Visibility Demo: Craps Game CPSC 427, Lecture 17 2/29

Name Visibility CPSC 427, Lecture 17 3/29

Private derivation (default) class B : A {... }; specifies private derivation of B from A. A class member inherited from A become private in B. Like other private members, it is inaccessible outside of B. If public in A, it can be accessed from within A or B or via an instance of A, but not via an instance of B. If private in A, it can only be accessed from within A. It cannot even be accessed from within B. CPSC 427, Lecture 17 4/29

Private derivation example Example: class A { private: int x; public: int y; }; class B : A {... f() {... x++;...} // privacy violation }; //-------- outside of class definitions -------- A a; B b; a.x // privacy violation a.y // ok b.x // privacy violation b.y // privacy violation CPSC 427, Lecture 17 5/29

Public derivation class B : public A {... from A. }; specifies public derivation of B A class member inherited from A retains its privacy status from A. If public in A, it can be accessed from within B and also via instances of A or B. If private in A, it can only be accessed from within A. It cannot even be accessed from within B. CPSC 427, Lecture 17 6/29

Public derivation example Example: class A { private: int x; public: int y; }; class B : public A {... f() {... x++;...} // privacy violation }; //-------- outside of class definitions -------- A a; B b; a.x // privacy violation a.y // ok b.x // privacy violation b.y // ok CPSC 427, Lecture 17 7/29

The protected keyword protected is a privacy status between public and private. Protected class members are inaccessible from outside the class (like private) but accessible within a derived class (like public). Example: class A { protected: int z; }; class B : A {... f() {... z++;...} // ok }; CPSC 427, Lecture 17 8/29

Protected derivation class B : protected A {... derivation of B from A. }; specifies protected A public or protected class member inherited from A becomes protected in B. If public in A, it can be accessed from within B and also via instances of A but not via instances of B. If protected in A, it can be accessed from within A or B but not from outside. If private in A, it can only be accessed from within A. It cannot be accessed from within B. CPSC 427, Lecture 17 9/29

Surprising example 1 1 class A { 2 protected: 3 int x; 4 }; 5 class B : public A { 6 public: 7 int f() { return x; } // ok 8 int g(a* a) { return a->x; } // privacy violation 9 }; Result: tryme1.cpp: In member function int B::g(A*) : tryme1.cpp:3: error: int A::x is protected tryme1.cpp:9: error: within this context CPSC 427, Lecture 17 10/29

Surprising example 2: contrast the following 1 class A { }; 2 class B : public A {}; // <-- public derivation 3 int main() { A* ap; B* bp; 4 ap = bp; } Result: OK. 1 class A { }; 2 class B : private A {}; // <-- private derivation 3 int main() { A* ap; B* bp; 4 ap = bp; } Result: tryme2.cpp: In function int main() : tryme2.cpp:4: error: A is an inaccessible base of B CPSC 427, Lecture 17 11/29

Surprising example 3 1 class A { protected: int x; }; 2 class B : protected A {}; 3 int main() { A* ap; B* bp; 4 ap = bp; } Result: tryme3.cpp: In function int main() : tryme3.cpp:4: error: A is an inaccessible base of B CPSC 427, Lecture 17 12/29

Names, Members, and Contexts Data and function names can be declared in many different contexts in C++: in a class, globally, in function parameter lists, and in code blocks (viz. local variables). Often the same identifier will be declared multiple times in different contexts. Two steps to determining the meaning of an occurrence of an identifier: 1. Determine which declaration it refers to. 2. Determine its accessibility according to the privacy rules. CPSC 427, Lecture 17 13/29

Declaration and reference contexts Every reference x to a class data or function member has two contexts associated with it: The declaration context is the context in which the referent of x (the thing that x refers to) appears. The reference context is the context in which the reference x appears. Accessibility rules apply to class data and function members depend on both the declaration context and the reference context of a reference x. CPSC 427, Lecture 17 14/29

Declaration context example Example: int x = 3; class A { int x; void f(int x) {...} void g() {int x;... } }; // declaration context: global // declaration context: A // declaration context: parameter // declaration context: block local CPSC 427, Lecture 17 15/29

Reference context example class A { int x; int f() {return x;} int g(a* p) {return p->x;} }; int main() { A obj; obj.x; } // reference context A // reference context A // reference context global All three commented occurrences of x have declaration context A because all three refer to A::x, the data member declared in class A. CPSC 427, Lecture 17 16/29

Inside and outside class references A reference x to a data/function member of class A is inside class A if the reference context of x is A; outside class A otherwise. For simple classes: an inside reference x is always valid. an outside reference x is valid iff the referent is public. CPSC 427, Lecture 17 17/29

Examples References to A::x class A { int x; int f() { return x; } int g(a* p) { return p->x; } int h(); }; int A::h () { return x; } #include <iostream> int main() { A aobject; std::cout << aobject.x; }; // inside // inside // inside // outside CPSC 427, Lecture 17 18/29

Inherited names In a derived class, names from the base class are inherited by the derived class, but their privacy settings are altered as described in the last lecture. The result is that the same member exists in both classes but with possibly different privacy settings. Question: Which privacy setting is used to determine visibility? Answer: The one of the declaration context of the referent. CPSC 427, Lecture 17 19/29

Inheritance example class A { protected: int x; }; class B : private A { int f() { return x; } int g(a* p) { return p->x; } }; // ok, x is inside B // not okay, x is outside A Let bb be an instance of class B. Then bb contains a field x, inherited from class A. This field has two names A::x and B::x. The names are distinct and may have different privacy attributes. In this example, A::x is protected and B::x is private. First reference is okay since the declaration context of x is B. Second reference is not since the declaration context of x is A. Both occurrences have reference context B. CPSC 427, Lecture 17 20/29

Inaccessible base class A base class pointer can only reference an object of a derived class if doing so would not violate the derived class s privacy. Recall surprising example 2 (bottom): 1 class A { }; 2 class B : private A {}; // <-- private derivation 3 int main() { A* ap; B* bp; 4 ap = bp; } The idea is that with private derivation, the fact that B is derived from A should be completely invisible from the outside. With protected derivation, it should be completely invisible except to its descendants. CPSC 427, Lecture 17 21/29

Visibility rules Every class member has one of four privacy attributes: public, protected, private, or hidden. These attributes determine the locations from which a class member can be seen. public members can be seen from any location. protected members can be seen from inside the class or its children. private members can only be seen from inside the class. hidden members cannot be seen at all. CPSC 427, Lecture 17 22/29

Explicit privacy attributes The privacy attributes for declared class members are given explicitly by the privacy keywords public, protected, and private. There is no way to explicitly declare a hidden member. Example: class A { private: int x; protected: int y; public: int z; }; CPSC 427, Lecture 17 23/29

Implicit privacy attributes Inherited class members are assigned implicit privacy attributes based on their attributes in the parent class and by the kind of derivation, whether public, protected, or private. 1. If the member is public in the parent class, then its attribute in the child class is given by the kind of derivation. 2. If the member is protected in the parent class, then its attribute in the child class is protected for public and protected derivation, and private for private derivation. 3. If the member is private or hidden in the parent class, then it is hidden in the child class. CPSC 427, Lecture 17 24/29

Implicit privacy chart Below is a revision of the chart presented in lecture 10. Attribute in base class Kind of Derivation public protected private public public protected private protected protected protected private private hidden hidden hidden hidden hidden hidden hidden Attribute in derived class. CPSC 427, Lecture 17 25/29

Summary 1. All members of the base class are inherited by the derived class and appear in every instantiation of that class. 2. All inherited members receive implicitly defined privacy attributes. 3. Visibility of all data members is determined solely by their privacy attributes. 4. Public and protected base class variables are always visible within a derived class. 5. Private and hidden base class variables are never visible in the derived class. 6. The kind of derivation never affects the visibility of inherited members in the derived class; only their implicit attributes. CPSC 427, Lecture 17 26/29

Demo: Craps Game CPSC 427, Lecture 17 27/29

Game Rules The player (known as the shooter) rolls a pair of fair dice 1. If the sum is 7 or 11 on the first throw, the shooter wins; this event is called a natural. 2. If the sum is 2, 3, or 12 on the first throw, the shooter loses; this event is called craps. 3. If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, this number becomes the shooter s point. The shooter continues rolling the dice until either she rolls the point again (in which case she wins) or rolls a 7 (in which case she loses). (From http://www.math.uah.edu/stat/games/craps.html) CPSC 427, Lecture 17 28/29

A Craps simulator Demo 17-Craps illustrates the use of derived classes in order to allow the simulator to work with both random dice and prerecorded dice throws stored in a file. CPSC 427, Lecture 17 29/29