ECE 538 Object Oriented Concepts Session 14 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

ECE 538 Object Oriented Concepts Session 14

Description:

circle() : shape() //no-arg constr //5-arg constructor ... shape* pShapes[3]; //array of pointers to shapes //define three shapes ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 22
Provided by: johngwe
Category:

less

Transcript and Presenter's Notes

Title: ECE 538 Object Oriented Concepts Session 14


1
ECE 538 Object Oriented ConceptsSession 14
  • Dr. John G. Weber
  • John.Weber_at_notes.udayton.edu
  • http//academic.udayton.edu/JohnWeber

2
Virtual Functions
  • Essential for polymorphism
  • Allows a polymorphism of functions similar to
    data polymorphism of template class
  • Example
  • Suppose we have several different shapes (ball,
    triangle, cube, etc)
  • Suppose each shape is a class which has a member
    function draw() which draws the shape on the
    screen
  • Now suppose we want to draw a picture consisting
    of objects from the above classes
  • Create array to hold pointers to all the objects
    in the picture
  • shape ptrarr100
  • Draw picture with this loop
  • for (int j0 jltN J)
  • ptrarrj -gt draw()

3
Example (cont)
  • Note completely different functions are
    executed by the call
  • ptrarrj -gt draw()
  • Each class (ball, cube, etc) has a unique draw
    function
  • When drawing a ball, the draw function in the
    ball class is called
  • When drawing a cube, the draw function from the
    cube class is called
  • Conditions to make this work
  • First all classes (shapes in this example) must
    be descended from a single base class
  • Second draw() function must be declared to be
    virtual in the base class

4
Example without Virtual Functions
//novert.cpp // normal functions accessed from
pointer include ltiostreamgt using namespace
std /////////////////////////////////////////////
/////////////////// class Base
//base class public void show()
//normal function cout
ltlt "Base\n" /////////////////////////////
/////////////////////////////////// class Derv1
public Base //derived class 1
public void show() cout ltlt
"Derv1\n" ///////////////////////////////
///////////////////////////////// class Derv2
public Base //derived class 2
public void show() cout ltlt
"Derv2\n" ///////////////////////////////
///////////////////////////////// int main()
Derv1 dv1 //object of derived
class 1 Derv2 dv2 //object of
derived class 2 Base ptr //pointer
to base class ptr dv1 //put
address of dv1 in pointer ptr-gtshow()
//execute show() ptr dv2 //put
address of dv2 in pointer ptr-gtshow()
//execute show() return 0
5
Example with Virtual Functions
// virt.cpp // virtual functions accessed from
pointer include ltiostreamgt using namespace
std /////////////////////////////////////////////
/////////////////// class Base
//base class public virtual
void show() //virtual function
cout ltlt "Base\n" ///////////////////////
///////////////////////////////////////// class
Derv1 public Base //derived class 1
public void show() cout ltlt
"Derv1\n" //////////////////////////////
////////////////////////////////// class Derv2
public Base //derived class 2
public void show() cout ltlt
"Derv2\n" //////////////////////////////
////////////////////////////////// int main()
Derv1 dv1 //object of derived
class 1 Derv2 dv2 //object of
derived class 2 Base ptr //pointer
to base class ptr dv1 //put
address of dv1 in pointer ptr-gtshow()
//execute show() ptr dv2 //put
address of dv2 in pointer ptr-gtshow()
//execute show() return 0
6
How does the compiler know?
  • In novert, compiler translates ptr-gtshow() to
    the show function in base class
  • In vert, compiler doesnt know what class is
    referenced by the contents of ptrcould be draw()
    for Derv1 or draw() for Derv2
  • Compiler defers decision to run-time
  • Called late binding or dynamic binding

7
A Graphics Example
// multshap.cpp // balls, rects, and
polygons include "msoftcon.h" //for
graphics functions ///////////////////////////////
///////////////////////////////// class shape
//base class protected
int xCo, yCo //coordinates of shape
color fillcolor //color fstyle
fillstyle //fill pattern public
//no-arg constructor shape()
xCo(0), yCo(0), fillcolor(cWHITE),
fillstyle(SOLID_FILL)
//4-arg
constructor
shape(int x, int y, color fc, fstyle fs)
xCo(x), yCo(y), fillcolor(fc),
fillstyle(fs)
void draw() const //set color
and fill style
set_color(fillcolor) set_fill_style(fill
style) /////////////////////////
///////////////////////////////////////
8
A Graphics Example (Cont)
class circle public shape private
int radius //(xCo, yCo) is center
public circle() shape() //no-arg
constr
//5-arg constructor circle(int x, int y,
int r, color fc, fstyle fs)
shape(x, y, fc, fs), radius(r)
void draw() const //draw the circle
shapedraw()
draw_circle(xCo, yCo, radius)
//////////////////////////////////////
//////////////////////////
9
A Graphics Example (Cont)
class rect public shape private
int width, height //(xCo, yCo) is upper
left corner public rect() shape(),
height(0), width(0) //no-arg ctor
//6-arg ctor
rect(int x, int y, int h, int w, color fc,
fstyle fs) shape(x, y, fc, fs), height(h),
width(w) void draw() const
//draw the rectangle
shapedraw() draw_rectangle(xCo, yCo,
xCowidth, yCoheight)
set_color(cWHITE) //draw diagonal
draw_line(xCo, yCo, xCowidth, yCoheight)
class tria public shape
private int height //(xCo,
yCo) is tip of pyramid public tria()
shape(), height(0) //no-arg constructor
//5-arg constructor
tria(int x, int y, int h, color fc, fstyle fs)
shape(x, y, fc, fs), height(h)
void draw() const //draw the triangle
shapedraw()
draw_pyramid(xCo, yCo, height)
10
A Graphics Example (Cont)
int main() init_graphics()
//initialize graphics system circle cir(40,
12, 5, cBLUE, X_FILL) //create circle
rect rec(12, 7, 10, 15, cRED, SOLID_FILL)
//create rectangle tria tri(60, 7, 11,
cGREEN, MEDIUM_FILL) //create triangle
cir.draw() //draw all shapes
rec.draw() tri.draw() set_cursor_pos(1,
25) //lower left corner return 0
11
Abstract Classes and Pure Virtual Functions
  • The shape class is an abstract class
  • We will not make an object shape but only objects
    from the derived classescircles, rectangles,
    triangles
  • Shape exists only as a parent of the derived
    classes
  • How can we ensure that someone doesnt try to
    instantiate a shape object?
  • Place a pure virtual function in the shape class
  • Pure virtual function is one with 0 added to the
    declaration
  • Lets look at multishap modified to be vertshap

12
A Graphics Example
// virtshap.cpp // virtual functions with
shapes include ltiostreamgt using namespace
std include "msoftcon.h" //for graphics
functions ////////////////////////////////////////
//////////////////////// class shape
//base class protected int
xCo, yCo //coordinates of center
color fillcolor //color fstyle
fillstyle //fill pattern public
//no-arg constructor shape()
xCo(0), yCo(0), fillcolor(cWHITE),
fillstyle(SOLID_FILL)
//4-arg
constructor
shape(int x, int y, color fc, fstyle fs)
xCo(x), yCo(y), fillcolor(fc),
fillstyle(fs) virtual void
draw() 0 //pure virtual draw function
set_color(fillcolor)
set_fill_style(fillstyle)
13
A Graphics Example (Cont)
//////////////////////////////////////////////////
////////////// class ball public shape
private int radius //(xCo,
yCo) is center public ball() shape()
//no-arg constr
//5-arg constructor
ball(int x, int y, int r, color fc, fstyle fs)
shape(x, y, fc, fs),
radius(r) void draw()
//draw the ball
shapedraw() draw_circle(xCo, yCo,
radius)
14
A Graphics Example (Cont)
int main() int j init_graphics()
//initialize graphics system shape
pShapes3 //array of pointers to
shapes //define
three shapes pShapes0 new ball(40, 12, 5,
cBLUE, X_FILL) pShapes1 new
rect(12, 7, 10, 15, cRED, SOLID_FILL)
pShapes2 new tria(60, 7, 11, cGREEN,
MEDIUM_FILL) for(j0 jlt3 j)
//draw all shapes pShapesj-gtdraw()
for(j0 jlt3 j) //delete all shapes
delete pShapesj
set_cursor_pos(1, 25) return 0
15
A Motion Example
class shape //base class
protected int xCo, yCo
//coordinates of center color fillcolor
//color fstyle fillstyle //fill
pattern public shape() xCo(0),
yCo(0), fillcolor(cWHITE), fillstyle(SOLID_FILL)
//no-arg constructor shape(int x,
int y, color fc, fstyle fs) xCo(x), yCo(y),
fillcolor(fc), fillstyle(fs) //4-arg
constructor int get_xCo return
xCo int get_yCo return yCo
virtual void draw() 0 //pure virtual
draw function
set_color(fillcolor) set_fill_style(fil
lstyle) void erase()
fillcolor cBLACK draw() virtual
void move (int xCn, int yCn) color
oldfillcolor oldfillcolor fillcolor
erase() fillcolor oldfillcolor xCo
xCn yCo yCn draw()
16
A Motion Example(Cont)
class rect public shape private
int width, height //(xCo, yCo) is upper
left corner public rect() shape(),
height(0), width(0) //no-arg ctor rect(int
x, int y, int h, int w, color fc, fstyle fs)
shape(x, y, fc, fs), height(h), width(w) //6-arg
ctor void draw()
//draw the rectangle
shapedraw() draw_rectangle(xCo, yCo,
xCowidth, yCoheight)
set_color(cWHITE) //draw diagonal
draw_line(xCo, yCo, xCowidth, yCoheight)
void move (int xCn, int yCn)
color oldfillcolor oldfillcolor
fillcolor erase() set_color(cBLACK)
//draw diagonal draw_line(xCo, yCo,
xCowidth, yCoheight) fillcolor
oldfillcolor xCo xCn yCo yCn
draw()
17
A Motion Example(Cont)
int main() int j,k,l init_graphics()
//initialize graphics system
shape pShapes3 //array of pointers
to shapes
//define three shapes pShapes0 new
ball(40, 5, 2, cBLUE, X_FILL)
pShapes1 new rect(12, 5, 2, 3, cRED,
SOLID_FILL) pShapes2 new tria(60, 5, 3,
cGREEN, MEDIUM_FILL) for(j0 jlt3 j)
//draw all shapes
pShapesj-gtdraw() for(k0 klt1000000000
k) //delay for(k0 klt5 k)
for(j0jlt3j) pShapesj-gtmove(pShapes
j-gtget_xC() k,pShapesj-gtget_yC()
k) for(l0 llt100000000 l) //delay
for(j0 jlt3 j)
//delete all shapes delete pShapesj
set_cursor_pos(1, 25) return
0
18
Virtual Destructors
  • Base class destructors should always be virtual
  • Suppose you use delete with a base class pointer
    to a derived class object to destroy the derived
    class object
  • If base class destructor not virtual
  • Delete calls the destructor for the base class,
    not the derived class
  • Results in only the base class part of the object
    being destroyed

19
Example
//vertdest.cpp //tests non-virtual and virtual
destructors include ltiostreamgt using namespace
std /////////////////////////////////////////////
/////////////////// class Base public
Base() //non-virtual
destructor // virtual Base()
//virtual destructor cout ltlt "Base
destroyed\n" ////////////////////////////
//////////////////////////////////// class Derv
public Base public Derv()
cout ltlt "Derv destroyed\n"
///////////////////////////////////////////////
///////////////// int main() Base pBase
new Derv delete pBase return 0
20
Example(cont)
//vertdest.cpp //tests non-virtual and virtual
destructors include ltiostreamgt using namespace
std /////////////////////////////////////////////
/////////////////// class Base public //
Base()
//non-virtual destructor virtual Base()
//virtual destructor cout
ltlt "Base destroyed\n" ///////////////////
///////////////////////////////////////////// clas
s Derv public Base public
Derv() cout ltlt "Derv destroyed\n"
/////////////////////////////////////////////
/////////////////// int main() Base
pBase new Derv delete pBase return 0

21
Design approach and milestones
  • July 13 class structure, UML and simple
    conversion
  • July 20 camera and controls
  • July 27 RADAR
  • July 30 Final Project by email
  • August 3 grades due
  • Project serves as final exam
Write a Comment
User Comments (0)
About PowerShow.com