L4: Inheritance. Inheritance. Chapter 8 and 10 of Budd.

Similar documents
Inheritance, and Polymorphism.

Inheritance and Substitution (Budd chapter 8, 10)

Java Object Oriented Design. CSC207 Fall 2014

CS-202 Introduction to Object Oriented Programming

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

Object Oriented Software Design II

Object Oriented Software Design II

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

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

Inheritance. Transitivity

SDV4001 Object Oriented Programming with C++ and UI Midterm 2013

OBJECT ORIENTED PROGRAMMING

CS 520 Theory and Practice of Software Engineering Fall 2017

Midterm Exam 5 April 20, 2015

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Lecture 5: Inheritance

Inheritance

Data Structures (INE2011)

CS 520 Theory and Practice of Software Engineering Fall 2018

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Object Oriented Programming. Java-Lecture 11 Polymorphism

ITI Introduction to Computing II

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

G Programming Languages - Fall 2012


ITI Introduction to Computing II

CS 320 Introduction to Software Engineering Spring March 06, 2017

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

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

Object-oriented Programming. Object-oriented Programming

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

ECE 3574: Dynamic Polymorphism using Inheritance

Object-Oriented Concepts and Design Principles

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

Friend Functions, Inheritance

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

CSC207 Week 3. Larry Zhang

S.O.L.I.D: Software Engineering Principles

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

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Inheritance and Interfaces

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

OBJECT ORIENTED PROGRAMMING

Subtypes and Subclasses

Polymorphism Part 1 1

What does it mean by information hiding? What are the advantages of it? {5 Marks}

CS304 Object Oriented Programming Final Term

Polymorphism: Interfaces and Iteration. Fundamentals of Computer Science

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Arrays Classes & Methods, Inheritance

Chapter 10 Classes Continued. Fundamentals of Java

COMP 2355 Introduction to Systems Programming

CS1150 Principles of Computer Science Objects and Classes

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

Goals of the Lecture OO Programming Principles

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

Chapter 6. Object- Oriented Programming Part II

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

Dynamic Binding C++ Douglas C. Schmidt

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

Inheritance and Encapsulation. Amit Gupta

Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע"י ד"ר גרא וייס

Chapter 1: Object-Oriented Programming Using C++

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

CSCI-1200 Computer Science II Fall 2006 Lecture 23 C++ Inheritance and Polymorphism

CS250 Final Review Questions

Inheritance. OOP: Inheritance 1

Polymorphism CSCI 201 Principles of Software Development

Concepts of Programming Languages

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Polymorphism. Agenda

Inheritance and Polymorphism

Self-review Questions

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

INHERITANCE IN OBJECT ORIENTED PROGRAMMING EASIEST WAY TO TEACH AND LEARN INHERITANCE IN C++ TWINKLE PATEL

!" #$ +-,.-% $/00&&& $ 0$0

Computer Programming Inheritance 10 th Lecture

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

Introduction to Inheritance

Programming in C++: Assignment Week 6

INHERITANCE - Part 1. CSC 330 OO Software Design 1

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Programming in C++: Programming Test-2

CS250 Final Review Questions

index.pdf January 21,

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Sahaj Computer Solutions OOPS WITH C++

ITI Introduction to Computing II

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

Making New instances of Classes

Programming, numerics and optimization

Chapter 14 Abstract Classes and Interfaces

Intro to: Design Principles

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

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

Inheritance (Deitel chapter 9)

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

Transcription:

L4: Inheritance Inheritance Definition Example Other topics: Is A Test, Reasons for Inheritance, C++ vs. Java, Subclasses and Subtypes 7 Forms of Inheritance Discussions Chapter 8 and 10 of Budd. SFDV4001 / L4 - Inheritance / 1

Definition The property that instances of a child class (or subclass) can access both data and behaviour (methods) associated with a parent class. Review Chapter 8 and 10 of Budd. SFDV4001 / L4 - Inheritance / 2

Example: Syntax class A { public: int getaval(); int getbval(); protected: int b; private: int a; class B: public A { public: int dostuff(){ getaval(); getbval(); b = 2; a = 3; ; SFDV4001 / L4 - Inheritance / 3

class Base { Example:Derived Classes public: virtual void print() { cout << Base class << endl; class Derived:public Base { public: virtual void print() { cout << Derived class << endl; SFDV4001 / L4 - Inheritance / 4

Example: Derived Classes void func(base &b) { b.print(); int main() { Base b; Derived d; func(b); func(d); SFDV4001 / L4 - Inheritance / 5

The Is-A Test To test whether inheritance is appropriate, it should make sense to say B is an A. SFDV4001 / L4 - Inheritance / 6

Reasons for Inheritance As a means of code reuse. As a means of concept reuse (like an interface). SFDV4001 / L4 - Inheritance / 7

C++ versus Java All classes in Java inherit from a common base class (Object). Advantages: Disadvantages: C++ does not. SFDV4001 / L4 - Inheritance / 8

Subclasses & Subtypes Subtypes are subclasses where the subclass can be substituted for the parent class. Most statically typed languages assume all subclasses are also subtypes, but this is not necessarily true. SFDV4001 / L4 - Inheritance / 9

Forms of Inheritance Specialisation Specification Construction Extension Limitation Variance Combination (discussed in Lecture 5) SFDV4001 / L4 - Inheritance / 10

Inheritance for Specialisation Child class is a specialised variety of the parent class. This is the ideal form of inheritance. SFDV4001 / L4 - Inheritance / 11

Example Class Rectangle { public: virtual void draw(){ virtual void setsize(int _width, int _height){ private: int x,y, width, height; Class ColouredRectangle : public Rectangle { public: virtual void draw(){ ; private: Rectangle::draw(); fill(x,y,width,height,colour); RGB colour; SFDV4001 / L4 - Inheritance / 12

Inheritance for Specification Can use inheritance to guarantee that classes maintain a common interface. In Java, we do this by using interfaces. In C++, we use abstract classes. SFDV4001 / L4 - Inheritance / 13

Example class Abstract { public: virtual void dosomething() = 0; ; class Concrete : public Abstract { public: // must define dosomething otherwise // is also an abstract class virtual void dosomething(); ; SFDV4001 / L4 - Inheritance / 14

Inheritance for Construction A class inherits from another to simplify implementation even though there may be no logical connection between the two. SFDV4001 / L4 - Inheritance / 15

Example public class Stack extends Vector { public Stack() { public Object push(object item) { addelement(item); return item; public synchronized Object pop() { Object obj; int len = size(); obj = peek(); removeelementat(len - 1); return obj;... SFDV4001 / L4 - Inheritance / 16

Inheritance for Extension A child class only adds new behaviour to its parent it does not override any behaviour. SFDV4001 / L4 - Inheritance / 17

Inheritance for Limitation Occurs when the behaviour of the subclass is more restrictive than the behaviour of the parent. In this case, we would have to override methods in the parent to provide more limiting behaviour. SFDV4001 / L4 - Inheritance / 18

Example Class Rectangle { private: int width, height; public: virtual void draw(){ virtual void setsize(int _width, int _height){ Class Square : public Rectangle { public: virtual void setsize(int side){ setsize(side, side);; virtual void setsize(int _width, int _height) { if (_width!= _height) Error! Rectangle::setSize(_width, _width); SFDV4001 / L4 - Inheritance / 19

Inheritance for Variance Two or more classes have similar implementations but no clear hierarchical relationship. Eg class Mouse and class Tablet SFDV4001 / L4 - Inheritance / 20

Constructors In C++ default parent constructors are always called if they exist, unless otherwise specified. If no default constructor exists, child class must explicitly call the appropriate parent constructor. Constructors are never virtual. SFDV4001 / L4 - Inheritance / 21

Example class A { public: A(){cout << A default\n ;; A(int num){cout << A(num)\n ;; class B : public A { public: B(){cout << B default\n ;; B(int num): A(num){ cout << B(num)\n ;; B(char ch) {cout << B(ch)\n ; SFDV4001 / L4 - Inheritance / 22

Destructors In C++, if there is any chance that a class will be derived from, then the destructor should be made virtual. It is generally good practice to make all destructors virtual. SFDV4001 / L4 - Inheritance / 23

Example class Parent { ; public: virtual ~Parent(){ cout << parent\n ;; class Child: public Parent { ; public: ~Child(){cout << child\n ;; Parent *p = new Child(); delete p; SFDV4001 / L4 - Inheritance / 24

Good and Bad Good: specialisation specification extension Ugly: construction limitation Complicated: combination SFDV4001 / L4 - Inheritance / 25

Benefits of Inheritance Reusability Reliability Code sharing Consistency of interface Rapid prototyping Information hiding SFDV4001 / L4 - Inheritance / 26

Why is it so Cool? class Shape {virtual void draw() = 0;; class Rectangle: public Shape{draw(){ ;; class Circle : public Shape{draw(){ ;; vector<shape *> shapes; shapes.push_back(new Rectangle()); shapes.push_back(new Circle()); shapes.push_back(new Triangle()); for (i=0; i<num_shapes; i++) shapes[i]->draw(); SFDV4001 / L4 - Inheritance / 27

Costs of Inheritance Execution speed Program size Program complexity SFDV4001 / L4 - Inheritance / 28