Docsity

Static and Dynamic Binding - Algorithms and Abstract Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Static and Dynamic Binding - Algorithms and Abstract Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

CPSC 223

Algorithms & Data Abstract Structures

Lecture 7: 

Static and Dynamic Binding,

Algorithm Analysis Intro

Today …

  • Quiz 4
  • Static vs. Dynamic Binding [Sect. 8.2]
  • Intro to Complexity Analysis [Sect. 9.1] CPSC 223 -­‐-­‐ Fall 2010 2

Methods and Inheritance

What is method overloading?

  • Two functions in the same class have the same name
  • But different arguments (types, number of args)
  • E.g.: getStockValue() vs. getStockValue(double fee)

What about with inheritance?

  • Derived class methods with the same name as base class methods … are hidden (regardless of args) CPSC 223 -­‐-­‐ Fall 2010 3

Method Hiding

class Account { public: ... void deductFee(double fee); ... }; class Checking : public Account { public: ... void deductFee(); ... }; CPSC 223 -­‐-­‐ Fall 2010 4

If the client writes:

Checking c;

c.deductFee(10);

What happens?

  • A compile error!!!
  • The method is hidden

Static Binding

Static binding is when a method is “bound” to an object

based on its static type

  • the static type is the declared type

A method statically bound to an object can

  • be a method of the object’s class
  • or be an inherited method of a superclass

Static binding is also called compile time binding

CPSC 223 -­‐-­‐ Fall 2010 7

Static Binding

Whenever we need a base class object, we can use a

derived class object in its place …

CPSC 223 -­‐-­‐ Fall 2010 8 Account Checking Account::getBalance() balance = 500. Checking::getMonthlyFees() Checking::getBalance() monthyFees = 10. Checking object Account object (part of the checking account object)

Static Binding

When the checking object is assigned to an account

object, we lose the derived parts

  • Here we are copying c into an Account object a
  • For c the checking account method is used
  • For a the account method is used
  • These are statically bound by the compiler CPSC 223 -­‐-­‐ Fall 2010 9 Checking c(500, 5); cout << c.getBalance(); Account a = c; cout << a.getBalance();

What is printed?

Upcasting

  • Casting changes the type of an object
  • “ Upcasting ” casts a derived type to a base type
  • Includes address types (pointers, references) Checking* cptr = new Checking(500, 5); Account* aptr = cptr;
  • The last statement upcasts the address types CPSC 223 -­‐-­‐ Fall 2010 10

Dynamic Binding (Virtual Functions)

Dynamic binding associates a member function based on

the object’s dynamic type

  • The type of the object, not the type of the pointer or reference
  • To be dynamically bound, the member function must override a virtual function declared in a base class
  • Since dynamic binding happens at run time, it is also called run time binding CPSC 223 -­‐-­‐ Fall 2010 13

Method Overriding

To Override a method in C++

  • Method must have the same signature as in the base class
  • Base class method must use the C++ “virtual” keyword CPSC 223 -­‐-­‐ Fall 2010 14

Virtual Functions

CPSC 223 -­‐-­‐ Fall 2010 15 Checking c(500, 5); cout << c.getBalance(); Account a = c; cout << a.getBalance(); Account* aptr = &c; cout << aptr->getBalance(); // base class class Account { public: Account(double bal = 0); virtual double getBalance(); private: double balance; }; // derived class class Checking : public Account { public: Checking(double b = 0, double f = 5); double getMonthlyFees(); double getBalance(); private: double monthlyFees; }; // 495 // 500 // 495 What is printed?

Virtual Functions

  • Use virtual to enable dynamic method binding
  • Any subclass class can override the function
  • Overriding requires methods to have same args

Once virtual always virtual

  • The virtual keyword does not need to be re-specified within subclasses
  • Although doing so is good practice CPSC 223 -­‐-­‐ Fall 2010 16

The List retrieve operation

Using a linked list void List::retrieve(int index, ListItemType& item) { ... check if valid index ... Node* curr = head; for(int i = 1; i < index; i++) curr = curr->next; item = curr->item; } Using an array void List::retrieve(int index, ListItemType& item) { ... check if valid index ... item = items[index-1]; } Which do you think is more efficient? CPSC 223, 2009 19 We compare algorithms based on:

  • the number of operations
  • and the cost of these operations

The List retrieve operation

Using a linked list

void List::retrieve(int index, ListItemType& item) { ... check if valid index ... Node* curr = head; for(int i = 1; i < index; i++) curr = curr->next; item = curr->item; }

Notice that for the linked list:

  • The number of operations (steps) required …
    • depends on the size of the list
    • and the position we are trying to retrieve CPSC 223, 2009 20

The List retrieve operation

Using an array

void List::retrieve(int index, ListItemType& item) { ... check if valid index ... item = items[index-1]; }

Whereas for the array:

• The number of operations (steps) required …

  • is constant
  • Size of the list and the index don’t change the cost CPSC 223, 2009 21

Algorithm Analysis

• In this class, we focus on the amount of time it takes

an algorithm to solve a problem

  • This is in contrast to the amount of memory (or space) used by an algorithm

• We also focus on worst-case algorithm efficiency

  • For example, when the index to retrieve is at the end of the linked list
  • This is in contrast to the best or average case CPSC 223, 2009 22

Algorithm Analysis

  • We consider two types of worst-case analyses: Order-of-magnitude (“ big O ” notation) focusing on the growth rate of an algorithm (as an upper bound) CPSC 223, 2009 25 Order-­‐of-­‐magnitude (big O) Assume: n items and index = n => Retrieve is “ order n ” written O ( n ) That is, retrieve requires .me propor.onal to n void List::retrieve(int index, ListItemType& item) { ... check if valid index ... Node* curr = head; for(int i = 1; i < index; i++) curr = curr->next; item = curr->item; }

Algorithm Analysis

  • We consider two types of worst-case analyses: Detailed (“ nitty-gritty ”) which counts operations and their costs CPSC 223, 2009 26 void List::retrieve(int index, ListItemType& item) { ... check if valid index ... item = items[index-1]; } (^) Detailed analysis Assume: n items and index = n What are the opera.ons and costs? Operations: s = subtraction; i = array access; a = assignment cost => 1· s + 1· i + 1· a If s , i = 1, and a = 1, the total cost is: 3 Note: Ignoring index check

Algorithm Analysis

  • We consider two types of worst-case analyses: Order-of-magnitude (“ big O ” notation) focusing on the growth rate of an algorithm (as an upper bound) CPSC 223, 2009 27 Order-­‐of-­‐magnitude (big O) Assume: n items and index = n => Retrieve is “ constant time ” denoted O (1) void List::retrieve(int index, ListItemType& item) { ... check if valid index ... item = items[index-1]; }

Algorithm Analysis .. The plan

  • We’ll walk through some examples of calculating the

“ nitty-gritty ” (detailed) time analysis

  • We’ll also discuss the concepts behind big O analysis
    • From the nitty-gritty cost we can obtain big O
    • From big O we cannot obtain the nitty-gritty cost
    • We can often compute big O without nitty-gritty (more later)
  • Start with searching and sorting algorithms ( next time ) CPSC 223, 2009 28