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

Similar documents
CS250 Final Review Questions

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

Inheritance (Deitel chapter 9)

Polymorphism Part 1 1

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

CS250 Final Review Questions

OBJECT ORIENTED PROGRAMMING

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

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

Inheritance and Polymorphism

What is an algorithm?

Chapter 20 - C++ Virtual Functions and Polymorphism

Inheritance and Polymorphism

Polymorphism. Arizona State University 1

ECE 3574: Dynamic Polymorphism using Inheritance

Chapter 9 - Object-Oriented Programming: Polymorphism

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

Reusing Classes. Hendrik Speleers

Chapter 14 Abstract Classes and Interfaces

Data Structures and Other Objects Using C++

CS1004: Intro to CS in Java, Spring 2005

CS105 C++ Lecture 7. More on Classes, Inheritance

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

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

Chapter 6. Object- Oriented Programming Part II

Introduction to Programming

ITI Introduction to Computing II

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

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Object Oriented Software Design II

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

Object Oriented Software Design II

Chapter 10 Classes Continued. Fundamentals of Java

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Lecture 5: Inheritance

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

C++ (classes) Hwansoo Han

COMP6771 Advanced C++ Programming

Overview. ITI Introduction to Computing II. Interface 1. Problem 1. Problem 1: Array sorting. Problem 1: Array sorting. Problem 1: Array sorting

2015 Academic Challenge

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

What about Object-Oriented Languages?

CS 251 Intermediate Programming Inheritance

CSCI 102 Fall 2010 Exam #1

ITI Introduction to Computing II

Lecture Notes on Programming Languages

Chapter 9 - Object-Oriented Programming: Inheritance

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

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

ITI Introduction to Computing II

C++ Important Questions with Answers

OOP THROUGH C++(R16) int *x; float *f; char *c;

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Inheritance, Polymorphism and the Object Memory Model

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

Object Oriented Design

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

CSCI 101L - Data Structures. Practice problems for Final Exam. Instructor: Prof Tejada

POLYMORPHISM. Phone : (02668) , URL :

Chapter 15: Inheritance, Polymorphism, and Virtual Functions

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

Programming, numerics and optimization

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CS250 Final Review Questions

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

Recommended Group Brainstorm (NO computers during this time)

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

Inheritance. Chapter 15 & additional topics

index.pdf January 21,

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

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

Inheritance, and Polymorphism.

ITI Introduction to Computing II

C++ Programming Classes. Michael Griffiths Corporate Information and Computing Services The University of Sheffield

Inheritance. OOP: Inheritance 1

Inheritance Motivation

ITI Introduction to Computing II

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

What are the characteristics of Object Oriented programming language?

Object Oriented Design Final Exam (From 3:30 pm to 4:45 pm) Name:

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

More on Inheritance. Interfaces & Abstract Classes

Making New instances of Classes

CSc 520. Principles of Programming Languages 45: OO Languages Introduction

CSE 307: Principles of Programming Languages

Object Oriented Programming CS250

Object-oriented Programming. Object-oriented Programming

Object Oriented Programming. Java-Lecture 11 Polymorphism

Ministry of Higher Education Colleges of Applied Sciences Final Exam Academic Year 2009/2010. Student s Name

Object Oriented Programming: Inheritance Polymorphism

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Inheritance and Polymorphism

Inheritance and Polymorphism

Inheritance and Interfaces

Lecture 10: Introduction to Inheritance

Transcription:

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

Topics Virtual Functions Pure Virtual Functions Abstract Classes Concrete Classes Binding Time, Static Binding, Dynamic Binding Overriding vs Redefining Reading: pp. 929-952 Problems: pp. 925-928 15.9-15.15 (all very good) CS250 - Intro to CS II 2

Abstract Class Consider a base class called Shape that contains a draw function Circle, Square, and Line are classes that are derived from Shape, and each one has a unique draw function If some kind of array of Shape pointers is maintained, a simple draw command can be sent to each array object invoking the specific draw method for each object type We are revisiting this idea CS250 - Intro to CS II 3

Abstract Class An abstract class is a class where the programmer never intends to instantiate an object of the abstract class type These classes are typically base classes and are used in an inheritance hierarchy to build more generic derived classes Parts of the abstract class are not implemented in the base class; therefore, this logic MUST be implemented in the derived class CS250 - Intro to CS II 4

Pure Virtual Functions A class is made abstract by having one or more pure virtual functions associated with the class as follows: o virtual void functionname () = 0; Each derived class must provide its own draw function that overrides the draw function of the abstract class CS250 - Intro to CS II 5

Abstract Class Example class Shape { public: Shape (int x = 0, int y = 0); void setx (int); void sety (int); int getx () const; int gety () const; virtual void draw () = 0; virtual double area () = 0; private: int mx; int my; }; CS250 - Intro to CS II 6

Concrete Class A concrete class is any class that can be instantiated o An object of a concrete class can be created Of Shape, Circle, Square, and Line, which are abstract and which are concrete? Why? CS250 - Intro to CS II 7

Concrete Class Example class Circle : public Shape { public: Circle (int x = 0, int y = 0, double radius = 0); void setradius (double); double getradius () const; virtual void draw (); virtual double area (); private: double mradius; }; CS250 - Intro to CS II 8

Virtual Functions A virtual function o Allows the derived class the ability to override the function and o Must have an implementation A pure virtual function o Requires the derived class to override the function o Cannot have an implementation CS250 - Intro to CS II 9

Binding Time Binding time - the time at which something becomes known Static Binding - binding time that happens during compilation (e.g. a variable s type) Dynamic Binding - binding time that happens during runtime (e.g. the heap address of a dynamically allocated memory) CS250 - Intro to CS II 10

Redefining vs Overriding A derived class can redefine a base class member (static binding) A derived class that redefines a virtual function of a base class is said to override the base class function (dynamic binding) CS250 - Intro to CS II 11

InBetween Composition CS250 - Intro to CS II 12

The Problem Turn InBetween Composition into InBetween Polymorphism Three Players: Human RandomAI ConservativeAI CS250 - Intro to CS II 13

Human This player uses the normal keyboard interaction as in InBetweenComposition CS250 - Intro to CS II 14

RandomAIPlayer This player uses the two cards face up to determine a bet amount as follows: rand () % (highcardvalue lowcardvalue + 1) + 1 This player cashes out if a random number mod 11 is equal to 0 CS250 - Intro to CS II 15

ConservativeAIPlayer This player uses the two cards face up to determine a bet amount as follows: if the highcardvalue lowcardvalue is less than 6, the player bets 1 chip; otherwise, the player bets 2 chips This player cashes out if their bank loses or gains 10% of its original value CS250 - Intro to CS II 16