Title: ECE 538 Object Oriented Concepts Session 14
1ECE 538 Object Oriented ConceptsSession 14
- Dr. John G. Weber
- John.Weber_at_notes.udayton.edu
- http//academic.udayton.edu/JohnWeber
2Virtual 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()
3Example (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
4Example 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
5Example 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
6How 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
7A 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) /////////////////////////
///////////////////////////////////////
8A 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)
//////////////////////////////////////
//////////////////////////
9A 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)
10A 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
11Abstract 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
12A 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)
13A 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)
14A 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
15A 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()
16A 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()
17A 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
18Virtual 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
19Example
//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
20Example(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
21Design 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