DOC PREVIEW
UT CS 345 - Lecture notes

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 345 slide 321 Spring 200521 April 2005Uses of virtual functions1. Virtual functions can be used to define interfacesindependently from implementations.class bufdef // abstract base class{ public: virtual int put(char x) = 0; virtual char get() = 0; // These are “pure” virtual functions: they // have no bodies. // Note: no constructor!};Note: No instances of bufdef can ever be created;its only use is as base for derived classes (likemammal).It is a pure interface definition.Clients can use bufdef as invoid copy( bufdef& b ){ ... b.put(c); ... ... x = b.get(); ...}When an application of copy is type-checked, an argu-ment of any class derived from bufdef which definesput and get is OK.So copy is a generic function, but not fully poly-morphic— its argument’s type must be derived frombufdef.CS 345 slide 322 Spring 200521 April 2005Classes derived from bufdef add implementations:class fixbuf : public bufdef{ public: fixbuf(){ ... } int put(char); // put and get have the same signatures char get(); // as their virtual counterparts. private: ...};class linkbuf : public bufdef{ public: linkbuf(){ ... } int put(char); char get(); private: ...};void main(){ fixbuf f; linkbuf g; copy (f); // uses put and get of fixbuf copy (g); // uses put and get of linkbuf} fixbuf  bufdef and linkbuf  bufdef, i.e., each has allof bufdef’s public members.Hence an instance of either fixbuf or linkbuf cansatisfy any requirement for an instance of bufdef.CS 345 slide 323 Spring 200521 April 2005Java has a separate kind of declaration for interfaces:public interface bufdef {int put( char x );char get();}...class fixbuf implements bufdef {fixbuf(){ ... }public int put( char x ){ ... }public char get(){ ... }}...class linkbuf implements bufdef { linkbuf(){ ... }public int put( char x ){ ... }public char get(){ ... }}CS 345 slide 324 Spring 200521 April 2005In Haskell, interfaces can be defined using typeclasses:class Bufdef a where put :: (a,Char) -> Intget :: a -> Charinstance BufDef fixbuf where put (b,c) = ... get b = ...instance BufDef linkbuf where put (b,c) = ... get b = ...copy :: Bufdef a => a -> ()copy b = ... put (b,c) ... x = get b ...A C++ abstract base class can have some implement-ed methods (i.e., member functions). Like a Haskellclass’s default definitions, these could be inherited bysubclasses/instance types.In both languages, they would overridden bydefinitions in• derived classes (i.e., subclasses) in C++• instance declarations in HaskellCS 345 slide 325 Spring 200521 April 20052. Virtual functions can be used to alter a bindingfrom one member function to another duringexecution.Example: Prime numbers again, this time in C++.counter (2) 2sieve counter(2) 3filter ( 2) 3sieve counter(2) 4,5filter ( 2) 5filter ( 3) 5 sieve First attempt:class Counter{ int value;public: Counter(int v){ value = v; } int out(){ return value++; }};class Filter{ int factor; ??? *source;public: Filter(??? *src, int f) { source = src; factor = f; } int out();};CS 345 slide 326 Spring 200521 April 2005class Sieve{ ??? *source;public: Sieve(??? *src) { source = src; } int primes();};void main(){ Counter c(2); Sieve s(&c); for(;;) { int next = s.primes(); cout << next << ", ";} }int Filter::out(){ for(;;) { int n = source->out(); if (n % factor) return n; }}int Sieve::primes(){ int n = source->out(); source = new Filter(source,n); return n;}Problem: what type is ‘???’ ?• sometimes Counter• sometimes Filter.CS 345 slide 327 Spring 200521 April 2005A solution using class derivation:class Emitter{public: virtual int out()=0; // place-holder};class Counter : public Emitter{ int value;public: Counter( int v ){ value = v; } int out(){ return value++; }};class Filter : public Emitter{ int factor; Emitter *source;public: Filter( Emitter* src, int f ) { source = src; factor = f; } int out();};int Filter::out(){ for(;;) { int n = source->out(); if( (n % factor) > 0 ) return n; }}CS 345 slide 328 Spring 200521 April 2005class Sieve{ Emitter* source;public: Sieve( Emitter* src ){ source = src; } int primes();};int Sieve::primes(){ int n = source->out(); source = new Filter( source, n ); return n;}void main(){ Counter c(2); Sieve s( &c ); for(int k = 0; k < 100; k++ ) { int next = s.primes(); cout << next << ", ";


View Full Document

UT CS 345 - Lecture notes

Download Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?