CPSC 427: Object-Oriented Programming

Similar documents
CPSC 427: Object-Oriented Programming

CPSC 427a: Object-Oriented Programming

Java Programming Lecture 7

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

COMP 110 Programming Exercise: Simulation of the Game of Craps

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

CS111: PROGRAMMING LANGUAGE II

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Study Guide to Exam 2

Lecture 5: Inheritance

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

C++ Inheritance and Encapsulation

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

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

CS111: PROGRAMMING LANGUAGE II

CPSC 427: Object-Oriented Programming

VIRTUAL FUNCTIONS Chapter 10

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

CPSC 427: Object-Oriented Programming

CS 162, Lecture 25: Exam II Review. 30 May 2018

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

COMP322 - Introduction to C++

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

Object-Oriented Principles and Practice / C++

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

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

ProgrammingAssignment #5: Let s Play Craps!

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object-Oriented Principles and Practice / C++

1. An object of a class can be treated as an object of its corresponding class.

STUDENT LESSON A14 Boolean Algebra and Loop Boundaries

Extending Classes (contd.) (Chapter 15) Questions:

Programming Assignment #4 Arrays and Pointers

CPSC 427: Object-Oriented Programming

Object Oriented Design

CPSC 427: Object-Oriented Programming

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

Lecture 13: more class, C++ memory management

Last Time: Rolling a Weighted Die

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

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

EAS230: Programming for Engineers Lab 1 Fall 2004

step is to see how C++ implements type polymorphism, and this Exploration starts you on that journey.

Department of Computer Science Undergraduate Events

8. Object-oriented Programming. June 15, 2010

CPSC 427a: Object-Oriented Programming

CS11 Introduction to C++ Fall Lecture 7

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

CPSC 427a: Object-Oriented Programming

CLASS DESIGN. Objectives MODULE 4

CPSC 427a: Object-Oriented Programming

Function Call Stack and Activation Records

ECE 122. Engineering Problem Solving with Java

CPSC 427: Object-Oriented Programming

Introduction to Java Programs for Packet #4: Classes and Objects

EE109 Lab Need for Speed

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Program #7: Let s Play Craps!

[ L5P1] Object-Oriented Programming: Advanced Concepts

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

Java Programming Lecture 6

Access and Non access Modifiers in Core Java Core Java Tutorial

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

Lecture 15: Object-Oriented Programming

VALLIAMMAI ENGINEERING COLLEGE

CIS 162 Project 4 Farkle (a dice game)

Public Class ClassName (naming: CPoint)

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

PROGRAMMING LANGUAGE 2

Inheritance. COMP Week 12

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Polymorphism and Interfaces. CGS 3416 Spring 2018

Inheritance, and Polymorphism.

CS 116 Week 8 Page 1

C Functions. 5.2 Program Modules in C

Chapter 4: Writing Classes

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

LAB: WHILE LOOPS IN C++

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Introduction to Computer Programming

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

CPSC 427: Object-Oriented Programming

Oracle. Java SE 8 Programmer I. 1z Version: Demo. [ Total Questions: 10] Web:

Object- Oriented Design with UML and Java Part I: Fundamentals

ECE 3574: Dynamic Polymorphism using Inheritance

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Basics of Programming with Python

INSTRUCTIONS TO CANDIDATES

Transcription:

CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 18 November 7, 2016 CPSC 427, Lecture 18 1/19

Demo: Craps Game Polymorphic Derivation (continued) Name Visibility CPSC 427, Lecture 18 2/19

Demo: Craps Game CPSC 427, Lecture 18 3/19

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 18 4/19

A Craps simulator Demo 18-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 18 5/19

Polymorphic Derivation (continued) CPSC 427, Lecture 18 6/19

Uses of polymorphism: Run-time variability Two types are closely related; differ only slightly. Example: Company has several different kinds of employees. Employee base class has a large and complicated payroll function. Payroll is same for all kinds of employees except for a function pay() that computes the actual weekly pay. Each employee kind has its own pay() function. Big payroll function is in base class. It calls pay() to get the actual pay for this Employee. CPSC 427, Lecture 18 7/19

Pure virtual functions Suppose we don t want B::f() and we never create instances of the base class B. Rather, we want every derived class to provide a definition for f(). We make B::f() into a pure virtual function by writing =0. class B { public: virtual int f()=0;... }; class U : B { virtual int f();... }; class V : B { virtual int f();... }; B* bp; A pure virtual function is sometimes called a promise. It tells the compiler that a construct like bp->f() is legal. The compiler requires every derived class to contain a method f(). CPSC 427, Lecture 18 8/19

Abstract classes An abstract class is a class with one or more pure virtual functions. An abstract class cannot be instantiated. It can only be used as the base for another class. The destructor can never be a pure virtual function but will generally be virtual. A pure abstract class is one where all member functions are pure virtual (except for the destructor) and there are no data members, Pure abstract classes define an interface à la Java. An interface allows user-supplied code to integrate into a large system. CPSC 427, Lecture 18 9/19

Name Visibility CPSC 427, Lecture 18 10/19

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 18 11/19

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 18 12/19

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 18 13/19

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 18 14/19

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 18 15/19

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 18 16/19

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 18 17/19

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 18 18/19

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 18 19/19