CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor:

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Chapter 10: Inheritance and Polymorphism
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Templates and Polymorphism Generic functions and classes
CS 151: Object-Oriented Design October 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 151: Object-Oriented Design September 24 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Object-Oriented Programming in C++
Inheritance in the Java programming language J. W. Rider.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Interfaces and Polymorphism CS 162 (Summer 2009).
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Motivation for Generic Programming in C++
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Web Design & Development Lecture 9
CMPE Data Structures and Algorithms in C++ September 14 Class Meeting
CMPE 135: Object-Oriented Analysis and Design October 3 Class Meeting
CS 3370 – C++ Object-oriented Programming
Advanced Program Design with C++
7. Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 11: Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
CMPE 180A Data Structures and Algorithms in C++ February 15 Class Meeting Department of Computer Engineering San Jose State University Spring 2018 Instructor:
CMPE 152: Compiler Design February 6 Class Meeting
Andy Wang Object Oriented Programming in C++ COP 3330
CMPE 180A Data Structures and Algorithms in C++ March 8 Class Meeting
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
CMPE 135: Object-Oriented Analysis and Design August 30 Class Meeting
CMPE 152: Compiler Design August 23 Class Meeting
CMPE 135: Object-Oriented Analysis and Design November 27 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
Inheritance: Polymorphism and Virtual Functions
From C to C++: Summary of weeks 1 - 4
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
CMPE 152: Compiler Design January 29 Class Meeting
CS 144 Advanced C++ Programming February 12 Class Meeting
CMPE 135: Object-Oriented Analysis and Design February 5 Class Meeting
CMPE 135: Object-Oriented Analysis and Design March 19 Class Meeting
CS 144 Advanced C++ Programming January 31 Class Meeting
CS 144 Advanced C++ Programming February 12 Class Meeting
CS 144 Advanced C++ Programming February 21 Class Meeting
CMPE 135: Object-Oriented Analysis and Design February 28 Class Meeting Department of Computer Engineering San Jose State University Spring 2019 Instructor:
CMPE 135 Object-Oriented Analysis and Design March 7 Class Meeting
CS 144 Advanced C++ Programming March 28 Class Meeting
CS 144 Advanced C++ Programming April 9 Class Meeting
CS 144 Advanced C++ Programming March 21 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
CMPE 152: Compiler Design March 19 Class Meeting
CS 151: Object-Oriented Design October 8 Class Meeting
CMPE 152: Compiler Design August 27 Class Meeting
Presentation transcript:

CMPE 135: Object-Oriented Analysis and Design September 26 Class Meeting Department of Computer Engineering San Jose State University Fall 2017 Instructor: Ron Mak www.cs.sjsu.edu/~mak

Simple Machine Learning for RPS Human players of the Rock Paper Scissors game generally do not make random choices. Humans try do develop strategies to beat the opponent. Humans exhibit patterns that a computer can exploit.

Simple Machine Learning for RPS, cont’d Continually record the last N choices between the human and the computer player, where N = 2, 3, 4, 5 and the last choice is the human’s. For example, suppose during the game, the choices are (the human’s choices are underlined): The table shows the sequence recorded for each value of N right after the human player chose paper. SRSPPSRSP N Sequence 2 SP 3 RSP 4 SRSP 5 PSRSP

Simple Machine Learning for RPS, cont’d The computer should record all the length-2 sequences, all the length-3 sequences, etc. For each sequence, the computer should also record how many times that sequence has occurred (each sequence’s frequency). For example, for N = 3, the stored sequences and their frequencies may be (in no particular order, human choices are underlined): PRS:1, SRP:2, RPR:1, PSR:1, SPS:1, PSS:1

Simple Machine Learning for RPS, cont’d PRS:1, SRP:2, RPR:1, PSR:1, SPS:1, PSS:1 Suppose the last two choices are SR. The computer can predict that the human most likely will next choose paper, since SRP appears twice in this small sample. Therefore, the computer should choose scissors to beat the human’s predicted throw of paper.

Simple Machine Learning for RPS, cont’d The computer can make similar predictions with its other recorded length-N sequences. As the computer plays more matches, it records more choice sequences, giving it more data to make predictions of the human’s next choice. And so it becomes increasingly difficult for a human player to beat the computer.

Interfaces An interface is a form of contract between programmers. An interface specifies the public member functions of classes that implement the interface. For each function: its name and return type, and the number, order, and types of its formal parameters. An interface specifies the API of the classes that implement the interface.

Interfaces, cont’d We can define an interface in C++ as an abstract class containing only pure virtual functions. An interface has no implementation. An interface cannot be instantiated. Each pure virtual function must be implemented by a class that implements the interface. Each interface must have a virtual destructor.

Interface Example class shape   // An interface class { public:     virtual ~shape() {}     virtual void move_x(int x) = 0;     virtual void move_y(int y) = 0;     virtual void draw() = 0; ... }; C++ interface classes are maintained in .h header files. In UML class diagrams, interface names are in italics or oblique font.

Interfaces and the RPS Game You’re developing the RPS game. You’ve designed a choice calculator that determines the computer’s next choice. The choice calculator has three algorithms: Random choice: Randomly choose rock, paper, or scissors each time. Smart choice: Use the simple machine learning algorithm to make a choice. Genius choice: Use a sophisticated machine learning algorithm.

Interfaces and the RPS Game, cont’d What if you don’t know ahead of time which choice algorithm you’ll want to use? What if you come up with new choice algorithms later? ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice

Interfaces and the RPS Game, cont’d Make ChoiceCalculator an interface. Subclasses implement the interface and generate the computer’s choices. ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice

Interfaces and the RPS Game, cont’d Now you can write: What is wrong with this code? Pointer variables rc, sc, and gc each can point only to an object of its type. ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice RandomChoice *rc = new RandomChoice(); SmartChoice *sc = new SmartChoice (); GeniusChoice *gc = new GeniusChoice();

Code to the Interface Instead, use a variable of the interface type Example: Variable calc and any code that uses calc does not need to know which choice subclass. As long as the code uses the ChoiceCalculator API. ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice ChoiceCalculator *calc = new SmartChoice();

Code to the Interface, cont’d ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice ChoiceCalculator *calc = new SmartChoice(); Code to the interface, not to a specific subclass. This design principle is not strict about what is an interface. The interface can also be any supertype.

Code to the Interface, cont’d ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice ChoiceCalculator *calc = new SmartChoice(); The call to new is awkward. What if we invent a new choice algorithm? We would have to rewrite any code that creates choice objects.

A Factory Class Create a choice factory class ChoiceFactory that has a static member function make_choice_calculator that takes a string parameter. The parameter determines which choice subclass to instantiate and return. The factory class encapsulates any future changes in the choice algorithms.

A Factory Class, cont’d ChoiceCalculator *ChoiceFactory::make_choice_calculator(string which) { if (which == "random") return new RandomChoice(); if (which == "smart") return new SmartChoice(); if (which == "genius") return new GeniusChoice(); } ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice ChoiceCalculator *calc = ChoiceFactory::make_choice_calculator(calc);

Interface Variables To be a strict interface, the C++ abstract class cannot contain any member variables. An interface cannot be instantiated. However, the class can contain constants. The constants are shared by any class that implements the interface. class ErrorCodes { public: static const int MISSING_SEMICOLON = 1000; static const int MISSING_PARENTHESIS = 1001; ... }

Objects and Interfaces If class C implements interface N, then objects of class C can be assigned to variables of the interface type N. ComputerPlayer Choice Calculator RandomChoice SmartChoice GeniusChoice ChoiceCalculator *calc = ChoiceFactory::make_choice_calculator(calc); Interface type Implements the ChoiceCalculator interface

Objects and Interfaces, cont’d The type of an object is never an interface type. The type of a variable can be an interface type. The value of such a variable is a reference to an object whose class implements the interface type.

C++ Runtime Type Identification Not as straightforward as for Java. No simple equivalent of Java’s instanceof operator. Is an object an instance of a class? Is an object an instance of a superclass?

typeid() myint has type: i mystr has type: NSt3__..._9allocatorIcEEEE #include <iostream> #include <string> #include <typeinfo> using namespace std; struct Base {}; // non-polymorphic struct Derived : Base {}; struct Base2 { virtual void foo() {} }; // polymorphic struct Derived2 : Base2 {}; int main() {     int myint = 50;     string mystr = "string";     double *mydoubleptr = nullptr;     cout << "myint has type: " << typeid(myint).name() << endl          << "mystr has type: " << typeid(mystr).name() << endl          << "mydoubleptr has type: "          << typeid(mydoubleptr).name() << endl; myint has type: i mystr has type: NSt3__..._9allocatorIcEEEE mydoubleptr has type: Pd

typeid(), cont’d     // cout << myint is a glvalue expression of polymorphic type;     // it is evaluated     const type_info& r1 = typeid(cout << myint);     cout << endl << "cout<<myint has type : " << r1.name() << endl;     // printf() is not a glvalue expression of polymorphic type;     // NOT evaluated     const type_info& r2 = typeid(printf("%d\n", myint));     cout << "printf(\"%d\\n\",myint) has type : " << r2.name() << endl; 50 cout<<myint has type : NSt3__113basic_ostreamIcNS_11char_traitsIcEEEE printf("%d\n",myint) has type : i

typeid(), cont’d // Non-polymorphic lvalue is a static type     Derived d1;     Base& b1 = d1;     cout << "reference to non-polymorphic base: " << typeid(b1).name() << endl;     Derived2 d2;     Base2& b2 = d2;     cout << "reference to polymorphic base: " << typeid(b2).name() << endl; reference to non-polymorphic base: 4Base reference to polymorphic base: 8Derived2

typeid(), cont’d try { // dereferencing a null pointer:     {         // dereferencing a null pointer:         // okay for a non-polymorphic expression         cout << "mydoubleptr points to "              << typeid(*mydoubleptr).name() << endl;         // not okay for a polymorphic lvalue         Derived2* bad_ptr = nullptr;         cout << "bad_ptr points to... ";         cout << typeid(*bad_ptr).name() << endl;     }     catch (const bad_typeid& e)          cout << " caught " << e.what() << endl; } mydoubleptr points to d bad_ptr points to...  caught std::bad_typeid