Object-Oriented Programming (OOP) Lecture No. 28

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

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
. 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.
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.
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.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 18
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Object Oriented Programming Lecture 11: Polymorphism.
CSC241 Object-Oriented Programming (OOP) Lecture No. 16.
Chapter 10 Inheritance and Polymorphism
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Instructor - C. BoyleFall Semester
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
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]
C++ Lesson 1.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Basic concepts of C++ Presented by Prof. Satyajit De
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Programming what is C++
CMSC 202 Polymorphism.
Advanced Program Design with C++
Java Course Review.
CSC241: Object Oriented Programming
Polymorphism.
Inheritance and Run time Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Classes in C++ C++ originally called "C with classes":
Object-Oriented Programming (OOP) Lecture No. 32
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Control Statement Examples
Chapter 2 Elementary Programming
Classes in C++ C++ originally called "C with classes":
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Inheritance and Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Chapter 20- Virtual Functions and Polymorphism
Programming II Polymorphism A.AlOsaimi.
Polymorphism Pattern.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Chapter 20 - C++ Virtual Functions and Polymorphism
Inheritance: Polymorphism and Virtual Functions
CISC/CMPE320 - Prof. McLeod
CPS120: Introduction to Computer Science
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Object-Oriented Programming (OOP) Lecture No. 43
CS150 Introduction to Computer Science 1
Fundamental Programming
Java Programming: From the Ground Up
CPS120: Introduction to Computer Science
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
CS410 – Software Engineering Lecture #8: Advanced C++ III
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:

Object-Oriented Programming (OOP) Lecture No. 28

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

Shape Hierarchy Shape draw calcArea Line Circle Triangle draw calcArea

Shape Hierarchy class Shape { … protected: char _type; public: 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[1] = new Line(p1, p2); shape[2] = 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<Line*>(_shape[i])->draw(); break; case ‘C’: static_cast<Circle*>(_shape[i]) ->draw(); … }

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

Sample Output Line Circle Triangle …

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<Line*>(_shape[i]) ->calcArea(); break; case ‘C’: static_cast<Circle*>(_shape[i]) … }

…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 draw calcArea Line Circle Triangle draw calcArea

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

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

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

Sample Output Line Circle Triangle …

Function printArea void printArea(Shape* _shape[], int size) { for (int i = 0; i < size; i++) { // Print shape name cout<< _shape[i] ->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->draw(); // Line::draw called // if draw() is virtual