


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Pure Virtual Functions. •A “pure virtual” function is not implemented in the base class. •must implement in derived classes.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



2/4/00 R-
2/4/00 R-
//client function (not a method) void printPoint( Point pt ) { pt.print( cout ); //the question: which print? }
Point p( 1.0, 9.0 ); ColorPoint cp( 6.0, 7.0, red );
printPoint( p ); p = cp; //information lost printPoint( p ); printPoint( cp );
2/4/00 R-
//client function void printPoint( Point *ptr ) { ofstream ofs( "point.out" ); ptr->print( ofs ); ofs.close(); }
Point *pptr = new Point( 1.0, 9.0 ); ColorPoint *cpptr = new ColorPoint( 6.0, 7.0, red );
printPoint( pptr ); printPoint( cpptr );
pptr = cpptr; printPoint ( pptr); 2/4/00 R-
Dynamic type can change during the program!
Point *myPointPointer = new ColorPoint( 3.14, 2.78, green );
2/4/00 R-
Point *myPointPointer = new ColorPoint( 3.14, 2.78, green );
myPointPointer->print( cout ); // myPointPointer is a Point*, so call Point::print
2/4/00 R-
but please use the keyword anyway for better style
2/4/00 R-
class Point { //base class public: virtual void print( ostream& os ); … };
class ColorPoint : public Point { //derived class public: virtual void print( ostream& os ); … };
//in a client Point *p = new ColorPoint( 3.13, 5.66, ochre );
p->print( cout ); // calls ColorPoint::print( ) 2/4/00 R-
Point *p = new ColorPoint( 3.13, 5.66, ochre ); p->print( cout );
2/4/00 R-
2/4/00 R-
Won’t happen unless laugh is virtual in mammal class
2/4/00 R-
2/4/00 R-
2/4/00 R-