Shape Hierarchy-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Main topics in this course are object-orientation, objects and classes, overloading, inheritance, polymorphism, generic programming, exception handling, introduction to design patterns. This lecture includes: Shape, Hierrarchy, Class, Public, Void, Int, Static, Cast, Type, Circle, If, Else, Logic

Typology: Slides

2011/2012

Uploaded on 08/08/2012

anchita
anchita 🇮🇳

4.4

(7)

113 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object
Object-
-Oriented Programming
Oriented Programming
(OOP)
(OOP)
Lecture No. 28
Lecture No. 28
Problem Statement
Problem Statement
Develop a function that can draw different
Develop a function that can draw different
types of geometric shapes from an array
types of geometric shapes from an array
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Shape Hierarchy-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object

Object

Oriented Programming

Oriented Programming

(OOP)

(OOP)

Lecture No. 28

Lecture No. 28

Shape Hierarchy

Shape Hierarchy

Shape

Line Circle Triangle

draw

calcArea

draw

calcArea

draw

calcArea

draw

calcArea

Shape Hierarchy

Shape Hierarchy

class Triangle : public Shape {

class Triangle : public Shape {

public:

public:

Triangle(Line l1, Line l2, Triangle(Line l1, Line l2,

double angle)

double angle)

void draw(){ cout <<

void draw(){ cout << “

Triangle

Triangle \

\

n

n ”

int calcArea() { int calcArea() { …… }}

Function drawShapes()

Function drawShapes()

void drawShapes(Shape* _shape[],

void drawShapes(Shape* _shape[],

int size) {

int size) {

for (int i = 0; i < size; i++) {

for (int i = 0; i < size; i++) {

_shape[i]

_shape[i]

>draw();

>draw();

Equivalent If Logic

Equivalent If Logic

if ( _shape[i]

if ( _shape[i]

getType() ==

getType() == ‘

‘ L

L ’

’ )

)

static_cast<Line*>(_shape[i])

static_cast<Line*>(_shape[i])

  • draw();

draw();

else if ( _shape[i]

else if ( _shape[i]

getType() ==

getType() == ‘

‘ C

C ’

’ )

)

static_cast<Circle*>(_shape[i])

static_cast<Circle*>(_shape[i])

  • draw();

draw();

… …

Problems with SwitchProblems with Switch

Statement

Statement

Delocalized Code

Delocalized Code

The above switch logic is same as was in

The above switch logic is same as was in

function drawArray()

function drawArray()

Further we may need to draw shapes or

Further we may need to draw shapes or

calculate area at more than one places in

calculate area at more than one places in

code code

Solution?

Solution?

To avoid switch, we need a mechanism that

To avoid switch, we need a mechanism that

can select the message target

can select the message target

automatically!

automatically!

Shape Hierarchy Revisited

Shape Hierarchy Revisited

class Shape {

class Shape {

virtual void draw();

virtual void draw();

virtual int calcArea(); virtual int calcArea();

class Line : public Shape { class Line : public Shape {

virtual void draw(); virtual void draw();

No type field

Function drawShapes()

Function drawShapes()

void drawShapes(Shape* _shape[], void drawShapes(Shape* _shape[],

int size) {

int size) {

for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {

_shape[i] _shape[i]-->draw();>draw();

Static vs Dynamic Binding

Static vs Dynamic Binding

Line _line;

Line _line;

_line.draw(); _line.draw(); // Always Line::draw// Always Line::draw

// called // called

Shape* _shape = new Line(); Shape* _shape = new Line();

_shape

_shape

draw(); // Shape::draw called

draw(); // Shape::draw called

// if draw() is not virtual // if draw() is not virtual

Shape* _shape = new Line();

Shape* _shape = new Line();

_shape _shape-->draw(); // Line::draw called>draw(); // Line::draw called

// if draw() is virtual

// if draw() is virtual