Programming Abstractions

Similar documents
Programming Abstractions

Programming Abstractions

CS 106B Lecture 27: Inheritance and Polymorphism in C++

CS 106X Lecture 26: Inheritance and Polymorphism in C++

Programming Abstractions

Admin. CS 112 Introduction to Programming. Recap: OOP Analysis. Software Design and Reuse. Recap: OOP Analysis. Inheritance

Topic 31 - inheritance

AP Computer Science. Gridworld, inheritance

Building Java Programs

Revisiting acm.graphics

CS 112 Introduction to Programming

Admin. q Admin and recap q Class inheritance

CS 112 Introduction to Programming

Inheritance. Writing classes. Writing more classes. Why are they very similar? Employee class. Secretary class. Readings: 9.1

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

Inheritance. Transitivity

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

Inheritance, and Polymorphism.

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Java Object Oriented Design. CSC207 Fall 2014

Inheritance and Polymorphism

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Big software. code reuse: The practice of writing program code once and using it in many contexts.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

OVERRIDING. 7/11/2015 Budditha Hettige 82

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

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

CSE 143 Lecture 12 Inheritance

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

CS11 Introduction to C++ Fall Lecture 7

Programming Exercise 14: Inheritance and Polymorphism

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Inheritance and Polymorphism

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance -- Introduction

1- Differentiate between extends and implements keywords in java? 2- What is wrong with this code:

Inheritance and Polymorphism

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Polymorphism. Arizona State University 1

Interactive Graphics

Chapter 1: Object-Oriented Programming Using C++

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

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

Overriding Variables: Shadowing

AP Computer Science. Interactions with superclass

Java Programming Lecture 7

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Super-Classes and sub-classes

Interactive Graphics. Eric Roberts Handout #23 CS 106A January 25, 2016 Interactive Graphics. Computer Graphics and the Utah Teapot.

CS-202 Introduction to Object Oriented Programming

CS111: PROGRAMMING LANGUAGE II

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

ECE 122. Engineering Problem Solving with Java

Previously, on Lesson Night... From Intermediate Programming, Part 1

Inheritance and Polymorphism

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

Lecture 15: Inheritance II

C++ Important Questions with Answers

ITI Introduction to Computing II

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

ECE 3574: Dynamic Polymorphism using Inheritance

ITI Introduction to Computing II

Programming II (CS300)

Programming Abstractions

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

CS111: PROGRAMMING LANGUAGE II

Inheritance. = way of forming new classes based on existing ones = way to share/reuse codebetween two or more classes

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

Reusing Classes. Hendrik Speleers

What is Inheritance?

Inheritance. CSE 142, Summer 2002 Computer Programming 1.

COMP 110/L Lecture 19. Kyle Dewey

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

Inheritance (Part 2) Notes Chapter 6

Programming via Java Subclasses

CS 106A, Lecture 11 Graphics

CS 251 Intermediate Programming Inheritance

Programming II (CS300)

Informatik II Tutorial 6. Subho Shankar Basu

04-24/26 Discussion Notes

Programming in C# Inheritance and Polymorphism

Accelerating Information Technology Innovation

Object-Oriented Programming

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Inheritance, polymorphism, interfaces

Inheritance. Overview. Chapter 15 & additional topics. Inheritance Introduction. Three different kinds of inheritance

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

INHERITANCE. Spring 2019

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

C++ Inheritance and Encapsulation

CLASS DESIGN. Objectives MODULE 4

Objects and Graphics

COMP 110/L Lecture 20. Kyle Dewey

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Data Structures (INE2011)

VIRTUAL FUNCTIONS Chapter 10

Transcription:

Programming Abstractions C S 1 0 6 B Cynthia Lee

Inheritance Topics Inheritance The basics Example: Stanford GObject class Polymorphism

Inheritance What? Why? How?

Inheritance: what? is-a relationship: A hierarchical connection where one category can be treated as a specialized version of another. every rectangle is a shape every lion is an animal type hierarchy: A set of data types connected by is-a relationships that can share common code. Re-use!

Inheritance: why? Remember the #1 rule of computer scientists: Computer scientists are super lazy in a good way! We want to reuse code and work as much as possible You ve already seen this going back to the very start of your CS education: Loops and Functions (instead of copy&paste to repeat code) Arrays (instead of copy&paste to make 100 named variables) Data structures (same idea as arrays but more expressive) Inheritance is another way of organizing smart reuse of code 5

Inheritance: how? inheritance: A way to form new classes based on existing classes, taking on their attributes/behavior. a way to group related classes a way to share code between two or more classes One class can extend another, absorbing its data/behavior.

Inheritance vocab superclass (base class): Parent class that is being extended. subclass (derived class): Child class that inherits from the superclass. Subclass gets a copy of every field and method from superclass. Subclass can add its own behavior, and/or change inherited behavior.

Inheritance Example Stanford Library G-Object family of classes Gob-ject

Behind the scenes We ve always told you not to worry about the graphics parts of your assignments. Just call this BoggleGUI function Now you can go ahead and take a look! 9

GObject hierarchy The Stanford C++ library contains a hierarchy of graphical objects based on a common base class named GObject. GArc GImage GLabel GLine GOval GPolygon GRect G3DRect hi GRoundRect

GObject members GObject defines the state and behavior common to all shapes: contains(x, y) get/setcolor() getheight(), getwidth() get/setlocation(), get/setx(), get/sety() move(dx, dy) setvisible(visible) double x; double y; double linewidth; std::string color; bool visible; The subclasses add state and behavior unique to them: Glabel GLine GPolygon GOval get/setfont get/setstartpoint addedge getsize get/setlabel get/setendpoint addvertex get/setfillcolor get/setfillcolor............

GObject members GObject defines the state and behavior common to all shapes: contains(x, y) get/setcolor() getheight(), getwidth() get/setlocation(), get/setx(), get/sety() move(dx, dy) setvisible(visible) double x; double y; double linewidth; std::string color; bool visible; The subclasses add state and behavior unique to them: Glabel GLine GPolygon GOval get/setfont get/setstartpoint addedge getsize get/setlabel get/setendpoint addvertex get/setfillcolor get/setfillcolor............

GObject hierarchy Q: Rectangle is-a Polygon, right? Why doesn t it inherit from Polygon?? The Stanford C++ library contains a hierarchy of graphical objects based on a common base class Although named true GObject. in geometry, they don t GArc share many fields and methods in this case. GImage GLabel GLine GOval GPolygon GRect G3DRect hi GRoundRect

Inheritance Example Your turn: let s write an Employee family of classes

Example: Employees Imagine a company with the following employee regulations: All employees work 40 hours / week Employees make $40,000 per year plus $500 for each year worked Except for lawyers who get twice the usual pay, and programmers who get the same $40k base but $2000 for each year worked Employees have 2 weeks of paid vacation days per year Except for programmers who get an extra week Each type of employee has some unique behavior: Lawyers know how to sue Programmers know how to write code

16 Employee class // Employee.h class Employee { Employee(string name, int years); virtual int hours(); virtual string name(); virtual double salary(); virtual int vacationdays(); virtual int years(); private: string m_name; int m_years; // Employee.cpp Employee::Employee(string name, int years) { m_name = name; m_years = years; } int Employee::hours() { return 40; } string Employee::name() { return m_name; } double Employee::salary() { return 40000.0 + (500 * m_years); } int Employee::vacationDays() { return 10; } int Employee::years() { return m_years; }

Exercise: Employees Exercise: Implement classes Lawyer and Programmer. A Lawyer remembers what law school he/she went to. Lawyers make twice as much salary as normal employees. Lawyers know how to sue people (unique behavior). Lawyers put, Esq. at the end of their name. Programmers make the same base salary as normal employees, but they earn a bonus of $2k/year instead of $500/year. Programmers know how to write code (unique behavior).

Inheritance syntax class Name : public SuperclassName { Example: class Lawyer : public Employee {... By extending Employee, each Lawyer object now: receives a hours, name, salary, vacationdays, and years method automatically can be treated as an Employee by client code

Call superclass c'tor SubclassName::SubclassName(params) : SuperclassName(params) { statements; } To call a superclass constructor from subclass constructor, use an initialization list, with a colon after the constructor declaration. Example: Lawyer::Lawyer(string name, string lawschool, int years) : Employee(name, years) { // calls Employee constructor first m_lawschool = lawschool; }

Your turn: inheritance string Lawyer::name() {??? } For adding, Esq. to the name, which of the following could work? A. return m_name + ", Esq."; B. return name() + ", Esq."; C. return Employee::name() + ", Esq."; D. None of the above E. More than one of the above // Employee.h class Employee { Employee(string name, int years); int hours(); string name(); double salary(); int vacationdays(); string vacationform(); int years(); private: string m_name; int m_years;

Call superclass member SuperclassName::memberName(params) To call a superclass overridden member from subclass member. Example: double Lawyer::salary() { // paid twice as much return Employee::salary() * 2; } Note: Subclass cannot access private members of the superclass. Note: You only need to use this syntax when the superclass's member has been overridden. If you just want to call one member from another, even if that member came from the superclass, you don't need to write Superclass::.

Polymorphism Start with how

Polymorphism polymorphism: Ability for the same code to be used with different types of objects and behave differently with each. Templates provide a kind of compile-time polymorphism. Grid<int> or Grid<string> will output different things for mygrid[0][0], but we can predict at compile time which it will do Inheritance provides run-time polymorphism. someemployee.salary() will behave differently at runtime depending on what type of employee may not be able to predict at compile time which it is

Polymorphism A pointer of type T can point to any subclass of T. Employee *neha = new Programmer("Neha", 2); Employee *diane = new Lawyer("Diane", "Stanford", 5); Programmer *cynthia = new Programmer("Cynthia", 10); Why would you do this? Handy if you want to have a function that works on any Employee, but takes advantage of custom behavior by specific employee type: void domonthlypaycheck(employee *employee) { } cout << "You are now $" << employee->salary()/12 << " wealthier!" << endl;

Polymorphism A pointer of type T can point to any subclass of T. Employee *neha = new Programmer("Neha", 2); Employee *diane = new Lawyer("Diane", "Stanford", 5); Programmer *cynthia = new Programmer("Cynthia", 10); When a member function is called on diane, it behaves as a Lawyer. diane->salary(); (This is because all the employee functions are declared virtual.) You can not call any Lawyer-only members on diane (e.g. sue). diane->sue(); // will NOT compile! You can call any Programmer-only members on cynthia (e.g. code). cynthia->code("java"); // ok!

Polymorphism examples You can use the object's extra functionality by casting. Employee *diane = new Lawyer("Diane", "Stanford", 5); diane->vacationdays(); // ok diane->sue("cynthia"); // compiler error ((Lawyer*) diane)->sue("cynthia"); // ok Pro Tip: you should not cast a pointer into something that it is not! It will compile, but the code will crash (or behave unpredictably) when you try to run it. Employee *carlos = new Programmer("Carlos", 3); carlos->code(); // compiler error ((Programmer*) carlos)->code("c++"); // ok ((Lawyer*) carlos)->sue("cynthia"); // No!!! Compiles but crash!!

Rules for virtual : runtime calls DerivedType * obj = new DerivedType(); If we call a method like this: obj->method(), only one thing could happen: 1. DerivedType s implementation of method is called BaseType * obj = new DerivedType(); If we call a method like this: obj->method(), two different things could happen: 1. If method is not virtual, then BaseType s implementation of method is called 2. If method is virtual, then DerivedType s implementation of method is called

Rules for virtual : pure virtual If a method of a class looks like this: virtual returntype method() = 0; then this method is a called pure virtual function and the class is called an abstract class Abstract classes are like Java interfaces You cannot do = new Foo(); if Foo is abstract (just like Java interfaces) ALSO, you cannot do = new DerivedFoo(); if DerivedFoo extends Foo and DerivedFoo does not implement all the pure virtual methods of Foo

class Mammal { virtual void makesound() = 0; string tostring() { return Mammal ; } class Cat : public Mammal { virtual void makesound() { cout << rawr << endl; } string tostring() { return Cat ; } class Siamese : public Cat { virtual void makesound() { cout << meow << endl; } string tostring() { return Siamese ; } virtual void scratchcouch() { cout << scraaaatch << endl; } What is printed? Siamese * s = new Mammal; cout << s->tostring(); (A) Mammal (B) Cat (C) Siamese (D) Gives an error (identify compiler or crash) (E) Other/none/more

class Mammal { virtual void makesound() = 0; string tostring() { return Mammal ; } class Cat : public Mammal { virtual void makesound() { cout << rawr << endl; } string tostring() { return Cat ; } class Siamese : public Cat { virtual void makesound() { cout << meow << endl; } string tostring() { return Siamese ; } virtual void scratchcouch() { cout << scraaaatch << endl; } What is printed? Siamese * s = new Siamese; cout << s->tostring(); (A) Mammal (B) Cat (C) Siamese (D) Gives an error (identify compiler or crash) (E) Other/none/more

class Mammal { virtual void makesound() = 0; string tostring() { return Mammal ; } class Cat : public Mammal { virtual void makesound() { cout << rawr << endl; } string tostring() { return Cat ; } class Siamese : public Cat { virtual void makesound() { cout << meow << endl; } string tostring() { return Siamese ; } virtual void scratchcouch() { cout << scraaaatch << endl; } What is printed? Mammal * m = new Mammal; cout << m->tostring(); (A) Mammal (B) Cat (C) Siamese (D) Gives an error (identify compiler or crash) (E) Other/none/more

class Mammal { virtual void makesound() = 0; string tostring() { return Mammal ; } class Cat : public Mammal { virtual void makesound() { cout << rawr << endl; } string tostring() { return Cat ; } class Siamese : public Cat { virtual void makesound() { cout << meow << endl; } string tostring() { return Siamese ; } virtual void scratchcouch() { cout << scraaaatch << endl; } What is printed? Mammal * m = new Siamese; cout << m->tostring(); (A) Mammal (B) Cat (C) Siamese (D) Gives an error (identify compiler or crash) (E) Other/none/more

class Mammal { virtual void makesound() = 0; string tostring() { return Mammal ; } class Cat : public Mammal { virtual void makesound() { cout << rawr << endl; } string tostring() { return Cat ; } class Siamese : public Cat { virtual void makesound() { cout << meow << endl; } string tostring() { return Siamese ; } virtual void scratchcouch() { cout << scraaaatch << endl; } What is printed? Mammal * m = new Siamese; m->scratchcouch(); (A) Mammal (B) Cat (C) Siamese (D) Gives an error (identify compiler or crash) (E) Other/none/more

class Mammal { virtual void makesound() = 0; string tostring() { return Mammal ; } class Cat : public Mammal { virtual void makesound() { cout << rawr << endl; } string tostring() { return Cat ; } class Siamese : public Cat { virtual void makesound() { cout << meow << endl; } string tostring() { return Siamese ; } virtual void scratchcouch() { cout << scraaaatch << endl; } What is printed? Cat * c = new Siamese; c->makesound(); (A) rawr (B) meow (C) Siamese (D) Gives an error (identify compiler or crash) (E) Other/none/more