CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design.

Slides:



Advertisements
Similar presentations
Interra Induction Training
Advertisements

1 CSC241: Object Oriented Programming Lecture No 21.
Object Oriented Programming
C++ Classes & Data Abstraction
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Inheritance and Polymorphism CS351 – Programming Paradigms.
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Virtual Functions Fall 2008 Dr. David A. Gaitros
LECTURE 07 Programming using C# Inheritance
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
CMSC 202 Inheritance.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions.
C# G 1 CSC 298 Object Oriented Programming Part 2.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Effective C++, 2nd Ed. By Scott Myers. Constructors, Destructors, and Assignment Operators 11.Define a copy constructor and an assignment operator for.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
Overview of C++ Polymorphism
Variations on Inheritance Object-Oriented Programming Spring
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Virtual Function and Polymorphism
CMSC 202 Polymorphism.
Advanced Program Design with C++
Creational Pattern: Prototype
Class A { public : Int x; A()
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Polymorphism.
Inheritance and Run time Polymorphism
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Polymorphism 2 CMSC 202.
CMSC 202 Lesson 19 Polymorphism 2.
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Polymorphism Reference and pointer implicit type casting
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

CSC 143 O 1 CSC 143 Inheritance and Object Oriented Design

CSC 143 O 2 Public Inheritance means "isa"  If class D publicly inherits from class B, every object of type D is also an object of type B (not vice-versa) class Person{…}; class Student: public Person {…}; void dance(const Person& p); // anyone can dance void study(const Student& s); // only students study Person p; Student s; // p is a Person, s a Student dance(p); // fine, p is a Person dance(s); // fine, s is a Student, therefore isa Person study(s); // OK study(p); // error! p is not a Student

CSC 143 O 3 IsA Design (1)  Trouble: penguins can fly!  C++ is stricter than the English language. In C++, "Birds can fly" means "All birds can fly"  Solution? class Bird{ public: virtual void fly() // birds can fly … }; class Penguin: public Bird{ // penguins are birds … };

CSC 143 O 4 IsA Design (2)  Refine the hierarchy NonFlyingBird Penguin Bird (no fly function) FlyingBird (fly function declared here)

CSC 143 O 5 IsA Design (3)  Could also do: Penguins can fly, but it is an error for them to try to do so. How? class Bird {… //no fly function}; class FlyingBird : public Bird{ virtual void fly(); … }; class NonFlyingBird{ … //no fly function}; class Penguin: public NonFlyingBird{ … // no fly function};

CSC 143 O 6 Interface  Implementation  Interface = function declaration  Implementation = code for the function  Member function interfaces are always inherited. If a function applies to a class, it must also apply to its subclasses.  If you want the subclasses to inherit  the interface only, make the member function pure virtual (virtual … = 0)  the interface and a default implementation, make the member function virtual  Non virtual means inherit the interface as well as a mandatory implementation

CSC 143 O 7 Example: A Shape Class class Shape { public: virtual void draw() const = 0; // Abstract class virtual void error(const string& msg); int ObjectID() const; … }; class Rectangle: public Shape{…}; class Ellipse: public Shape{…};  Rectangle and Ellipse must provide a draw function  Every class must support an error function (but you don't have to write your own if you don't want to)  Every object has an ID. That ID is determined by the definition of Shape::objectID. Don't change it!

CSC 143 O 8 Virtual Destructor  Make sure that base classes have a virtual destructor  If D is derived from B, what happens in the following? B* pB = new D; … delete pB;  If you try to delete a derived class object through a base pointer and the base class doesn't have a virtual destructor, the result is undefined.