polymorphism l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Polymorphism PowerPoint Presentation
Download Presentation
Polymorphism

Loading in 2 Seconds...

play fullscreen
1 / 17

Polymorphism - PowerPoint PPT Presentation


  • 361 Views
  • Uploaded on

Polymorphism. Static & Dynamic Binding Virtual Functions Abstract & Concrete Classes Overloading & Overriding. Class Pear & Truffle. class Pear { protected: int a,b; public: Pear(int x=0, int y=0): a(x),b(y){} int add(){return a+b;} friend ostream &operator<<

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Polymorphism' - paul


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
polymorphism
Polymorphism
  • Static & Dynamic Binding
  • Virtual Functions
  • Abstract & Concrete Classes
  • Overloading & Overriding
class pear truffle
Class Pear & Truffle

class Pear {

protected:

int a,b;

public:

Pear(int x=0, int y=0): a(x),b(y){}

int add(){return a+b;}

friend ostream &operator<<

(ostream &apple,const Pear &p)

{apple<<p.a<<”, ”<<p.b;

return apple;}

};

class Truffle:public Pear{

int c;

public: Truffle(int x=0, int y=0, int z=0)

:Pear(x,y), c(z){ }

int add()

{return Pear::add()+c;}

friend ostream &

operator<<

(ostream & banana,

const Truffle & t){

Pear p =

static_cast<Pear>(t);

banana<<p<<", "<<t.c;

return banana;

}

};

problem
Problem
  • The add function for Pear will be called instead of the add function for Truffle
  • See truffle.txt

int main(){

Truffle t(3,4,5);

Pear *ptr;

ptr = &t;

cout<<ptr->add()<<endl; //7

return 0;

}

static binding
Static Binding
  • Normally, which type of function gets invoked depends on the type of pointer

Pear *ptr; //declares a pointer to Pear object

ptr->add(); //calls the Pear add function

  • This is due to static binding, which means we identify which function to call at compile time
  • We would like this decision to be made during execution
    • Look at what type of object the pointer points to
    • Call the appropriate function based on this type
dynamic binding
Dynamic Binding
  • Virtual functions allow derived classes to have their own version of the base class functions
  • The system uses dynamic binding (or “late binding”) to determine which version of the function to invoke at runtime

Truffle t(3,4,5); //declares Truffle object

Pear *ptr; //declares pointer to Pear object

ptr = &t; //pointer points to Truffle object

cout<<ptr->add(); //with dynamic binding,

//this now calls the Truffle add function

making add function virtual
Making add Function Virtual

class Pear {

protected:

int a,b;

public:

Pear(int x=0, int y=0): a(x),b(y){}

virtual int add(){return a+b;}

friend ostream &operator<<

(ostream &apple,const Pear &p)

{apple<<p.a<<”, ”<<p.b;

return apple;}

};

class Truffle:public Pear{

int c;

public: Truffle(int x=0, int y=0, int z=0)

:Pear(x,y), c(z){ }

int add()

{return Pear::add()+c;}

friend ostream &

operator<<

(ostream & banana,

const Truffle & t){

Pear p =

static_cast<Pear>(t);

banana<<p<<", "<<t.c;

return banana;

}

};

problem solved
Problem Solved
  • Now the appropriate add function will be called
    • Based on the object that is being pointed to, instead of being based on the pointer’s class
  • See truffleVirtual.txt

int main(){

Truffle t(3,4,5);

Pear *ptr;

ptr = &t;

cout<<ptr->add()<<endl; //12

return 0;

}

class exercise 1
Class Exercise 1
  • On program on next slide
    • What is the output if the CC class’s identity function IS declared virtual
    • What is the output if the CC class’s identity function IS NOT declared virtual
    • See exercise1V.txt & exercise1nV.txt
slide9
class CC {

public:

virtual void identity()

{cout<<"Community College"<<endl;}

};

class Kapiolani:public CC {

public:

void identity()

{cout<<"Kapiolani Community College"<<endl;}

};

class Honolulu:public CC {

public:

void identity()

{cout<<"Honolulu Community College"<<endl;}

};

void main(){

CC *schools[2];

schools[0] = new Kapiolani();

schools[1] = new Honolulu();

for(int i=0;i<2;i++)

schools[i]->identity();

CC c;

c.identity();

Kapiolani k;

k.identity();

c=k;

c.identity();}

virtual functions
Virtual Functions
  • A function is virtual if
    • It is declared virtual
    • Or, there is a base class function with the same signature that is declared virtual
  • Signature
    • Consists of function name plus the types and counts of all the parameters of the function
polymorphism11
Polymorphism
  • Polymorphism
    • The ability of objects of different classes related by inheritance to respond differently to the same member function call
  • To implement polymorphism, we need both
    • Virtual functions
    • Dynamic binding
example
Example
  • See binOp.txt
    • Uses nodes of different classes to create a syntax tree to evaluate an arithmetic expression
    • Class hierarchy
      • Class Node has children BinOp & Data
      • Class BinOp has children Plus & Times
      • All have function eval()
abstract concrete class
Abstract & Concrete Class
  • Abstract class (or abstract base class)
    • Cannot instantiate objects from this kind of class (but pointers are OK)
    • A class is made abstract by declaring one or more of its virtual functions “pure”

virtual void identity() = 0;

  • Concrete class
    • Can instantiate objects from this kind of class
example14
Example

class CC {

public:

virtual void identity()=0;

};

class Kapiolani:public CC {

public:

void identity(){

cout<<"Kapiolani Community College"<<endl;}

};

class Honolulu:public CC {

public:

void identity(){

cout<<"Honolulu Community College"<<endl;}

};

void main(){

CC *ptr = new Kapiolani();

Honolulu h;

CC *ptr2 = &h;

CC c; //error (See CCpure.cpp)

overloading
Overloading
  • Two or more functions with same name, but different signatures

void classA::f (int, double)

void classA::f (char, int)

void classB::f (char, char)

  • Uses static binding
    • Identify which function to call at compile time
      • classA *x = new classB;
      • x->f (int, double) will call classA’s function f (int, double)
      • x->f (char, char) will be an error (classA does not have this function)
overriding
Overriding
  • Same name and same signature, but one or more base & one or more derived classes
    • void classA::f(int)
    • void classB::f(int)
  • Uses dynamic binding
    • Identify which function to call at run time
      • classA *x = new classB;
      • x->f (int) will call classB’s function f (int)
class exercise 2
Class Exercise 2
  • See exercise2.txt