8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
1/31
Object-Oriented Programming(OOP)
Lecture No. 28
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
2/31
Problem Statement
Develop a function that can draw differenttypes of geometric shapes from an array
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
3/31
Shape Hierarchy
Shape
Line Circle Triangle
draw
calcArea
drawcalcArea
drawcalcArea
drawcalcArea
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
4/31
Shape Hierarchy
class Shape {
protected:
char _type; public:Shape() { }void draw(){ cout
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
5/31
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
6/31
Shape Hierarchy
class Circle : public Shape {
public:
Circle(Point center, double radius){
}
void draw(){ cout
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
7/31
Shape Hierarchy
class Triangle : public Shape {
public:
Triangle(Line l1, Line l2,double angle)
{ } void draw(){ cout
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
8/31
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;}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
9/31
Function drawShapes()
void drawShapes(Shape* _shape[],int size) {
for (int i = 0; i < size; i++){
_shape[i]->draw();}
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
10/31
Sample Output
ShapeShapeShapeShape
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
11/31
Function drawShapes()
void drawShapes(Shape* _shape[], int size) {for (int i = 0; i < size; i++) {
// Determine object type with// switch & accordingly call// draw() method
}}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
12/31
Required Switch Logicswitch ( _shape[i]->getType() ){
case L : static_cast(_shape[i])->draw();
break;case C :
static_cast(_shape[i])->draw();
break;
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
13/31
Equivalent If Logic
if ( _shape[i]- >getType() == L ) static_cast(_shape[i])->draw();
else if ( _shape[i]- >getType() == C )static_cast(_shape[i])->draw();
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
14/31
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
15/31
Problems with Switch
Statement
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
16/31
Delocalized Code
Consider a function that prints area ofeach shape from an input array
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
17/31
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.}}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
18/31
Required Switch Logic
switch ( _shape[i]->getType() ){
case L :
static_cast(_shape[i])->calcArea(); break;case C :
static_cast(_shape[i])
->calcArea(); break; }
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
19/31
Delocalized Code
The above switch logic is same as was infunction drawArray()
Further we may need to draw shapes or
calculate area at more than one places incode
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
20/31
Other Problems
Programmer may forget a check
May forget to test all the possible cases
Hard to maintain
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
21/31
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
22/31
Polymorphism Revisited
In OO model, polymorphism means thatdifferent objects can behave in differentways for the same message (stimulus)
Consequently, sender of a message doesnot need to know the exact class of
receiver
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
23/31
Virtual Functions
Target of a virtual function call isdetermined at run-time
In C++, we declare a function virtual by
preceding the function header withkeyword virtual
class Shape { virtual void draw();
}
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
24/31
Shape Hierarchy
Shape
Line Circle Triangle
draw
calcArea
drawcalcArea
drawcalcArea
drawcalcArea
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
25/31
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
26/31
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
27/31
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
28/31
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
29/31
Function printArea
void printArea(Shape* _shape[],int size) {
for (int i = 0; i < size; i++) {
// Print shape namecoutcalcArea();
cout
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
30/31
Static vs Dynamic Binding
Static binding means that target functionfor a call is selected at compile time
Dynamic binding means that targetfunction for a call is selected at run time
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 28
31/31