DOC PREVIEW
UT CS 345 - Deriving a new class from IntArray

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 313 Spring 200519 April 2005Deriving a new class from IntArrayClass declaration:#include "IntArray.h"class IntArrayRC : public IntArray{ public: IntArrayRC( int ); int& operator[]( int ); private: void rangeCheck( int );};Class implementation:IntArrayRC::IntArrayRC( int s ) :IntArray( s ) {}int& IntArrayRC::operator[]( int index ){ rangeCheck( index ); return IntArray::operator[]( index );}void IntArrayRC::rangeCheck( int index ){ if ( index < 0 || index >= getSize() ) { cerr << "Bad index for IntArrayRC: " << "\n\tsize: " << size << "\tindex: " << index << "\n"; exit( 1 ); }}CS 345 slide 314 Spring 200519 April 2005A demonstration run of IntArrayRC:eart h % cat >s.c#include <stream.h>#include "IntArray.h"// ... definitions of IntArrayRC ...const size = 12;main(){ IntArrayRC A( size ); for( int i = 1; i <= size; ++i ) A[ i ] = i;}earth% g++ s.c IntArray.aearth% a.outBad index for IntArrayRC: size: 12 index: 12Considering• classes as sets• derived classes as subsets,IntArray RC  IntArrayx  IntArrayRC x  IntArrayi.e., an instance of IntArrayRC can be used wherever aninstance of IntArray is expected.CS 345 slide 315 Spring 200519 April 2005That’s because an IntArrayRC is a kind of IntArray.For example, if we define#include "IntArray.h"void swap( IntArray &a, int i, int j ){ int temp = a[i]; a[i] = a[j]; a[j] = temp;}then the following is perfectly valid:IntArray A;IntArray RC R;swap( A, 3, 7 );swap( R, 3, 7 ); // because R is an IntArrayThe other way around, however, is not valid:#include "IntArray.h"void swapRC( IntArray RC &r, int i, int j ){ int temp = r[i]; r[i] = r[j]; r[j] = temp;}IntArray A;IntArrayRC R;swapRC( A, 3, 7 ); // type error hereswapRC( R, 3, 7 ); // OKEven in the valid case, there’s a problem:swap() always uses IntArray::operator[],so indexes are not range-checked.CS 345 slide 316 Spring 200519 April 2005We can fix this by changing IntArray’s declaration tomake operator[] a virtual function:class IntArray{public: ... virtual int& operator[]( int ); ...}After this change, swap() calls the version ofoperator[] that belongs to its argument.Unlike the overloading we’ve seen previously, thiskind of polymorphism is resolved at run-time.It’s called dynamic polymorphism.The cost of dynamic polymorphism is the overheadof virtual functions:• function code is accessed through a table (smallcost: two extra memory references)• in-line expansion is no longer possible(function-call-and-return overhead is unavoid-able, and some optimizations cannot beobtained).Note: In Java, all member functions are auto-matically “virtual” (unless declared “final”).CS 345 slide 317 Spring 200519 April 2005How are virtual functions implemented?Example:class A{public: int a; virtual void f( A& ); virtual void g( int ); virtual void h( double );};class B : public A{public: int b; void g( int ); // overrides A::g virtual void m( B* );};class C : public B{public: int c; void h( double ); // overrides A::h virtual void n( C* );};CS 345 slide 318 Spring 200519 April 2005Virtual-function calls: An examplevoid F1( A& p ){ p.h(9.5); }...C x; ... F1(x) ...F1(x) is handled by indirection:vtblavptrbc&A::f&B::g&C::h&B::m&C::n•••••••••••••••C::nB::mC::hB::gA::fpABCThe call p.h(9.5) becomes something like ((p->vptr)[2])(p, 9.5)The class-C vtbl is shared by all instances of C.CS 345 slide 319 Spring 200519 April 2005 Virtual functions work only for reference andpointer arguments.By-value arguments are always monomorphic:Whatever the argument’s actual type, it istreated like a value of the parameter’s type(which it is). Pointer arithmetic is not compatible withdynamic polymorphism.When• a base-class pointer A *p actually pointsto an array of derived-class objects C and• sizeof( C ) > sizeof( A )arithmetic on p is performed in units ofsizeof( A ), i.e.,p+1 == p + sizeof( A )CS 345 slide 320 Spring 200519 April 2005A virtual-function exercise:class A{ public: virtual char f() {return 'A';} char g() {return 'A';} virtual char h() {return 'A';} char testF() {return f();} char testG() {return g();} char testH() {return h();}};class D : public A{ public: char f() {return 'D';} char g() {return 'D';} // D inherits testF, testG, testH};void main(){ D d;cout << d.testF() << d.testG() << d.testH();}Result: _____What would be printed if virtual char f(){...} wereomitted from


View Full Document

UT CS 345 - Deriving a new class from IntArray

Download Deriving a new class from IntArray
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 Deriving a new class from IntArray 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 Deriving a new class from IntArray 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?