CSC241 Object-Oriented Programming (OOP) Lecture No. 16.

Slides:



Advertisements
Similar presentations
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
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.
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
C# DownCast vs UpCast.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
Polymorfisme “Dynamic binding provides design flexibility and may enhance software maintainability. “
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
Object-Oriented Programming (OOP) Lecture No. 6
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Programming Languages and Paradigms Object-Oriented Programming.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
CE Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CSC241 Object-Oriented Programming (OOP) Lecture No. 18
CMSC 2021 Polymorphism. CMSC 2022 Static vs. Dynamic Binding Binding The determination of which method in the class hierarchy is to be used for a particular.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
Chapter 10 Inheritance and Polymorphism
1 Original Source : and Problem and Problem Solving.ppt.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
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. 14.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Exception Handling How to handle the runtime errors.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Computer Science Department Inheritance & Polymorphism.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
A First Book of C++ Chapter 12 Extending Your Classes.
C++ Lesson 1.
Basic concepts of C++ Presented by Prof. Satyajit De
CMSC 202 Polymorphism.
Polymorphism.
Classes in C++ C++ originally called "C with classes":
Object-Oriented Programming (OOP) Lecture No. 28
Classes in C++ C++ originally called "C with classes":
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Programming II Polymorphism A.AlOsaimi.
Polymorphism CMSC 202, Version 4/02.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Fundamental Programming
Java Programming: From the Ground Up
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Functions Divide and Conquer
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:

CSC241 Object-Oriented Programming (OOP) Lecture No. 16

Problem Statement  Develop a function that can draw different types of geometric shapes from an array

Shape Hierarchy Shape LineCircleTriangle draw calcArea draw calcArea draw calcArea draw calcArea

Shape Hierarchy class Shape { … protected: char _type; public: Shape() { } void draw(){ cout << "Shape\n"; } int calcArea() { return 0; } char getType() { return _type; } };

… Shape Hierarchy class Line : public Shape { … public: Line(Point p1, Point p2) { … } void draw(){ cout << "Line\n"; } };

… Shape Hierarchy class Circle : public Shape { … public: Circle(Point center, double radius) { … } void draw(){ cout << "Circle\n"; } int calcArea() { … } };

… Shape Hierarchy class Triangle : public Shape { … public: Triangle(Line l1, Line l2, double angle) { … } void draw(){ cout << "Triangle\n"; } int calcArea() { … } };

Drawing a Scene int main() { Shape* _shape[10]; Point p1(0, 0), p2(10, 10); _shape[0] = new Line(p1, p2); _shape[1] = new Circle(p1, 15); … void drawShapes(_shape, 10); return 0; }

Function drawShapes() void drawShapes(Shape* _shape[], int size) { for (int i = 0; i < size; i++) { _shape[i]->draw(); }

Sample Output Shape …

Function drawShapes() void drawShapes(Shape* _shape[], int size) { for (int i = 0; i < size; i++) { // Determine object type with // switch & accordingly call // draw() method }

Required Switch Logic switch (_shape[i]->getType()) { case ‘L’: static_cast (_shape[i])->draw(); break; case ‘C’: static_cast (_shape[i])->draw(); break; … }

Equivalent If Logic if (_shape[i]->getType() == ‘L’) static_cast (_shape[i])->draw(); else if (_shape[i]->getType() == ‘C’) static_cast (_shape[i])->draw(); …

Sample Output Line Circle Triangle Circle …

Problems with Switch Statement

…Delocalized Code  Consider a function that prints area of each shape from an input array

Function printArea void printArea(Shape* _shape[], int size) { for (int i = 0; i < size; i++) { // Print shape name. // Determine object type with // switch & accordingly call // calcArea() method. }

Required Switch Logic switch (_shape[i]->getType()) { case ‘L’: static_cast (_shape[i])->calcArea(); break; case ‘C’: static_cast (_shape[i])->calcArea(); break; … }

…Delocalized Code  The above switch logic is same as was in function drawArray()  Further we may need to draw shapes or calculate area at more than one places in code

Other Problems  Programmer may forget a check  May forget to test all the possible cases  Hard to maintain

Solution?  To avoid switch, we need a mechanism that can select the message target automatically!

Polymorphism Revisited  In OO model, polymorphism means that different objects can behave in different ways for the same message (stimulus)  Consequently, sender of a message does not need to know the exact class of receiver

Virtual Functions  Target of a virtual function call is determined at run-time  In C++, we declare a function virtual by preceding the function header with keyword “virtual” class Shape { … virtual void draw(); }

Shape Hierarchy Shape LineCircleTriangle draw calcArea draw calcArea draw calcArea draw calcArea

…Shape Hierarchy Revisited class Shape { … virtual void draw(); virtual int calcArea(); }; class Line : public Shape { … virtual void draw(); }; No type field

… Shape Hierarchy Revisited class Circle : public Shape { … virtual void draw(); virtual int calcArea(); }; class Triangle : public Shape { … virtual void draw(); virtual int calcArea(); };

Function drawShapes() void drawShapes(Shape* _shape[], int size) { for (int i = 0; i < size; i++) { _shape[i]->draw(); }

Sample Output Line Circle Triangle Circle …

Function printArea void printArea(Shape* _shape[], int size) { for (int i = 0; i < size; i++) { // Print shape name cout calcArea(); cout << endl; }

Static vs Dynamic Binding  Static binding means that target function for a call is selected at compile time  Dynamic binding means that target function for a call is selected at run time

Static vs Dynamic Binding Line _line; line.draw();// Always Line::draw called Shape* _shape = new Line(); _shape->draw(); // Shape::draw called // if draw() is not virtual Shape* _shape = new Line(); _shape->draw(); // Line::draw called // if draw() is virtual