Chapter 10 Inheritance and Polymorphism

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Inheritance COMP53 Sept Inheritance Inheritance allows us to define new classes by extending existing classes. A child class inherits all members.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Inheritence ( הורשה Inheritence ( הורשה ( השקפים מבוססים על שקפים של אליהו ברוטמן ומרינה.
Inheritance and Polymorphism CS351 – Programming Paradigms.
1 Inheritance and Polymorphism Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Polymorphism &Virtual Functions
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Outline §Review of the last class l class variables and methods l method overloading and overriding §Inheritance and polymorphism l polymorphism l abstract.
Structured Programming Good for programming in the small Often doesn't scale up Limitations –Changes at top may affect lower-level algorithms –Code reuse.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
CSC241 Object-Oriented Programming (OOP) Lecture No. 16.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Coming up: Inheritance
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Peyman Dodangeh Sharif University of Technology Fall 2014.
1 Another way to define a class Inheritance..!!. 2 Why Inheritance ? Inheritance is a mechanism for building class types from existing class types defining.
1 Chapter 4: Another way to define a class Inheritance..!!
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
A First Book of C++ Chapter 12 Extending Your Classes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Web Design & Development Lecture 9
Polymorphism.
Inheritance and Run time Polymorphism
CSC 143 Inheritance.
Virtual Functions Department of CSE, BUET Chapter 10.
Polymorphism CT1513.
Inheritance and Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 14 Abstract Classes and Interfaces
C++ Object Oriented 1.
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.
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Chapter 10 Inheritance and Polymorphism C/C++ Language Programming Wanxiang Che

Array of Objects // Initialize sum double sum = 0; // Add areas to sum Circle circles[2] = {Circle(3), Circle(4)}; // Initialize sum double sum = 0; // Add areas to sum for (int i = 0; i < 2; i++) sum+=circles[i].get_area();

How to sum different Shape? Circle circle(3); Rectangle rect(4, 5); double sum = 0; sum += circle.get_area(); sum += rect.get_area();

How to sum different Shape? Shape shapes[2] = {Circle(3), Rectangle(4, 5)}; // Initialize sum double sum = 0; // Add areas to sum for (int i = 0; i < 2; i++) sum += shapes[i].get_area();

Inheritance Concept Derive a new class (subclass) from an existing class (base class or superclass). Inheritance creates a hierarchy of related classes (types) which share code and interface.

Shape class hierarchy Shape inherits (isa) Rectangle • • • • Circle Square Triangle Circle • • • • inherits (isa)

Inheritance UML

Define a Class Hierarchy Syntax: class DerivedClassName : access-level BaseClassName access-level specifies the type of derivation private by default, or public E.g. class Circle: public Shape Any class can serve as a base class Thus a derived class can also be a base class

Redefining Functions A derived class can override methods defined in its parent class. With overriding, the method in the subclass has the identical signature to the method in the base class. a subclass implements its own version of a base class method. class A { protected: int x, y; public: void print () {cout<<“From A”<<endl;} } class B : public A { public: void print () {cout<<“From B”<<endl;} }

redefining vs. overloading Overloading a function A way to provide more than one function with the same name but with different signatures to distinguish them. Redefining a function The function must be defined in the derived class using the same signature and same return type as in its base class

Static Binding Classes X print() inherits (isa) Y print() Z print() X x; Y y; Z z; X *px; px = & ??; // can be x,y,or z px->print(); // ?? print() inherits (isa) Y print() Z print()

Two Types of Binding Static Binding (the default in C++) px->print() uses X’s print this is known at compile time Dynamic Binding px->print() uses the print() in the object pointed at this is only known at run time with virtual functions

Why “only known at run time”? Assume dynamic binding is being used: X x; Y y; Z z; X *px; cin >> val; if (val == 1) px = &x; else px = &y; px->print(); // which print() is used?

Define Virtual Functions To enable dynamic binding for a function, need to do two things: The function must be declared virtual in the base class. The variable that references the object for the function must contain the address of the object.

Virtual Function class Shape { public: Shape(); Shape(const Shape& orig); virtual ~Shape(); void print(); virtual double get_area(); };

Solution int main() { Shape *shapes[2] = {new Circle(3), new Rectangle(4, 5)}; double sum = 0; for(int i = 0; i < 2; i++){ sum += shapes[i]->get_area(); } cout << sum << endl; return 0;

Note If a function is defined virtual in a base class, it is automatically virtual in all its derived classes. It is not necessary to add the keyword virtual in the function declaration in the derived class.